import java.awt.*;
import java.applet.*;
import java.util.*;

import cnp.ew.image.*;
import cnp.ew.button.*;
import cnp.ew.util.*;
import cnp.ew.lightweight.*;
import cnp.ew.misc.*;
import cnp.ew.tdemo.*;
import cnp.ew.text.*;
import cnp.ew.properties.*;
import cnp.ew.grid.*;
import cnp.ew.layout.*;
import cnp.ew.displayer.*;
import cnp.ew.list.*;
import cnp.ew.diagram.*;
import cnp.ew.dialog.*;
import cnp.ew.kdemo.*;
import cnp.ew.richtext.*;

public class CpSpreadsheetDemo extends CpLcApplet implements CpObserver
{
    CpWpPanelLc toolbar, formatbar, typebar;
    CpReadOnlyTextAreaLc statusText;

    CpDropDownListLc fontCombo, scaleCombo, sizeCombo, formatCombo;

    CpGridLc gridLc;
    CpGridPane gridpane;

    CpToolBarButtonLc insertLcButton, newButton, openButton, saveButton, printButton, previewButton, cutButton, copyButton, pasteButton, undoButton;
    CpToolBarButtonLc sumButton, functionButton, sortupButton, sortdownButton;
    CpToolBarTwoStateButtonLc showGridButton, showHeadersButton, boldButton, italicButton, leftButton, rightButton, centerHButton, topButton, bottomButton, centerVButton, wrapButton, borderButton;

    CpToolBarTwoStateButtonLc forecolorButton, backcolorButton, colorButton;
    CpColorPalette pal, colorPal;
    CpGridPalette borderPal, borderPalette;
    static int[] borderImageIds;

    CpListChooserDialog lcList;

    boolean dontPopup = false;


    static int newId;
    static int openId;
    static int saveId;
    static int printId;
    static int previewId;
    static int cutId;
    static int copyId;
    static int pasteId;
    static int undoId;
    static int sumId;
    static int functionId;
    static int sortupId;
    static int sortdownId;
    static int showGridId;
    static int showHeadersId;
    static int insertLcId;
    static int wrapId;
    static int boldId;
    static int italicId;
    static int underlineId;
    static int leftId;
    static int rightId;
    static int centerhId;
    static int topId;
    static int bottomId;
    static int centervId;
    static int forecolorId;
    static int backcolorId;
    static int borderId;

    static int ptBarnumId;

    static {
         newId = CpToolkit.registerImageName("wpnew.gif");
         openId = CpToolkit.registerImageName("wpopen.gif");
         saveId = CpToolkit.registerImageName("wpsave.gif");
         printId = CpToolkit.registerImageName("wpprint.gif");
         previewId = CpToolkit.registerImageName("wppreview.gif");
         cutId = CpToolkit.registerImageName("wpcut.gif");
         copyId = CpToolkit.registerImageName("wpcopy.gif");
         pasteId = CpToolkit.registerImageName("wppaste.gif");
         undoId = CpToolkit.registerImageName("wpundo.gif");
         boldId = CpToolkit.registerImageName("wpbold.gif");
         italicId = CpToolkit.registerImageName("wpitalic.gif");
         underlineId = CpToolkit.registerImageName("wpunderline.gif");
         leftId = CpToolkit.registerImageName("wpleft.gif");
         rightId = CpToolkit.registerImageName("wpright.gif");
         centerhId = CpToolkit.registerImageName("wpcenter.gif");
         topId = CpToolkit.registerImageName("sstop.gif");
         bottomId = CpToolkit.registerImageName("ssbottom.gif");
         centervId = CpToolkit.registerImageName("sscenterv.gif");
         forecolorId = CpToolkit.registerImageName("ssforecolor.gif");
         backcolorId = CpToolkit.registerImageName("ssbackcolor.gif");
         borderId = CpToolkit.registerImageName("ssborder.gif");
         wrapId = CpToolkit.registerImageName("sswrap.gif");
         sumId = CpToolkit.registerImageName("sssum.gif");
         functionId = CpToolkit.registerImageName("ssfunction.gif");
         sortupId = CpToolkit.registerImageName("sssortup.gif");
         sortdownId = CpToolkit.registerImageName("sssortdown.gif");
         showGridId = CpToolkit.registerImageName("ssshowgrid.gif");
         showHeadersId = CpToolkit.registerImageName("ssshowheaders.gif");
         insertLcId = CpToolkit.registerImageName("ssinsertlc.gif");

         ptBarnumId = CpToolkit.registerImageName("ptbarnum.gif");

        borderImageIds = new int[12];
        for (int i = 0; i < 12; i++) {
            borderImageIds[i] = CpToolkit.registerImageName("ssborder" + i + ".gif");
        }
    }

    static CpType[] types = {CpBasicType.STRING, CpBasicType.NUMBER, CpBasicType.DATE };

    public void init()
    {
        super.init();

        setBackground(Color.lightGray);

        CpPanelLc panel;

        setLc(panel = new CpPanelLc());
        panel.setDrawOffscreen(true);

        CpAttachmentsLayout layout = new CpAttachmentsLayout();
        panel.setLayout(layout);

        createToolbar(panel);
        CpAttachments attachments = new CpAttachments(10, 10, 10, -1);
        attachments.setHeight(33);
        layout.setAttachments(toolbar, attachments);
        panel.add(toolbar);

        createFormatbar(panel);
        attachments = new CpAttachments(10, -1, 10, -1);
        attachments.setTopAttachment(0, toolbar);
        attachments.setHeight(33);
        layout.setAttachments(formatbar, attachments);

/*        createTypebar(panel);
        attachments = new CpAttachments(10, -1, 10, -1);
        attachments.setTopAttachment(0, formatbar);
        attachments.setHeight(33);
        layout.setAttachments(typebar, attachments);
*/
        createStatusText();
        attachments = new CpAttachments(13, -1, 10, 10);
        attachments.setHeight(20);
        layout.setAttachments(statusText, attachments);
        panel.add(statusText);

        gridLc = new CpGridLc();
    	gridpane = gridLc.getGridPane();
        gridpane.addObserver(this);
        attachments = new CpAttachments(10, -1, 10, -1);
        attachments.setTopAttachment(0, formatbar);
        attachments.setBottomAttachment(0, statusText);
        layout.setAttachments(gridLc, attachments);
        panel.add(gridLc);

        // This is sneaky...
        gridpane.notifyObservers(GRID_SELECTION_CHANGED, gridpane.getSelectedCell());
        layout();
        gridpane.requestFocus();
	}

    void createToolbar(CpPanelLc panel)
    {
        toolbar = new CpWpPanelLc(516, false);
        panel.add(toolbar);

        CpAttachmentsLayout layout = new CpAttachmentsLayout();
        toolbar.setLayout(layout);

        newButton = new CpToolBarButtonLc(CpToolkit.getImage(newId), "New", "Creates a new document");
        toolbar.add(newButton);
        newButton.reshape(6, 5, 23, 22);
        newButton.addObserver(this);

        openButton = new CpToolBarButtonLc(CpToolkit.getImage(openId), "Open", "Opens an existing document");
        openButton.reshape(29, 5, 23, 22);
        toolbar.add(openButton);
        openButton.addObserver(this);

        saveButton = new CpToolBarButtonLc(CpToolkit.getImage(saveId), "Save", "Saves the active document");
        saveButton.reshape(52, 5, 23, 22);
        toolbar.add(saveButton);
        saveButton.addObserver(this);

        printButton = new CpToolBarButtonLc(CpToolkit.getImage(printId), "Print", "Prints the active document");
        printButton.reshape(83, 5, 23, 22);
        toolbar.add(printButton);
        printButton.addObserver(this);

        previewButton = new CpToolBarButtonLc(CpToolkit.getImage(previewId), "Print Preview", "Displays full pages");
        previewButton.reshape(106, 5, 23, 22);
        toolbar.add(previewButton);
        previewButton.addObserver(this);

        cutButton = new CpToolBarButtonLc(CpToolkit.getImage(cutId), "Cut", "Cuts the selection and puts it on the clipboard");
        cutButton.reshape(137, 5, 23, 22);
        toolbar.add(cutButton);
        cutButton.addObserver(this);

        copyButton = new CpToolBarButtonLc(CpToolkit.getImage(copyId), "Copy", "Copies the selection and puts it on the clipboard");
        copyButton.reshape(160, 5, 23, 22);
        toolbar.add(copyButton);
        copyButton.addObserver(this);

        pasteButton = new CpToolBarButtonLc(CpToolkit.getImage(pasteId), "Paste", "Inserts Clipboard contents");
        pasteButton.reshape(183, 5, 23, 22);
        toolbar.add(pasteButton);
        pasteButton.setIsDisabled(true);
        pasteButton.addObserver(this);

        undoButton = new CpToolBarButtonLc(CpToolkit.getImage(undoId), "Undo", "Reverses the last action");
        undoButton.reshape(206, 5, 23, 22);
        toolbar.add(undoButton);
        undoButton.addObserver(this);

        sumButton = new CpToolBarButtonLc(CpToolkit.getImage(sumId), "Sum", "Inserts SUM function and proposes range");
        sumButton.reshape(237, 5, 23, 22);
        toolbar.add(sumButton);
        sumButton.addObserver(this);

        functionButton = new CpToolBarButtonLc(CpToolkit.getImage(functionId), "Paste Function", "Opens the paste function wizard");
        functionButton.reshape(260, 5, 23, 22);
        toolbar.add(functionButton);
        functionButton.addObserver(this);

        sortupButton = new CpToolBarButtonLc(CpToolkit.getImage(sortupId), "Sort Ascending", "Sorts selected rows in ascending order");
        sortupButton.reshape(291, 5, 23, 22);
        toolbar.add(sortupButton);
        sortupButton.addObserver(this);

        sortdownButton = new CpToolBarButtonLc(CpToolkit.getImage(sortdownId), "Sort Descending", "Sorts selected rows in descending order");
        sortdownButton.reshape(314, 5, 23, 22);
        toolbar.add(sortdownButton);
        sortdownButton.addObserver(this);

        showGridButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(showGridId), "Show Gridlines", "Toggles display of the grid lines");
        showGridButton.reshape(345, 5, 23, 22);
        showGridButton.setState(true);
        toolbar.add(showGridButton);
        showGridButton.addObserver(this);

        showHeadersButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(showHeadersId), "Show Headers", "Toggles display of the headers");
        showHeadersButton.reshape(368, 5, 23, 22);
        showHeadersButton.setState(true);
        toolbar.add(showHeadersButton);
        showHeadersButton.addObserver(this);

        scaleCombo = new CpDropDownListLc();
        scaleCombo.addItem("50%");
        scaleCombo.addItem("100%");
        scaleCombo.addItem("200%");
        CpAttachments attachments = new CpAttachments(399, 5, -1, -1);
        attachments.setWidth(80);
        layout.setAttachments(scaleCombo, attachments);
        toolbar.add(scaleCombo);
        scaleCombo.addObserver(this);

        insertLcButton = new CpToolBarButtonLc(CpToolkit.getImage(insertLcId), "Insert Object", "Inserts an object in the cursor cell");
        insertLcButton.reshape(487, 5, 23, 22);
        toolbar.add(insertLcButton);
        insertLcButton.addObserver(this);
    }

    void createFormatbar(CpPanelLc panel)
    {
        formatbar = new CpWpPanelLc(0, true);
        panel.add(formatbar);

        CpAttachmentsLayout layout = new CpAttachmentsLayout();
        formatbar.setLayout(layout);

    	String[] fonts = getToolkit().getFontList();
        fontCombo = new CpDropDownListLc();
    	fonts = getToolkit().getFontList();
    	for (int i = 0; i < fonts.length; i++)
    	{
    	    fontCombo.addItem(fonts[i]);
    	}
        CpAttachments attachments = new CpAttachments(5, 5, -1, -1);
        attachments.setWidth(105);
        layout.setAttachments(fontCombo, attachments);
        formatbar.add(fontCombo);
        fontCombo.addObserver(this);

        sizeCombo = new CpDropDownListLc();
        for (int i = 8; i < 65; i += 2 * (i / 8)) {
            sizeCombo.addItem((new Integer(i)).toString());
        }
        attachments = new CpAttachments(-1, 5, -1, -1);
        attachments.setLeftAttachment(3, fontCombo);
        attachments.setWidth(42);
        layout.setAttachments(sizeCombo, attachments);
        formatbar.add(sizeCombo);
        sizeCombo.addObserver(this);

        formatCombo = new CpDropDownListLc();
        attachments = new CpAttachments(-1, 5, -1, -1);
        attachments.setLeftAttachment(3, sizeCombo);
        attachments.setWidth(100);
        layout.setAttachments(formatCombo, attachments);
        formatbar.add(formatCombo);
        formatCombo.addObserver(this);

        boldButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(boldId), "Bold", "Makes the selection bold (toggle).");
        boldButton.reshape(262, 5, 23, 22);
        formatbar.add(boldButton);
        boldButton.addObserver(this);

        italicButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(italicId), "Italic", "Makes the selection italic (toggle)");
        italicButton.reshape(285, 5, 23, 22);
        formatbar.add(italicButton);
        italicButton.addObserver(this);

        leftButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(leftId), "Align Left", "Left-justifies cells");
        leftButton.reshape(316, 5, 23, 22);
        formatbar.add(leftButton);
        leftButton.addObserver(this);

        centerHButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(centerhId), "Center Horizontally", "Center-justifies cells");
        centerHButton.reshape(339, 5, 23, 22);
        formatbar.add(centerHButton);
        centerHButton.addObserver(this);

        rightButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(rightId), "Align Right", "Right-justifies cells");
        rightButton.reshape(362, 5, 23, 22);
        formatbar.add(rightButton);
        rightButton.addObserver(this);

        topButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(topId), "Align Top", "Top-justifies cells");
        topButton.reshape(385, 5, 23, 22);
        formatbar.add(topButton);
        topButton.addObserver(this);

        centerVButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(centervId), "Center Vertically", "Center-justifies cells");
        centerVButton.reshape(408, 5, 23, 22);
        formatbar.add(centerVButton);
        centerVButton.addObserver(this);

        bottomButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(bottomId), "Align Bottom", "Bottom-justifies cells");
        bottomButton.reshape(431, 5, 23, 22);
        formatbar.add(bottomButton);
        bottomButton.addObserver(this);

        backcolorButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(backcolorId), "Background Color", "Sets the background color for the selected cells");
        backcolorButton.reshape(462, 5, 23, 22);
        formatbar.add(backcolorButton);
        backcolorButton.addObserver(this);

        forecolorButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(forecolorId), "Text Color", "Sets the text color for the selected cells");
        forecolorButton.reshape(485, 5, 23, 22);
        formatbar.add(forecolorButton);
        forecolorButton.addObserver(this);

        borderButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(borderId), "Set Borders", "Sets the border for the selected cells");
        borderButton.reshape(516, 5, 23, 22);
        formatbar.add(borderButton);
        borderButton.addObserver(this);

        wrapButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(wrapId), "Set Text Wrapping", "Set text to wrap in cells");
        wrapButton.reshape(539, 5, 23, 22);
        formatbar.add(wrapButton);
        wrapButton.addObserver(this);
    }

 /*   void createTypebar(CpWpPanelLc panel)
    {
        typebar = new CpWpPanelLc();
        panel.add(typebar);

        CpAttachmentsLayout layout = new CpAttachmentsLayout();
        typebar.setLayout(layout);

        typeCombo = new CpDropDownListLc();
        for (int i = 0; i < types.length; i++) {
            typeCombo.addItem(types[i].toString());
        }

        CpAttachments attachments = new CpAttachments(5, 5, -1, -1);
        attachments.setWidth(233);
        layout.setAttachments(typeCombo, attachments);
        typebar.add(typeCombo);
        typeCombo.addObserver(this);

        formatCombo = new CpDropDownListLc();
        attachments = new CpAttachments(-1, 5, -1, -1);
        attachments.setLeftAttachment(10, typeCombo);
        attachments.setWidth(50);
        layout.setAttachments(formatCombo, attachments);
        typebar.add(formatCombo);
        formatCombo.addObserver(this);
    }
    */

    void createStatusText()
    {
        statusText = new CpReadOnlyTextAreaLc("");
        statusText.setFont(CpFonts.dialogPlainTen());
        statusText.setVerticalAlignment(statusText.ALIGN_CENTER);
	}

    void updateJustificationButtons(CpGridCell selectedCell)
    {
        leftButton.setState(selectedCell.getHorizontalAlignment() == CpAlignable.ALIGN_LEFT);
        rightButton.setState(selectedCell.getHorizontalAlignment() == CpAlignable.ALIGN_RIGHT);
        centerHButton.setState(selectedCell.getHorizontalAlignment() == CpAlignable.ALIGN_CENTER);
        topButton.setState(selectedCell.getVerticalAlignment() == CpAlignable.ALIGN_TOP);
        bottomButton.setState(selectedCell.getVerticalAlignment() == CpAlignable.ALIGN_BOTTOM);
        centerVButton.setState(selectedCell.getVerticalAlignment() == CpAlignable.ALIGN_CENTER);
    }


    public void update(CpObservable o, int facet, Object arg)
    {
        if (facet == GRID_SELECTION_CHANGED) {
            CpGridCell cell = (CpGridCell)arg;

            //statusText.setText(getStatusText());
            toolbar.enableDamageRepair(false);
//            typeCombo.select("" + cell.getType().toString());
            Vector formats = cell.getType().getFormats();
            formatCombo.setItems(formats);
//        	for (int i = 0; i < formats.size(); i++) {
  //              formatCombo.addItem((String)formats.elementAt(i));
    //        }
            if (!formats.isEmpty()) {
                if (!cell.getFormatString().equals("")) {
                    formatCombo.select(cell.getFormatString());
                }
            } else {
                formatCombo.removeAllItems();
                String s = "General";
                formatCombo.addItem(s);
                formatCombo.select(s);
            }
            boldButton.setState(cell.isBold());
            italicButton.setState(cell.isItalic());
            fontCombo.select(cell.getFontName());
            sizeCombo.select("" + cell.getPointSize());
            updateJustificationButtons(cell);
            wrapButton.setState(cell.getShouldWrap());
            toolbar.enableDamageRepair(true);
            toolbar.repairDamage();
        }
        if (facet == CpEvent.HELP_TEXT_CHANGED) {
            statusText.setText((String)arg);
        }
        if (facet == CpEvent.BUTTON_CLICKED) {
            if (o == newButton) {
                gridLc.newDocument(this);
            }
            if (o == openButton || o == saveButton || o == cutButton || o == copyButton || o == pasteButton || o == functionButton || o == undoButton || o == printButton || o == previewButton) {
                CpMessageDialog p = new CpMessageDialog(getFrame(), "Alert:", "This feature is disabled in this applet demo.");
                p.show();
//                    gridpane.openDocument(this);
            }
/*            if (o == saveButton) {
                CpMessageDialog p = new CpMessageDialog(getFrame(), "Yow!", "THere onec was a man from nantucket.  Blah blah. annd there is no shor t cut.  fotball this weekend and dfi d sisjfj a sdfjsdf asd f s fdjasjdf asdfjs df s");
                p.show();
//                    gridpane.saveDocument(this);
            }
            if (o == cutButton) {
               // gridpane.menuCut();
                pasteButton.setIsDisabled(false);
            }
            if (o == copyButton) {
              //  gridpane.menuCopy();
                pasteButton.setIsDisabled(false);
            }
            if (o == pasteButton) {
               // gridpane.menuPaste();
            }
            if (o == functionButton) {
                CpMessageDialog p = new CpMessageDialog(getFrame(), "Alert:", "This feature is disabled in this applet demo.");
                p.show();
            }
            */
            if (o == sumButton) {
                gridpane.insertSumFunction();
            }
            if (o == sortupButton) {
                gridpane.sortRows(CpSortable.ASCENDING);
            }
            if (o == sortdownButton) {
                gridpane.sortRows(CpSortable.DESCENDING);
            }
            if (o == showGridButton) {
                gridpane.toggleGrid();
            }
            if (o == showHeadersButton) {
                gridLc.toggleHeaders();
            }
            if (o == boldButton) {
                gridpane.setBold(boldButton.getState());
            }
            if (o == italicButton) {
                gridpane.setItalic(italicButton.getState());
            }
            if (o == backcolorButton || o == forecolorButton) {
                if (dontPopup == true) {
                    dontPopup = false;
                } else {
                    dontPopup = true;
                    if (colorPal == null) {
                        colorPal = new CpColorPalette(3, 16, 16);
                        colorPal.addObserver(this);
                        colorPal.setDrawOffscreen(true);
                        colorPal.setKeepCachedImage(true);
                    }
                    pal = colorPal;
                    colorButton = (CpToolBarTwoStateButtonLc)o;
                    colorButton.popUpLcAt(pal, new Rectangle(0, backcolorButton.size().height, pal.preferredSize().width, pal.preferredSize().height));
                    pal.requestFocus();
                }
            }
            if (o == borderButton) {
                if (dontPopup == true) {
                    dontPopup = false;
                } else {
                    dontPopup = true;
                    if (borderPalette == null) {
                        borderPalette = new CpGridPalette(4, 20, 20, new CpImageDisplayer());
                        Vector borderImages = new Vector();
                        for (int i = 0; i < 12; i++) {
                            Image im;
                            borderImages.addElement(im = CpToolkit.getImage(borderImageIds[i]));
                        }
                        borderPalette.setItems(borderImages);
                        borderPalette.addObserver(this);
                        borderPalette.setDrawOffscreen(true);
                        borderPalette.setKeepCachedImage(true);
                    }
                    borderPal = borderPalette;
                    borderButton.popUpLcAt(borderPal, new Rectangle(borderButton.size().width - borderPal.preferredSize().width, borderButton.size().height, borderPal.preferredSize().width, borderPal.preferredSize().height));
                    borderPal.requestFocus();
                }
            }
            if (o == leftButton) {
                gridpane.setHorizontalAlignment(CpAlignable.ALIGN_LEFT);
                updateJustificationButtons(gridpane.getSelectedCell());
            }
            if (o == rightButton) {
                gridpane.setHorizontalAlignment(CpAlignable.ALIGN_RIGHT);
                updateJustificationButtons(gridpane.getSelectedCell());
            }
            if (o == centerHButton) {
                gridpane.setHorizontalAlignment(CpAlignable.ALIGN_CENTER);
                updateJustificationButtons(gridpane.getSelectedCell());
            }
            if (o == topButton) {
                gridpane.setVerticalAlignment(CpAlignable.ALIGN_TOP);
                updateJustificationButtons(gridpane.getSelectedCell());
            }
            if (o == bottomButton) {
                gridpane.setVerticalAlignment(CpAlignable.ALIGN_BOTTOM);
                updateJustificationButtons(gridpane.getSelectedCell());
            }
            if (o == centerVButton) {
                gridpane.setVerticalAlignment(CpAlignable.ALIGN_CENTER);
                updateJustificationButtons(gridpane.getSelectedCell());
            }
            if (o == insertLcButton) {
                Vector choices = new Vector();
              choices.addElement("Chart");
              choices.addElement("Word Processor");
		      choices.addElement("Image");
              choices.addElement("Button");
              choices.addElement("Diagram");
                lcList = new CpListChooserDialog(getFrame(), "Please Select An Item", "Please select a component to insert into the spreadsheet.", choices);
    	        lcList.addObserver(this);
    	        lcList.show();
            }
            if (o == wrapButton) {
                gridpane.setShouldWrap(wrapButton.getState());
            }
        }
        if (facet == LC_POPPED_DOWN) {
            // What a mess...
            if (pal != null) {
                pal = null;
                Point mouseLoc = colorButton.mouseLocation();
                Rectangle rect = new Rectangle(0, 0, colorButton.size().width, colorButton.size().height);
                if (!rect.inside(mouseLoc.x, mouseLoc.y)) {
                    dontPopup = false;
                    colorButton.setState(false);
                }
            }
            if (borderPal != null) {
                borderPal = null;
                Point mouseLoc = borderButton.mouseLocation();
                Rectangle rect = new Rectangle(0, 0, borderButton.size().width, borderButton.size().height);
                if (!rect.inside(mouseLoc.x, mouseLoc.y)) {
                    dontPopup = false;
                    borderButton.setState(false);
                }
            }
        }
        if (facet == OBJECT_CHANGED) {
            if (o == borderPal) {
                int borderType = borderPal.getSelectedIndex();
                gridpane.setBorderType(borderType);
            }
            if (o == pal) {
                if (colorButton == forecolorButton) {
               	    gridpane.setForecolor((Color)arg);
               	} else {
               	    gridpane.setBackcolor((Color)arg);
               	}
            }
            if (o == lcList) {
           	    String lcName = (String)arg;
               	if (lcName.equals("Image")) {
               	    gridpane.insertLc(new CpImageLc(CpToolkit.getImage(ptBarnumId)));
               	}
           	    if (lcName.equals("Button")) {
                    CpLabeledImageButtonLc example = new CpLabeledImageButtonLc("Howdy");
                    example.setBackground(Color.lightGray);
           	        gridpane.insertLc(example);
           	    }
           	    if (lcName.equals("Diagram")) {
           	        gridpane.insertLc(CpDiagramLc.example());
           	    }
           	    if (lcName.equals("Chart")) {
           	        CpAbstractLc chart = CpDemoExamples.createExampleChart();
           	        gridpane.insertLc(chart);
           	    }
           	    if (lcName.equals("Word Processor")) {
                    CpRichTextEditorLc richtext = CpDemoExamples.createExampleRichText();
           	        gridpane.insertLc(richtext);
           	    }
           	}
            if (o == fontCombo) {
                gridpane.setFontName(fontCombo.getSelectedItem());
                gridpane.requestFocus();
            }
            if (o == sizeCombo) {
                gridpane.setFontSize((new Integer(sizeCombo.getSelectedItem())).intValue());
                gridpane.requestFocus();
            }
       /*     if (o == typeCombo) {
                gridpane.setType(types[fontCombo.getSelectedIndex()]);
                gridpane.requestFocus();
            }
            */
            if (o == formatCombo) {
                gridpane.setFormatString(formatCombo.getSelectedItem());
                gridpane.requestFocus();
            }
            if (o == scaleCombo) {
                switch (scaleCombo.getSelectedIndex()) {
                    case 0 : { gridpane.scale(.5); break; }
                    case 1 : { gridpane.scale(1); break; }
                    case 2 : { gridpane.scale(2); break; }
                }
            }
        }
    }

	public boolean handleEvent (Event evt) {

		switch(evt.id) {
		    case Event.WINDOW_DESTROY: {
		        hide();
		    //    dispose();
			//    System.exit(0);
            }
		}
		return super.handleEvent(evt);
	}
}
