import java.awt.*;
import java.io.*;
import java.util.*;
import java.applet.*;
import cnp.ew.util.*;
import cnp.ew.layout.*;
import cnp.ew.misc.*;
import cnp.ew.lightweight.*;
import cnp.ew.button.*;
import cnp.ew.text.*;
import cnp.ew.list.*;
import cnp.ew.image.*;

/**
 * CpDemoLauncher: provides a simple Applet-Viewer like application
 * that can launch our applets.  To add applets, do the following:
 *   - add the title to either the static array applicationTitles or widgetTitles
 *   - add the class name as a string (without .class) to the classNames static array
 * Note that order is important; the title arrays and the className array have to match up.
 *
 * @version        $Version$
 * @author         $Author: Ken Cooper$
 */
public class CpDemoLauncher extends Frame
{
    Label label;
    Applet currentApplet;
    int currentAppletIndex;

    static String[] applicationTitles = {
        "Word Processor",
        "Spreadsheet",
        "Charting",
        "Graphics Editor",
        "Java Editor"
    };


    static String[] widgetTitles = {
        "Bordered Panels",
        "Buttons",
        "Gauges",
        "Spin Buttons",
        "Formatted Entryfields",
        "Calendar",
        "Customizable Entryfields",
        "Notebooks",
        "Sliders",
        "Dialogs",
        "Lists",
        "Attachments",
        "Split Bars"
    };

    static String[] classNames = {
        "CpWordProcessorDemo",
        "CpSpreadsheetDemo",
        "CpChartDemo",
        "CpDiagramDemo",
        "CpJavaEditor",


        "CpBorderedPanelDemo",
        "CpButtonDemo",
        "CpGaugeDemo",
        "CpSpinButtonDemo",
        "CpPictureFieldDemo",
        "CpCalendarDemo",
        "CpEntryFieldDemo",
        "CpNotebookDemo",
        "CpSliderDemo",
        "CpDialogDemo",
        "CpListDemo",
        "CpAttachmentsDemo",
        "CpSplitbarDemo"
    };

    CheckboxMenuItem [] menuItems;

    public CpDemoLauncher()
    {
        setTitle("Cooper & Peters Demo Applet Viewer");
        CpImageGetter.getDefault().isApplet = false;
        CpToolkit.setDefaultComponent(this);

        menuItems = new CheckboxMenuItem[applicationTitles.length + widgetTitles.length];

        MenuBar menuBar = new MenuBar();
        Menu menu = new Menu("Applications");
        for (int i=0; i < applicationTitles.length; i++) {
            menuItems[i] = new CheckboxMenuItem(applicationTitles[i]);
            menu.add(menuItems[i]);
        }
        menuBar.add(menu);

        menu = new Menu("Components");
        for (int i=0; i < widgetTitles.length; i++) {
            menuItems[i + applicationTitles.length] = new CheckboxMenuItem(widgetTitles[i]);
            menu.add(menuItems[i + applicationTitles.length]);
        }
        menuBar.add(menu);
        setMenuBar(menuBar);

        label = new Label();
        add(label);

        showNewApplet(0);
        show();
    }

    public void layout()
    {
        Insets insets = insets();
        int innerWidth = size().width - insets.left - insets.right;
        int innerHeight = size().height - insets.top - insets.bottom;
        int labelHeight = label.preferredSize().height;
        label.reshape(insets.left, size().height - labelHeight - insets.bottom, innerWidth, labelHeight);

        if (currentApplet != null) {
            currentApplet.reshape(insets.left, insets.top, innerWidth, innerHeight - labelHeight);
        }
    }



	public boolean handleEvent (Event evt) {

		switch(evt.id) {
		case Event.WINDOW_DESTROY:
	        if (currentApplet != null) {
	            currentApplet.stop();
	        }
			System.exit(0);
	    case Event.ACTION_EVENT:
	        if (evt.target instanceof CheckboxMenuItem) {
                showNewApplet(indexForMenuItem((CheckboxMenuItem)evt.target));
            }
	        break;
		}
		return super.handleEvent(evt);
	}

    int indexForMenuItem(CheckboxMenuItem menuItem)
    {
        for (int i = 0; i < menuItems.length; i++) {
            if (menuItem == menuItems[i]) {
                return i;
            }
        }
        return -1;
    }

	void showNewApplet(int index)
	{
	    if (currentApplet != null) {
	        remove(currentApplet);
	        currentApplet.hide();
	        currentApplet.stop();
	        menuItems[currentAppletIndex].setState(false);
	    }

	    try {
	        currentApplet = (Applet)Class.forName(classNames[index]).newInstance();
	    } catch (ClassNotFoundException e) {  System.out.println(e); return; }
	    catch (InstantiationException e) { System.out.println(e); return; }
	    catch (IllegalAccessException e) { System.out.println(e); return; }

        currentAppletIndex = index;
        menuItems[currentAppletIndex].setState(true);

        add(currentApplet);
        label.setText("Initializing...");

        resizeByOnePixel();

        currentApplet.init();
        currentApplet.start();

        // TBD: need to put this in the framework; damn AWT doesn't send us a gotFocus the first time...
        //((CpLcApplet)currentApplet).getRootPanel().gotFocus(new Event(null, 0, null), null);

        label.setText("Running...");
    }


    /**
     * TBD: This is a complete hack, and needs to be looked into.  Size() is not answering the correct
     * dimension unless we explicitly resize the frame, and this causes each applet to fail. To get
     * around it, we size up and down by one pixel when they switch applets. Ick.
     */
    int sizeToggle = 0;

    void resizeByOnePixel()
    {
        int curHeight = size().height;
        if (curHeight == 0) {
            curHeight = 480;
        }
        resize(640, curHeight + sizeToggle);
        if (sizeToggle > 0) {
            sizeToggle = -1;
        } else {
            sizeToggle = 1;
        }
    }


	public static void main (String argv[]) {
		new CpDemoLauncher();
	}
}



