import java.awt.*;
import java.util.*;
import java.applet.*;
import cnp.ew.spin.*;
import cnp.ew.button.*;
import cnp.ew.layout.*;
import cnp.ew.image.*;
import cnp.ew.misc.*;
import cnp.ew.text.*;
import cnp.ew.lightweight.*;
import cnp.ew.util.*;
import cnp.ew.dialog.*;

public class CpDialogDemo extends CpLcApplet
implements CpObserver, Runnable
{
    static final int msgdlgId =  CpToolkit.registerImageName("msgdlg.gif");
    static final int promptdlgId = CpToolkit.registerImageName("promptdlg.gif");
    static final int okcanceldlgId = CpToolkit.registerImageName("okcanceldlg.gif");
    static final int progressdlgId = CpToolkit.registerImageName("progressdlg.gif");
    static final int listdlgId = CpToolkit.registerImageName("listdlg.gif");

    static final int buttonWidth= 150;
    static final int buttonHeight = 84;
    static final int buttonSpace = 10;
    static final int buttonBorderMargin = 9;

    CpLabeledImageButtonLc msgdlgButton, promptdlgButton, okcanceldlgButton, progressdlgButton, listdlgButton;
    CpReadOnlyTextAreaLc statusText;
    CpPanelLc mainPanel;

    public void init()
    {
        CpAttachmentsLayout layout;
        CpAttachments attachments;

        super.init();

        mainPanel = new CpPanelLc();
        mainPanel.setDrawOffscreen(true);
        mainPanel.setBorderMargin(15);
        mainPanel.setLayout(layout = new CpAttachmentsLayout());
        setLc(mainPanel);


        listdlgButton = new CpLabeledImageButtonLc();
        listdlgButton.setBorderMargin(buttonBorderMargin);
        listdlgButton.setLabel("List Prompter");
        listdlgButton.setImage(
            CpToolkit.getImage(listdlgId),
            CpLabeledImageButtonLc.IMAGE_OUT
        );
        listdlgButton.addObserver(this);
        listdlgButton.setIsTabStop(true);
        attachments = new CpAttachments(0, 0, -1, -1);
        attachments.setWidth(buttonWidth);
        attachments.setHeight(buttonHeight);
        layout.setAttachments(listdlgButton, attachments);
        mainPanel.add(listdlgButton);


        progressdlgButton = new CpLabeledImageButtonLc();
        progressdlgButton.setBorderMargin(buttonBorderMargin);
        progressdlgButton.setLabel("Progress Indicator");
        progressdlgButton.setImage(
            CpToolkit.getImage(progressdlgId),
            CpLabeledImageButtonLc.IMAGE_OUT
        );
        progressdlgButton.addObserver(this);
        progressdlgButton.setIsTabStop(true);
        attachments = new CpAttachments();
        attachments.setLeftAttachment(buttonSpace, listdlgButton);
        attachments.setTopAttachment(0);
        attachments.setWidth(buttonWidth);
        attachments.setHeight(buttonHeight);
        layout.setAttachments(progressdlgButton, attachments);
        mainPanel.add(progressdlgButton);

        promptdlgButton = new CpLabeledImageButtonLc();
        promptdlgButton.setBorderMargin(buttonBorderMargin);
        promptdlgButton.setLabel("Text Prompter");
        promptdlgButton.setImage(
            CpToolkit.getImage(promptdlgId),
            CpLabeledImageButtonLc.IMAGE_OUT
        );
        promptdlgButton.addObserver(this);
        promptdlgButton.setIsTabStop(true);
        attachments = new CpAttachments();
        attachments.setLeftAttachment(buttonSpace, progressdlgButton);
        attachments.setTopAttachment(0);
        attachments.setWidth(buttonWidth);
        attachments.setHeight(buttonHeight);
        layout.setAttachments(promptdlgButton, attachments);
        mainPanel.add(promptdlgButton);



        msgdlgButton = new CpLabeledImageButtonLc();
        msgdlgButton.setBorderMargin(buttonBorderMargin);
        msgdlgButton.setLabel("Message Box");
        msgdlgButton.setImage(
            CpToolkit.getImage(msgdlgId),
            CpLabeledImageButtonLc.IMAGE_OUT
        );
        msgdlgButton.addObserver(this);
        msgdlgButton.setIsTabStop(true);
        attachments = new CpAttachments();
        attachments.setTopAttachment(buttonSpace, progressdlgButton);
        attachments.setLeftAttachment((buttonWidth + buttonSpace)/2);
        attachments.setWidth(buttonWidth);
        attachments.setHeight(buttonHeight);
        layout.setAttachments(msgdlgButton, attachments);
        mainPanel.add(msgdlgButton);


        okcanceldlgButton = new CpLabeledImageButtonLc();
        okcanceldlgButton.setBorderMargin(buttonBorderMargin);
        okcanceldlgButton.setLabel("OK/Cancel Prompter");
        okcanceldlgButton.setImage(
            CpToolkit.getImage(okcanceldlgId),
            CpLabeledImageButtonLc.IMAGE_OUT
        );
        okcanceldlgButton.addObserver(this);
        okcanceldlgButton.setIsTabStop(true);
        attachments = new CpAttachments();
        attachments.setTopAttachment(buttonSpace, progressdlgButton);
        attachments.setLeftAttachment(buttonSpace, msgdlgButton);
        attachments.setWidth(buttonWidth);
        attachments.setHeight(buttonHeight);
        layout.setAttachments(okcanceldlgButton, attachments);
        mainPanel.add(okcanceldlgButton);


        statusText = new CpReadOnlyTextAreaLc();
        statusText.setBorderStyle(CpAbstractLc.BORDER_LIGHTINWARD3D);
        statusText.setBorderMargin(1);
        statusText.setFont(CpFonts.dialogPlainTen());
        attachments = new CpAttachments();
        attachments.setTopAttachment(10, okcanceldlgButton);
        attachments.setLeftAttachment(0);
        attachments.setWidth(3 * buttonWidth + 2 * buttonSpace);
        layout.setAttachments(statusText, attachments);
        mainPanel.add(statusText);


        setBackground(Color.lightGray);
        layout();
    }

    CpListChooserDialog listChooser;
    CpMessageDialog okcanceldlg;
    CpPrompterDialog prompter;

    public void update(CpObservable o, int facet, Object arg)
    {
	    if (o == msgdlgButton) {
	        if (facet == CpEvent.BUTTON_CLICKED) {
        	    CpMessageDialog dlg = new CpMessageDialog(
                    getFrame(),
                    "Please Note",
                    "This messagebox will automatically size itself to accomodate the size of the text that's placed within it.  This makes for a generally reusable component.",
                    CpMessageDialog.STYLE_OK);
            	 dlg.show();
	        }
	    }

	    if (o == okcanceldlgButton) {
	        if (facet == CpEvent.BUTTON_CLICKED) {
            	okcanceldlg = new CpMessageDialog(
    	            getFrame(),
    	            "Please Confirm",
    	            "Your system must be restarted before this very important document you've written can be saved.\n .        \nRestart?",
    	            CpMessageDialog.STYLE_OKCANCEL);
    	        okcanceldlg.show();
    	    }
	    }

	   if (o == progressdlgButton) {
	        if (facet == CpEvent.BUTTON_CLICKED) {
    	        Thread thread = new Thread(this);
    	        thread.start();
    	    }
	    }

	    if (o == promptdlgButton) {
	        if (facet == CpEvent.BUTTON_CLICKED) {
            	prompter = new CpPrompterDialog(getFrame(), "Please Enter A String", "How many software developers does it take to put in a lightbulb?", "None.  That's a hardware problem.");
    	        prompter.addObserver(this);
    	        prompter.show();
    	    }
	    }


	    if (o == listdlgButton) {
	        if (facet == CpEvent.BUTTON_CLICKED) {
        	    Vector items = new Vector();
                items.addElement("Object Oriented");
                items.addElement("Network-savvy");
                items.addElement("Interpreted");
                items.addElement("Robust");
                items.addElement("Secure");
                items.addElement("Architecture Neutral");
                items.addElement("Portable");
                items.addElement("High-performance");
                items.addElement("Multithreaded");
                items.addElement("Dynamic");
    	        listChooser = new CpListChooserDialog(getFrame(), "Please Select An Item", "Please select your favorite buzzword for describing Java.", items);
    	        listChooser.addObserver(this);
    	        listChooser.show();
    	    }
	    }

	    if (facet == OBJECT_CHANGED) {
	        if (o == prompter) {
	            statusText.setText("Your answer was " + arg + ". Very inventive.");
	        }

	        if (o == listChooser) {
	            statusText.setText("Your favorite is " + arg + ".  That's ours too!");
	        }

	        if (o == okcanceldlg) {
	            if (arg.equals("OK")) {
	                statusText.setText("Don't worry; this will only take a few minutes.");
	            } else {
	                statusText.setText("Document purged.");
	            }
	        }
    	}
	}

	public void run()
	{
	    CpProgressDialog gaugeDialog = new CpProgressDialog(getFrame(), "Please Wait", "Your computer is in the process of some very important calculations.  This may take a few minutes.", 0, 100, 0);
	    gaugeDialog.show();

	    for (int i=0; i <= 100; i += 5) {
	        gaugeDialog.setValue(i);
	        try {
	            Thread.sleep(200);
	        } catch (InterruptedException e) {
	        }
	    }
        gaugeDialog.closeWindow();
	}
}

