/*
	This simple extension of the java.awt.Frame class
	contains all the elements necessary to act as the
	main window of an application.
 */

import java.awt.*;

public class MainFrame extends Frame
{
	public MainFrame()
	{
		// This code is automatically generated by Visual Cafe when you add
		// components to the visual environment. It instantiates and initializes
		// the components. To modify the code, only use code syntax that matches
		// what Visual Cafe can generate, or Visual Cafe may be unable to back
		// parse your Java file into its visual environment.
		
		//{{INIT_CONTROLS
		setLayout(null);
		setSize(900,480);
		setVisible(false);
		dataPanel.setLayout(null);
		add(dataPanel);
		dataPanel.setBackground(java.awt.Color.lightGray);
		dataPanel.setBounds(200,0,700,480);
		label1.setText("?");
		dataPanel.add(label1);
		label1.setBounds(328,216,58,63);
		button1.setLabel("add Frame 1");
		add(button1);
		button1.setBackground(java.awt.Color.lightGray);
		button1.setBounds(24,24,108,24);
		button2.setLabel("add Frame 2");
		add(button2);
		button2.setBackground(java.awt.Color.lightGray);
		button2.setBounds(24,72,108,24);
		setTitle("AWT Application");
		//}}
		
		//{{INIT_MENUS
		//}}
		
		//{{REGISTER_LISTENERS
		SymWindow aSymWindow = new SymWindow();
		this.addWindowListener(aSymWindow);
		SymAction lSymAction = new SymAction();
		button1.addActionListener(lSymAction);
		button2.addActionListener(lSymAction);
		//}}
	}
	
	public MainFrame(String title)
	{
		this();
		setTitle(title);
	}
	
    /**
     * Shows or hides the component depending on the boolean flag b.
     * @param b  if true, show the component; otherwise, hide the component.
     * @see java.awt.Component#isVisible
     */
    public void setVisible(boolean b)
	{
		if(b)
		{
			setLocation(50, 50);
		}	
		super.setVisible(b);
	}
	
	static public void main(String args[])
	{
		try
		{
			//Create a new instance of our application's frame, and make it visible.
    		(new MainFrame()).setVisible(true);
		}
		catch (Throwable t)
		{
			System.err.println(t);
			t.printStackTrace();
			//Ensure the application exits with an error condition.
			System.exit(1);
		}
	}
	
	public void addNotify()
	{
		// Record the size of the window prior to calling parents addNotify.
		Dimension d = getSize();
		
		super.addNotify();
	
		if (fComponentsAdjusted)
			return;
	
		// Adjust components according to the insets
		setSize(getInsets().left + getInsets().right + d.width, getInsets().top + getInsets().bottom + d.height);
		Component components[] = getComponents();
		for (int i = 0; i < components.length; i++)
		{
			Point p = components[i].getLocation();
			p.translate(getInsets().left, getInsets().top);
			components[i].setLocation(p);
		}
		fComponentsAdjusted = true;
	}
	
	// Used for addNotify check.
	boolean fComponentsAdjusted = false;
	
	//{{DECLARE_CONTROLS
	java.awt.Panel dataPanel = new java.awt.Panel();
	java.awt.Label label1 = new java.awt.Label();
	java.awt.Button button1 = new java.awt.Button();
	java.awt.Button button2 = new java.awt.Button();
	//}}
	
	//{{DECLARE_MENUS
	//}}
	
	class SymWindow extends java.awt.event.WindowAdapter
	{
		public void windowClosing(java.awt.event.WindowEvent event)
		{
			Object object = event.getSource();
			if (object == MainFrame.this)
				MainFrame_WindowClosing(event);
		}
	}
	
	void MainFrame_WindowClosing(java.awt.event.WindowEvent event)
	{
		// to do: code goes here.
			 
		MainFrame_WindowClosing_Interaction1(event);
	}


	void MainFrame_WindowClosing_Interaction1(java.awt.event.WindowEvent event)
	{
		try {
	        setVisible(false);    // Hide the invoking frame
	        dispose();            // Free system resources
		    System.exit(0);       // close the application
		} catch (Exception e) {
		}
	}

	
	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
			Object object = event.getSource();
			if (object == button1)
				button1_ActionPerformed(event);
			else if (object == button2)
				button2_ActionPerformed(event);
			
		}
	}


	void button1_ActionPerformed(java.awt.event.ActionEvent event)
	{
	    try
	    {
            Panel1 pnl = new Panel1();
		    dataPanel.removeAll();
		    dataPanel.add( pnl );
		    dataPanel.validate();
		}
		catch( Exception ex )
		{
		    System.out.println( "ERROR: " + ex );
		}
	}

	void button2_ActionPerformed(java.awt.event.ActionEvent event)
	{
		Panel2 pnl = new Panel2();
		dataPanel.removeAll();
		dataPanel.add( pnl );
	    dataPanel.validate();
	}
}

