/**
 * pSHIELD
 * Service Discovery
 *
 @author Davide Migliacci
 * Department of System and Computer Science (DIS)
 * University of Rome "Sapienza"
 * Via Ariosto, 25
 * 00184, Rome, IT
 *
 *
 * Created on 16-May-2007
 * Version 1.0
 
 */


package eu.artemis.shield.composition.compositionmanager.impl;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Frame;
import java.awt.TextArea;

import java.net.URL;

import javax.swing.BorderFactory;
import javax.swing.JPanel;


import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;

public class CMGUI extends Frame implements ActionListener 
{

  private static final int MAJOR = 0;
  private static final int MINOR = 1;
  
  private static final int width = 440;
  private static final int height = 440;
  
  private boolean working = true;
  
  private BundleContext bc = null;  
  private CM cm = null;
  
  private TextArea ta = null;
  
  private JPanel main_panel = null;
  private JPanel textarea_panel = null;
  private JPanel buttons_panel = null;
  
  private static final int intNumBtn = 3
  private static String[] strBtn = new String[intNumBtn];    
  
  // -----------------------------------------------
  //  CONSTRUCTORS
  // -----------------------------------------------    
  
  public CMGUI(BundleContext bc, CM cm)
    {    
      super("pSHIELD - Composition Engine v" + MAJOR + "." + MINOR);
      
      strBtn[0"Start/Stop";
      strBtn[1"Clear Log";
      strBtn[2"Hide Me";  
      
      this.bc = bc;
      this.cm = cm;
      
      addWindowListener(windowExit);
      createGUI();
            
      setSize(width,height);
        setForeground(Color.black);
        setBackground(Color.lightGray);
        
        /*
         * ENABLE / DISABLE THE GUI...
         */
        setVisible(true);
    }
  
  //  -----------------------------------------------
  //  EVENT HANDLER
  // -----------------------------------------------
    
    WindowAdapter windowExit = new WindowAdapter()
  {
      public void windowClosing(WindowEvent e
      
        actionExit();
        }
    };
    
    public void actionExit() 
    {       
        // Exiting...
      if (bc != null)
      {
        try 
        {
          bc.getBundle().stop();   
        }        
        catch (BundleException BE)
        {
          BE.printStackTrace();
        }
      }      
  }   
            
    public void actionPerformed(ActionEvent e
    {            
      if (e.getActionCommand().equals(strBtn[0]))
    {   
        if working ){
          append("\n");
          appendts("STOP \n");
          working = false;  
        }
        else {
          append("\n");
          appendts("START \n");
          working = true;
        }
    }
    else if (e.getActionCommand().equals(strBtn[1]))
    {
      ta.setText("- Bundle ready\n");
    }
    else if (e.getActionCommand().equals(strBtn[1]))
    {
      setVisible(false);
    }
    else
    {
      //Unknown Command
    }    
    }
    
    // -----------------------------------------------
  //  DESIGN GRAPHICS
  // -----------------------------------------------
    
    private void createGUI()
    {
      
      // Initialize the main Panel
    main_panel = new JPanel ( );
    main_panel.setLayoutnew BorderLayout() );
    main_panel.setBorderBorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(), BorderFactory.createLoweredBevelBorder()) );
    
    // Initialize the Combo Panel
    textarea_panel = new JPanel();
    
    GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        
        textarea_panel.setLayoutgridbag );
    
    c.gridwidth = 1;   // The cell occupies 1 column 
        c.gridheight = 1;  // The cell occupies 1 row
      c.gridx = 0;    // The cell is located in 1 column 
        c.gridy = 0;    // The cell is located in 1 row    
        c.weightx = 0.1;  // The cell occupies the minimum row length
    c.weighty = 0.1;  // The cell occupies the entire column length
    

      ta = new TextArea();
      ta.setEditable(false);
        gridbag.setConstraintsta, c);
        textarea_panel.add(ta);
    
    // Initialize the Buttons Panel
    buttons_panel = new JPanel();
    
    for (int i = 0; i < intNumBtn; i++)
      {
        /*
           * ADD BUTTONS TO START SEVERAL TEST
           */   
      c.gridx = i;      // The cell is located in (i+1) column
          Button b = new Button(strBtn[i]);
            b.addActionListener(this);
            buttons_panel.add(b);
      
    
    
    // Add the subpanels to the main panel 
    
    main_panel.addtextarea_panel, BorderLayout.CENTER );
    main_panel.addbuttons_panel, BorderLayout.PAGE_END );
    
    // Add the main panel to the Frame
    addmain_panel );
    
      /*
         * CENTER FRAME ON THE SCREEN
         */
        Dimension dialogSize = getSize();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation(screenSize.width/- dialogSize.width/2, screenSize.height/- dialogSize.height/2);
        
        /*
         * pSHIELD ICON 
         */
        try
      {  
          /*
           * Check if we are in a JAR file...
           */
          URL url = this.getClass().getResource("logo_pSHIELD_16x16.jpg");
          if (url != null)
          {
            this.setIconImage(Toolkit.getDefaultToolkit().createImage(url));  
          }                      
          else
          {
            /*
               * We are not in a JAR file...
               */                           
          }
      }
        catch(Exception e)
        {  
          e.printStackTrace();
        }
        
        appendts("Bundle started");
        
    }
    
    public void appendts(String str)
    {
      append("- " + str + "\n");      
    }   
    
    public void append(String str)
    {
      /*
       * Add text to the textArea
       */
      ta.setForeground(Color.BLACK);
      ta.append(str);
    }
    
}