
package BW.UML;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import BW.Interface.*;
import BW.kernel.*;
import BW.UML.*;
import BW.UML.ClickBar.*;

// Make a main window with two top-level menus: File and Help.
// Help has a submenu and demonstrates a few interesting menu items.
public class AwarenessMainWindow extends Frame implements UI_GUIElement, ActionListener, WindowListener {

  
  private UI_Interface manager;
  private Hashtable UI_Events; 


  //////////// variaveis da interface grafica

  MenuBar mb;
  Menu memberMenu;
  Menu eventsMenu;
  Menu eventsMenu_Profile;
  Menu exitMenu;
  Menu helpMenu;
  private Scroller sc;
  private boolean isSet;


  ////////////////  construtor
  
  public AwarenessMainWindow(UI_Interface manager) {

    super("Past Awareness Window");
    setSize(400, 280);

    this.manager = manager;
    makeMenu();  
    
    
   
    

    resetAllEvents();

    sc = null;
    isSet = false;
    Hide();

    addWindowListener (this);
  }

   /** limpa o hashtable que contem os vetores de eventos, deixando apenas o indice por prototipo
   */ 
   public void resetAllEvents () {
      UI_Events = new Hashtable();
      Vector prototypes = UML_BW_Mediator.getPrototypes(); 
      for (int i=0; i<prototypes.size(); ++i) {
         try {
	      UI_Events.put (this.mountKey((BW_Event)prototypes.elementAt(i)),new Vector());
	   } catch (NullPointerException np) {
              System.out.println("Ocorreu um null-pointer");
         }
      }
   }

   /** monta a chave que sera usada como indice no hashtable de eventos
   */

   public String mountKey (BW_Event obj) {
		   Class classe = obj.getClass();
		   StringBuffer key = new StringBuffer();
		   key.append ((obj.getClass().getName()).toUpperCase());
		   key.append("-");
		   key.append(obj.getTypeId());	         	
		   return key.toString();
	}

  /** torna a janela visivel 
  */
  public void Show ()
  {
     this.setVisible(true);
  }
  
  /** esconde a janela
  */
  public void Hide()
  {
     this.setVisible(false);
  }

  /** preenche os menus da janela, direcionando funcoes
  */
  public void makeMenu () {
     mb = new MenuBar();
     eventsMenu = new Menu ("Events");
     eventsMenu.add(new MenuItem("Get Events"));
     eventsMenu_Profile = new Menu ("Profiles");
     eventsMenu_Profile.add(new MenuItem("Personal Profiles"));
     eventsMenu_Profile.add(new MenuItem("Personal Paper"));
     eventsMenu.add(eventsMenu_Profile);
     mb.add(eventsMenu);
     memberMenu = new Menu ("Member");
     memberMenu.add(new MenuItem("Search Info"));
     memberMenu.add(new MenuItem("Modify Info"));
     mb.add(memberMenu);
     exitMenu = new Menu ("Exit");
     exitMenu.add(new MenuItem("Really?"));
     mb.add(exitMenu);
     helpMenu = new Menu ("Help");
     helpMenu.add(new MenuItem("Fundamental"));
     helpMenu.add(new MenuItem("Advanced"));
     mb.add(helpMenu);

     eventsMenu.addActionListener(this); 
     eventsMenu_Profile.addActionListener(this); 
	memberMenu.addActionListener(this); 
	exitMenu.addActionListener(this); 
	helpMenu.addActionListener(this); 
     

     setMenuBar(mb);
  }

   public void actionPerformed(ActionEvent e) {
      String item = e.getActionCommand();
      if (item.equals("Get Events"))
         click_getEvents();
      else
      if (item.equals("Personal Profiles"))
         click_personalProfiles();
      else
      if (item.equals("Personal Paper"))
         click_personalPaper();
      else
      if (item.equals("Search Info"))
         click_searchInfo();
      else
      if (item.equals("Modify Info"))
         click_modifyInfo();
      else
      if (item.equals("Really?"))
	   exit();
      else
	if (item.equals("Fundamental"))
	   click_fundamental();
      else
	if (item.equals("Advanced"))
	   click_advanced();
   }

   private void click_getEvents () {
      Notify();
   }

   private void click_personalProfiles() {
      AwarenessProfileWindow APW = new AwarenessProfileWindow(manager,manager.getMember(),true);     
   }

   private void click_personalPaper() {
      AwarenessProfileWindow APW = new AwarenessProfileWindow(manager,manager.getMember(),false);     
   }

   private void click_searchInfo() {
      AwarenessGroupInfoWindow AGIW = new AwarenessGroupInfoWindow(manager.getGroup());     
   }
 
   private void click_modifyInfo() {
      AwarenessUserInfoWindow AUIW = new AwarenessUserInfoWindow(manager,manager.getMember());     
   }

   private void click_fundamental() {
      String fundamental = new String("./fundamental.txt");
      AwarenessHelpWindow AHW = new AwarenessHelpWindow(fundamental);
   }

   private void click_advanced() {
      String advanced = new String("./advanced.txt");
      AwarenessHelpWindow AHW = new AwarenessHelpWindow(advanced);
   }

  
  public void Notify ()
  {
      String key;
      Vector evtPointer; 

      Vector events = (Vector)manager.getEvents(this).clone();
	System.out.println(" passou por aqui");
      resetAllEvents();
      for (int i=0; i<events.size(); ++i)
      {
         key = mountKey((BW_Event)events.elementAt(i));
         evtPointer = (Vector)UI_Events.get(key);
         evtPointer.addElement(events.elementAt(i));
      }
      setBarObjects();
  }

 
  public void itemStateChanged (ItemEvent e)
  {
     int state = e.getStateChange();
     if (state==ItemEvent.SELECTED)
	this.Show();
     else {
	if (state==ItemEvent.DESELECTED)
	   this.Hide();
     }
  }

  public void setUI_Interface (UI_Interface manager)
  {
    this.manager = manager;
  }

  public UI_Interface getUI_Interface ()
  {
     return manager;
  }

  public void addUI_GUIEvent (UI_GUIEvent guievent, BW_Event prototype)
  {
     /// nao faz nada, pois a estrutura hashtable nao foi construida para suportar diversos GUIEVents
  }

  public void exit() {
    setVisible(false); // hide the Frame
    dispose(); // tell windowing system to free resources
  }


  /** metodo invocado por um objeto bar, dispara a exibicao de detalhes de um determinado evento
  */
  public void showEvent (BW_Event prototype) {
     String chave = this.mountKey(prototype);
     Vector eventList = (Vector)UI_Events.get(chave);
     AwarenessEventsWindow eventWindow = new AwarenessEventsWindow(this,prototype,eventList);
  }

   /** atualiza a exibicao do grafico de barras
   */
   public void setBarObjects () {

      Vector evtPointer; 

      Color color[] = new Color[9];
      color[0] = Color.blue;
      color[1] = Color.black;
	color[2] = Color.green;
	color[3] = Color.orange;
	color[4] = Color.cyan;
	color[5] = Color.red;
	color[6] = Color.yellow;
	color[7] = Color.magenta;
	color[8] = Color.lightGray;
      int colorIndex = 0;

      Vector dados = new Vector();
      
	try {
	   Enumeration e1 = UI_Events.elements();
	   while (e1.hasMoreElements()) {
		evtPointer = (Vector) e1.nextElement();
            if (evtPointer.size()>0)
            {
		   bar elementBar = new bar(this,(BW_Event)evtPointer.elementAt(0), String.valueOf(evtPointer.size()),((BW_Event)evtPointer.elementAt(0)).getName(), color[colorIndex],"SOLID");  
		   dados.addElement(elementBar);
		   colorIndex ++;
		}
            
         }
	   if (!isSet) {
	
		sc = new Scroller(this.getSize(),dados);
		sc.setSize(this.getSize());
		this.add(sc);
		repaint();
		isSet=true;
	   }
         else {
            sc.setObjects (dados);
         }
	} catch (NullPointerException npe) {
	       System.out.println("catch" + npe);	}
   }

  
  public void windowClosing (WindowEvent evt){
     this.setVisible(false);
     this.dispose();
  }

  public void windowClosed (WindowEvent evt) {
  }

  public void windowOpened (WindowEvent evt) {
     
  }

  public void windowIconified (WindowEvent evt) {
  }

  public void windowActivated (WindowEvent evt) {
	System.out.println("teste");
  }

  public void windowDeactivated (WindowEvent evt) {
  }

  public void windowDeiconified (WindowEvent evt) {
  }

}
