package espial.demo.phoneapp;

import espial.awt.*;
import espial.application.*;

import java.awt.*;
import espial.awt.borders.*;
import espial.awt.item.*;
import espial.awt.layout.*;
import java.awt.event.*;
import espial.image.*;

/**
 * THis is the Phone Application Launcher UI.<p>
 * Most of the UI code was generated using Kalos Architect which currently
 * supports version 2.2 of Kalos Espresso.  This UI is built using
 * version 3.0 of Kalos Espresso which introduces many more features.
 */
public class PhoneApp extends EPanel
	implements ItemListener, App {
	
	final static String APP_EMAIL = "email";
	final static String APP_DIR = "directory";
	final static String APP_SCRATCH = "scratch pad";
	
  EToolStickyButton m_email;
  EToolStickyButton m_directory;
  EToolStickyButton m_scratch;
	
  EPanel m_appSpace;
	CardLayout m_appCard;
	
	AppContext m_appcontext;
	
	/** 
	 * The UI is built here because we need the AppContext to load
	 * and run images. 
	 */
	public void init (AppContext appc) {
		m_appcontext = appc;
    buildUI ();
    userInitialization ();
    eventRegistration ();
	}
	
	/**
	 * This method is called to switch between the cards in the app launcher.
	 */
	private void switchCard (String card) {
		m_appCard.show (m_appSpace, card);
		m_appSpace.invalidate ();
		m_appSpace.validate ();
	}
	
  ///// kaBeginUserMethod: m_scratch_itemStateChanged
	/**
	 * This method is called when the user selects the scratch pad app.
	 * <p>TBD: Implementation of the scratch pad app.
	 */
  void m_scratch_itemStateChanged (ItemEvent e) {
    int evState = e.getStateChange ();
		
    if (evState == ItemEvent.SELECTED) {
			switchCard (APP_SCRATCH);
    }
  }
  ///// kaEndUserMethod: m_scratch_itemStateChanged
	
  ///// kaBeginUserMethod: userInitialization
  void userInitialization () {
    // TODO: Please initialize the members here.
    //       Do not modify the buildUI() method.
    //       Event listeners are registered after this method is called.
		
		PhoneDirApp pda = new PhoneDirApp ();
		pda.init (m_appcontext);
		
		m_appSpace.add (pda, APP_DIR);
		m_appSpace.add (new EPanel (), APP_EMAIL);
		m_appSpace.add (new EPanel (), APP_SCRATCH);
		switchCard (APP_EMAIL);
  }
  ///// kaEndUserMethod: userInitialization
	
  ///// kaBeginUserMethod: m_directory_itemStateChanged
	/**
	 * This method is called when the user selects the directory app.
	 */
  void m_directory_itemStateChanged (ItemEvent e) {
    int evState = e.getStateChange ();
		
    if (evState == ItemEvent.SELECTED) {
			switchCard (APP_DIR);
    }
  }
  ///// kaEndUserMethod: m_directory_itemStateChanged
	
  ///// kaBeginUserMethod: m_email_itemStateChanged
	/**
	 * This method is called when the user selects the email app.
	 * <p>TBD: Implementation of the email app.
	 */
  void m_email_itemStateChanged (ItemEvent e) {
    int evState = e.getStateChange ();
		
    if (evState == ItemEvent.SELECTED) {
			switchCard (APP_EMAIL);
    }
  }
  ///// kaEndUserMethod: m_email_itemStateChanged
	
  ///// kaBeginMethod: eventRegistration
  void eventRegistration () {
    m_email.addItemListener (this);
    m_directory.addItemListener (this);
    m_scratch.addItemListener (this);
  }
  ///// kaEndMethod: eventRegistration
	
  ///// kaBeginMethod: PhoneApp
  public PhoneApp() {
  }
  ///// kaEndMethod: PhoneApp
	
  ///// kaBeginMethod: buildUI
  void buildUI () {
		
    // setting up BorderLayout
    BorderLayout layout_this;
    layout_this = new BorderLayout (0, 0);
    this.setLayout (layout_this);
		
    // setting up Container
    EPanel container_1;
    container_1 = new EPanel ();
		
    // setting up GroupBorder
    GroupBorder border_container_1;
    border_container_1 = new GroupBorder (new Color (109,185,212), null);
    border_container_1.setInnerInsets (0, 5, 0, 5);
    border_container_1.setOuterInsets (0, 0, 0, 0);
    border_container_1.setSides (false, false, false, true);
    container_1.setBorder (border_container_1);
		
    // setting up VFlowLayout
    VFlowLayout layout_container_1;
    layout_container_1 = new VFlowLayout (0, 0, 5);
    container_1.setLayout (layout_container_1);
		
		EBooleanGroup bg = new EBooleanGroup ();
		
    // setting up Sticky Button
    m_email = new EToolStickyButton ();
		m_email.setBooleanGroup (bg);
		m_email.setState (true);
    m_email.setImagePosition (EToolStickyButton.TOP);
    m_email.setImageBundle (m_appcontext.getImageBundledResource (getClass(), "email.gif"));
    // ending setup of m_email
		
    container_1.add (m_email);
		
    // setting up Sticky Button
    m_directory = new EToolStickyButton ();
		m_directory.setBooleanGroup (bg);
		m_directory.setState (false);
    m_directory.setImagePosition (EToolStickyButton.TOP);
    m_directory.setImageBundle (m_appcontext.getImageBundledResource (getClass(), "directory.gif"));
    // ending setup of m_directory
		
    container_1.add (m_directory);
		
    // setting up Sticky Button
    m_scratch = new EToolStickyButton ();
		m_scratch.setBooleanGroup (bg);
		m_scratch.setState (false);
    m_scratch.setImagePosition (EToolStickyButton.TOP);
    m_scratch.setImageBundle (m_appcontext.getImageBundledResource (getClass(), "scratchpad.gif"));
    // ending setup of m_scratch
		
    container_1.add (m_scratch);
    // ending setup of container_1
		
    this.add (container_1, BorderLayout.WEST);
		
    // setting up Container
    m_appSpace = new EPanel ();
    m_appSpace.setLayout (m_appCard = new CardLayout ());
    // ending setup of m_appSpace
		
    this.add (m_appSpace, BorderLayout.CENTER);
  }
  ///// kaEndMethod: buildUI
	
  ///// kaBeginMethod: itemStateChanged
  public void itemStateChanged (ItemEvent e) {
    Object src = e.getSource();
		
    // Sticky Button
    if (src == m_directory) {
      m_directory_itemStateChanged (e);
    }
    // Sticky Button
    else if (src == m_scratch) {
      m_scratch_itemStateChanged (e);
    }
    // Sticky Button
    else if (src == m_email) {
      m_email_itemStateChanged (e);
    }
  }
  ///// kaEndMethod: itemStateChanged
	
  ///// kaBeginMethod: main
  public static void main (String [] args) {
    Frame f = new Frame ("Test");
		
		Window w;
		
		try {
			w = new Window (f);
		}
		catch (Exception x) {
			w = f;
			f.setIconImage (ImageUtils.getImageResource (PhoneApp.class, "eg16icon.gif"));
		}
		
		w.setSize (320, 240);
		
    EContainer p;
		DoubleBufferPanel dbp;
		
		ERootPanel root = new ERootPanel ();
		p = root.getContentLayer ();
		
    w.setLayout (new GridLayout (1,1));
    w.add (dbp = new DoubleBufferPanel ());
		
    ImageUtils.assignTrackingComponent (root);
		
    dbp.setLayout (new GridLayout (1, 1));
		dbp.add (root);
		
		PhoneApp pa = new PhoneApp ();
		pa.init (new espial.test.DevAppContext ());
		
		p.setLayout (new GridLayout (1, 1));
    p.add (pa);
		
		w.invalidate ();
		w.validate ();
		w.show ();
		
  }
  ///// kaEndMethod: main
	
}
