import java.awt.*;
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.list.*;

public class CpButtonDemo extends CpLcApplet
implements CpObserver
{
    static final int buttonImage1UpId =  CpToolkit.registerImageName("th_up.gif");
    static final int buttonImage1DownId = CpToolkit.registerImageName("th_dn.gif");
    static final int buttonImage2UpId = CpToolkit.registerImageName("spot.gif");
    static final int buttonImage2DownId = CpToolkit.registerImageName("spot2.gif");
    static final int buttonImage3UpId = CpToolkit.registerImageName("mbox-emp.gif");
    static final int buttonImage3DownId = CpToolkit.registerImageName("mbox-ful.gif");

    CpCheckboxLc disabledCheckbox, borderedCheckbox;
    CpDropDownListLc choice;
    CpLabeledImageButtonLc labeledImageButton, labeledButton, imageButton;
    CpLabeledImageTwoStateButtonLc twoStateButton;
    CpReadOnlyTextAreaLc statusText;
    CpPanelLc mainPanel;

    public void init()
    {
        CpPanelLc panel1, panel2, panel3;
        CpAttachmentsLayout layout, mainLayout;
        CpAttachments attachments;
        CpReadOnlyTextAreaLc orientationLabel;

        super.init();

        mainPanel = new CpPanelLc();
        mainPanel.setDrawOffscreen(true);
        mainPanel.setLayout(mainLayout = new CpAttachmentsLayout());
        setLc(mainPanel);

        panel1 = new CpPanelLc(CpAbstractLc.BORDER_LIGHTINWARD3D);
        attachments = new CpAttachments(10, 10, 10, -1);
        attachments.setHeight(35);
        mainLayout.setAttachments(panel1, attachments);
        mainPanel.add(panel1);
        panel1.setLayout(layout = new CpAttachmentsLayout());

        orientationLabel = new CpReadOnlyTextAreaLc("Orientation");
        attachments = new CpAttachments(30, -1, -1, -1);
        attachments.setVerticalCentering();
        layout.setAttachments(orientationLabel, attachments);
        panel1.add(orientationLabel);

        choice = new CpDropDownListLc();
        choice.addItem("Image On Right");
        choice.addItem("Image On Left");
        choice.addItem("Image On Bottom");
        choice.addItem("Image On Top");
        choice.select(3);
        choice.addObserver(this);
        choice.setIsTabStop(true);
        attachments = new CpAttachments(-1, -1, -1, -1);
        attachments.setLeftAttachment(5, orientationLabel);
        attachments.setVerticalCentering();
        layout.setAttachments(choice, attachments);
        panel1.add(choice);


        disabledCheckbox = new CpCheckboxLc("Disabled");
        disabledCheckbox.setBackground(Color.lightGray);
        attachments = new CpAttachments(-1, -1, -1, -1);
        attachments.setLeftAttachment(30, choice);
        attachments.setVerticalCentering();
        disabledCheckbox.setIsTabStop(true);
        disabledCheckbox.addObserver(this);
        layout.setAttachments(disabledCheckbox, attachments);
        panel1.add(disabledCheckbox);


        borderedCheckbox = new CpCheckboxLc("Bordered");
        borderedCheckbox.setBackground(Color.lightGray);
        borderedCheckbox.setState(true);
        attachments = new CpAttachments(-1, -1, -1, -1);
        attachments.setLeftAttachment(30, disabledCheckbox);
        attachments.setVerticalCentering();
        borderedCheckbox.setIsTabStop(true);
        borderedCheckbox.addObserver(this);
        layout.setAttachments(borderedCheckbox, attachments);
        panel1.add(borderedCheckbox);


        statusText = new CpReadOnlyTextAreaLc();

        panel2 = new CpPanelLc(CpAbstractLc.BORDER_LIGHTINWARD3D);
        attachments = new CpAttachments(10, -1, 10, -1);
        attachments.setTopAttachment(10, panel1);
        attachments.setBottomAttachment(10, statusText);
        mainLayout.setAttachments(panel2, attachments);
        mainPanel.add(panel2);

        panel2.setLayout(layout = new CpAttachmentsLayout());

        labeledImageButton = new CpLabeledImageButtonLc();
        labeledImageButton.setBorderMargin(5);
        labeledImageButton.setLabel("Labeled Button");
        labeledImageButton.setIsTabStop(true);
        labeledImageButton.setImage(
            CpToolkit.getImage(buttonImage1UpId),
            CpLabeledImageButtonLc.IMAGE_OUT
        );
        labeledImageButton.setImage(
            CpToolkit.getImage(buttonImage1DownId),
            CpLabeledImageButtonLc.IMAGE_IN
        );
        panel2.add(labeledImageButton);
        labeledImageButton.addObserver(this);
        attachments = new CpAttachments();
        attachments.setLeftAttachment(20);
        attachments.setVerticalCentering();
        attachments.setWidth(150);
        layout.setAttachments(labeledImageButton, attachments);

        labeledButton = new CpLabeledImageButtonLc();
        labeledButton.setBorderMargin(5);
        labeledButton.setLabel("Multiple Line\nTitle");
        labeledButton.setIsTabStop(true);
        panel2.add(labeledButton);
        labeledButton.addObserver(this);
        attachments = new CpAttachments();
        attachments.setLeftAttachment(20, labeledImageButton);
        attachments.setVerticalCentering();
        layout.setAttachments(labeledButton, attachments);

        imageButton = new CpLabeledImageButtonLc();
        imageButton.setBorderMargin(5);
        imageButton.setIsTabStop(true);
        imageButton.setImage(
            CpToolkit.getImage(buttonImage2UpId),
            CpLabeledImageButtonLc.IMAGE_OUT
        );
        imageButton.setImage(
            CpToolkit.getImage(buttonImage2DownId),
            CpLabeledImageButtonLc.IMAGE_IN
        );
        panel2.add(imageButton);
        imageButton.addObserver(this);
        attachments = new CpAttachments();
        attachments.setLeftAttachment(CpAttachments.ATTACH_SIBLING, 20);
        attachments.setLeftSibling(labeledButton);
        attachments.setVerticalCentering();
        layout.setAttachments(imageButton, attachments);

        twoStateButton = new CpLabeledImageTwoStateButtonLc();
        twoStateButton.setBorderMargin(5);
        twoStateButton.setIsTabStop(true);
        twoStateButton.setLabel("Two State");
        twoStateButton.setHasBorder(true);
        twoStateButton.setImage(
            CpToolkit.getImage(buttonImage3UpId),
            CpLabeledImageTwoStateButtonLc.IMAGE_TRUE
        );
        twoStateButton.setImage(
            CpToolkit.getImage(buttonImage3DownId),
            CpLabeledImageTwoStateButtonLc.IMAGE_FALSE
        );

        twoStateButton.setDepressedWhenOn(true);
        panel2.add(twoStateButton);
        twoStateButton.addObserver(this);
        attachments = new CpAttachments();
        attachments.setLeftAttachment(CpAttachments.ATTACH_SIBLING, 20);
        attachments.setLeftSibling(imageButton);
        attachments.setWidth(115);
        attachments.setVerticalCentering();
        layout.setAttachments(twoStateButton, attachments);


        statusText.setFont(CpFonts.dialogPlainEight());
        statusText.setText("No Button Pressed");
        statusText.setBorderStyle(CpAbstractLc.BORDER_LIGHTINWARD3D);
        statusText.setBorderMargin(2);
        attachments = new CpAttachments(10, -1, 10, 10);
        mainLayout.setAttachments(statusText, attachments);
        mainPanel.add(statusText);

        setBackground(Color.lightGray);
        layout();
    }


    String currentLine="";

	void setStatus(String buttonTitle, int eventId)
	{
	    switch (eventId) {
	    case CpEvent.BUTTON_PRESS:
	        currentLine = buttonTitle + " button pressing";
	        break;
	    case CpEvent.BUTTON_PRESSING:
	        currentLine += ".";
	        break;
	    case CpEvent.BUTTON_RELEASE:
	        currentLine += " released";
	        break;
	    case CpEvent.BUTTON_CLICKED:
	        currentLine += " inside button";
	        break;
	    default:
	        return;
	    }
	    statusText.setText(currentLine);

	}


    public boolean handleEvent(Event e)
    {
	    if (e.target == disabledCheckbox && e.id == Event.ACTION_EVENT) {
	        mainPanel.enableDamageRepair(false);
	        labeledImageButton.setIsDisabled(disabledCheckbox.getState());
	        labeledButton.setIsDisabled(disabledCheckbox.getState());
	        imageButton.setIsDisabled(disabledCheckbox.getState());
	        twoStateButton.setIsDisabled(disabledCheckbox.getState());
	        mainPanel.enableDamageRepair(true);
	        mainPanel.repairDamage();
	    }
	    if (e.target == borderedCheckbox && e.id == Event.ACTION_EVENT) {
	        mainPanel.enableDamageRepair(false);
	        labeledImageButton.setHasBorder(borderedCheckbox.getState());
	        labeledButton.setHasBorder(borderedCheckbox.getState());
	        imageButton.setHasBorder(borderedCheckbox.getState());
	        twoStateButton.setHasBorder(borderedCheckbox.getState());
	        mainPanel.enableDamageRepair(true);
	        mainPanel.repairDamage();
	    }

	    if (e.target == choice && e.id == Event.ACTION_EVENT ) {
	        mainPanel.enableDamageRepair(false);
            labeledImageButton.setLabelOrientation(choice.getSelectedIndex());
            twoStateButton.setLabelOrientation(choice.getSelectedIndex());
	        mainPanel.enableDamageRepair(true);
	        mainPanel.repairDamage();
	    }
	    return super.handleEvent(e);
	}

    public void update(CpObservable o, int facet, Object arg)
    {
	    if (o == disabledCheckbox && facet == OBJECT_CHANGED) {
	        mainPanel.enableDamageRepair(false);
	        labeledImageButton.setIsDisabled(disabledCheckbox.getState());
	        labeledButton.setIsDisabled(disabledCheckbox.getState());
	        imageButton.setIsDisabled(disabledCheckbox.getState());
	        twoStateButton.setIsDisabled(disabledCheckbox.getState());
	        mainPanel.enableDamageRepair(true);
	        mainPanel.repairDamage();
	    }
	    if (o == borderedCheckbox && facet == OBJECT_CHANGED) {
	        mainPanel.enableDamageRepair(false);
	        labeledImageButton.setHasBorder(borderedCheckbox.getState());
	        labeledButton.setHasBorder(borderedCheckbox.getState());
	        imageButton.setHasBorder(borderedCheckbox.getState());
	        twoStateButton.setHasBorder(borderedCheckbox.getState());
	        mainPanel.enableDamageRepair(true);
	        mainPanel.repairDamage();
	    }

	    if (o == choice && facet == OBJECT_CHANGED) {
	        mainPanel.enableDamageRepair(false);
            labeledImageButton.setLabelOrientation(choice.getSelectedIndex());
            twoStateButton.setLabelOrientation(choice.getSelectedIndex());
	        mainPanel.enableDamageRepair(true);
	        mainPanel.repairDamage();
	    }

	    if (o == labeledImageButton) {
	        setStatus("Thumbs Up", facet);
	    }

	    if (o == labeledButton) {
	        setStatus("Multi line Title", facet);
	    }

	    if (o == imageButton) {
	        setStatus("Mr. Spot", facet);
	    }

	    if (o == twoStateButton) {
	        setStatus("Mailbox", facet);
	    }
	}
}

