
import java.awt.*;
import java.util.*;
import cnp.ew.diagram.*;
import cnp.ew.util.*;
import cnp.ew.layout.*;
import cnp.ew.list.*;
import cnp.ew.displayer.*;
import cnp.ew.lightweight.*;
import cnp.ew.button.*;
import cnp.ew.image.*;
import cnp.ew.misc.*;
import cnp.ew.properties.*;
import cnp.ew.tdemo.*;
import cnp.ew.kdemo.*;
import cnp.ew.text.*;
import cnp.ew.spin.*;
import cnp.ew.scrolling.*;
import cnp.ew.notebook.*;
import cnp.ew.charts.*;
import cnp.ew.richtext.*;
import cnp.ew.dialog.*;
import cnp.ew.grid.*;

public class CpDiagramDemo extends CpLcApplet implements CpObserver
{
    CpReadOnlyTextAreaLc comboLabel;
    CpDropDownListLc combo;

    CpToolBarButtonLc newButton, openButton, saveButton, printButton, previewButton;
    CpToolBarButtonLc cutButton, copyButton, pasteButton, undoButton, alignLeftButton;
    CpToolBarButtonLc alignRightButton, alignCenterHButton, alignTopButton, alignBottomButton;
    CpToolBarButtonLc alignCenterVButton, matchSizeHButton, matchSizeVButton, matchSizeBButton, distributeHButton, distributeVButton, toggleGridButton, scaleUpButton, scaleDownButton;

    CpToolBarTwoStateButtonLc arrowButton, buttonButton, checkBoxButton, comboButton, entryButton, graphicsButton, gridButton, groupBoxButton;
    CpToolBarTwoStateButtonLc hscrollButton, imageButton, lineButton, listButton, notebookButton, radioButton, staticTextButton, timeButton, vscrollButton;

    CpPanelLc toolbar, palette;
    CpReadOnlyTextAreaLc statusText;

	CpDiagram diagram;
	CpDiagramLc diagramFrame;
	//CpPropertySheetFrame propertyFrame;
	CpPropertySheet propertySheet;

	CpAttachments palAttachments, propAttachments;

    static final int OMT = 0;
    static final int BUILDER = 1;
    static final int ORGCHART = 2;
    static final int LAYOUT = 3;

    int currentApp = 0;

    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 alignLeftId;
    static int alignRightId;
    static int alignCenterHId;
    static int alignTopId;
    static int alignBottomId;
    static int alignCenterVId;
    static int matchSizeHId;
    static int matchSizeVId;
    static int matchSizeBId;
    static int distributeHId;
    static int distributeVId;
    static int toggleGridId;
    static int scaleUpId;
    static int scaleDownId;

    static int arrowId;
    static int buttonId;
    static int checkBoxId;
    static int comboId;
    static int entryId;
    static int graphicsId;
    static int gridId;
    static int groupBoxId;
    static int hscrollId;
    static int imageId;
    static int lineId;
    static int listId;
    static int notebookId;
    static int radioId;
    static int staticTextId;
    static int timeId;
    static int vscrollId;
    static int moverId;
    static int sizerId;

    static int guyId;
    static int guy2Id;
    static int inventorId;
    static int ptBarnumId;
    static int sailorId;

    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");
         alignLeftId = CpToolkit.registerImageName("ibalignl.gif");
         alignRightId = CpToolkit.registerImageName("ibalignr.gif");
         alignCenterHId = CpToolkit.registerImageName("ibalignch.gif");
         alignTopId = CpToolkit.registerImageName("ibalignt.gif");
         alignBottomId = CpToolkit.registerImageName("ibalignb.gif");
         alignCenterVId = CpToolkit.registerImageName("ibaligncv.gif");
         matchSizeHId = CpToolkit.registerImageName("ibmatchsizeh.gif");
         matchSizeVId = CpToolkit.registerImageName("ibmatchsizev.gif");
         matchSizeBId = CpToolkit.registerImageName("ibmatchsizeb.gif");
         distributeHId = CpToolkit.registerImageName("ibdistribh.gif");
         distributeVId = CpToolkit.registerImageName("ibdistribv.gif");
         toggleGridId = CpToolkit.registerImageName("ibtogglegrid.gif");
         scaleUpId = CpToolkit.registerImageName("ibzoomin.gif");
         scaleDownId = CpToolkit.registerImageName("ibzoomout.gif");

         arrowId = CpToolkit.registerImageName("ibarrow.gif");
         buttonId = CpToolkit.registerImageName("ibbutton.gif");
         checkBoxId = CpToolkit.registerImageName("ibcheckBox.gif");
         comboId = CpToolkit.registerImageName("ibcombo.gif");
         entryId = CpToolkit.registerImageName("ibentry.gif");
         graphicsId = CpToolkit.registerImageName("ibgraphics.gif");
         gridId = CpToolkit.registerImageName("ibgrid.gif");
         groupBoxId = CpToolkit.registerImageName("ibgroupBox.gif");
         hscrollId = CpToolkit.registerImageName("ibhscroll.gif");
         imageId = CpToolkit.registerImageName("ibimage.gif");
         lineId = CpToolkit.registerImageName("ibline.gif");
         listId = CpToolkit.registerImageName("iblist.gif");
         notebookId = CpToolkit.registerImageName("ibnotebook.gif");
         radioId = CpToolkit.registerImageName("ibradio.gif");
         staticTextId = CpToolkit.registerImageName("ibstaticText.gif");
         timeId = CpToolkit.registerImageName("ibtime.gif");
         vscrollId = CpToolkit.registerImageName("ibvscroll.gif");
         moverId = CpToolkit.registerImageName("ibmover.gif");
         sizerId = CpToolkit.registerImageName("ibsizer.gif");

         guyId = CpToolkit.registerImageName("guy.gif");
         guy2Id = CpToolkit.registerImageName("guy2.gif");
         inventorId = CpToolkit.registerImageName("inventor.gif");
         ptBarnumId = CpToolkit.registerImageName("ptbarnum.gif");
         sailorId = CpToolkit.registerImageName("sailor.gif");
    }

	public void init () {

		super.init();

        CpImageGetter.getDefault().setApplet(this);

        setBackground(Color.lightGray);

        CpPanelLc panel;

        setLc(panel = new CpPanelLc());
        panel.setDrawOffscreen(true);

        CpAttachmentsLayout layout = new CpAttachmentsLayout();
        panel.setLayout(layout);

        combo = new CpDropDownListLc();
        combo.addItem("OMT Diagrammer");
        combo.addItem("Interface Builder");
        combo.addItem("Org Chart Editor");
        combo.addItem("Page Layout Editor");
        CpAttachments attachments = new CpAttachments(-1, 5, -1, -1);
        attachments.setHorizontalCentering();
  //      attachments.setWidth(175);
        layout.setAttachments(combo, attachments);
        panel.add(combo);
        combo.addObserver(this);

        createToolbar();
        attachments = new CpAttachments(4, -1, 10, -1);
        attachments.setHeight(33);
        attachments.setTopAttachment(5, combo);
        layout.setAttachments(toolbar, attachments);
        panel.add(toolbar);

        createStatusText();
        attachments = new CpAttachments(13, -1, 10, 0);
        attachments.setHeight(33);
        layout.setAttachments(statusText, attachments);
        panel.add(statusText);

        createPalette();
        palAttachments = new CpAttachments(10, -1, -1, -1);
        palAttachments.setTopAttachment(0, toolbar);
        palAttachments.setWidth(0);
        palAttachments.setBottomAttachment(0, statusText);
        layout.setAttachments(palette, palAttachments);
        panel.add(palette);


        propertySheet = new CpPropertySheet();
        propAttachments = new CpAttachments(-1, -1, 10, -1);
        propAttachments.setWidth(0);
        propAttachments.setTopAttachment(0, toolbar);
        propAttachments.setBottomAttachment(0, statusText);
        layout.setAttachments(propertySheet, propAttachments);

        propertySheet.setBackground(Color.white);
        propertySheet.setPropertiesAndModel(new Vector(), null);

        panel.add(propertySheet);

        diagramFrame = new CpDiagramLc(true, true, new CpDiagram());
        diagramFrame.setBackground(Color.white);
        diagram = diagramFrame.getDiagram();
        diagram.addObserver(this);
        attachments = new CpAttachments(-1, -1, -1, -1);
        attachments.setTopAttachment(0, toolbar);
        attachments.setLeftAttachment(0, palette);
        attachments.setRightAttachment(0, propertySheet);
        attachments.setBottomAttachment(0, statusText);
        layout.setAttachments(diagramFrame, attachments);
        panel.add(diagramFrame);
        selectApp(OMT);

        // Attempt to load image at startup, rather than switching apps.  Ken will this work?
        CpToolkit.getImage(ptBarnumId);

        diagram.requestFocus();

        layout();

	}

    void createToolbar()
    {
        toolbar = new CpPanelLc();

        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);

        alignLeftButton = new CpToolBarButtonLc(CpToolkit.getImage(alignLeftId), "Align Left", "Aligns the left edges of the selected items");
        alignLeftButton.reshape(237, 5, 23, 22);
        toolbar.add(alignLeftButton);
        alignLeftButton.addObserver(this);

        alignRightButton = new CpToolBarButtonLc(CpToolkit.getImage(alignRightId), "Align Right", "Aligns the right edges of the selected items");
        alignRightButton.reshape(260, 5, 23, 22);
        toolbar.add(alignRightButton);
        alignRightButton.addObserver(this);

        alignCenterHButton = new CpToolBarButtonLc(CpToolkit.getImage(alignCenterHId), "Align Center Horizontally", "Aligns the horizontal centers of the selected items");
        alignCenterHButton.reshape(283, 5, 23, 22);
        toolbar.add(alignCenterHButton);
        alignCenterHButton.addObserver(this);

        alignTopButton = new CpToolBarButtonLc(CpToolkit.getImage(alignTopId), "Align Top", "Aligns the top edges of the selected items");
        alignTopButton.reshape(306, 5, 23, 22);
        toolbar.add(alignTopButton);
        alignTopButton.addObserver(this);

        alignBottomButton = new CpToolBarButtonLc(CpToolkit.getImage(alignBottomId), "Align Bottom", "Aligns the bottom edges of the selected items");
        alignBottomButton.reshape(329, 5, 23, 22);
        toolbar.add(alignBottomButton);
        alignBottomButton.addObserver(this);

        alignCenterVButton = new CpToolBarButtonLc(CpToolkit.getImage(alignCenterVId), "Align Center Vertically", "Aligns the vertical centers of the selected items");
        alignCenterVButton.reshape(352, 5, 23, 22);
        toolbar.add(alignCenterVButton);
        alignCenterVButton.addObserver(this);

        matchSizeHButton = new CpToolBarButtonLc(CpToolkit.getImage(matchSizeHId), "Match Width", "Makes the selected items the same width");
        matchSizeHButton.reshape(383, 5, 23, 22);
        toolbar.add(matchSizeHButton);
        matchSizeHButton.addObserver(this);

        matchSizeVButton = new CpToolBarButtonLc(CpToolkit.getImage(matchSizeVId), "Match Height", "Makes the selected items the same height");
        matchSizeVButton.reshape(406, 5, 23, 22);
        toolbar.add(matchSizeVButton);
        matchSizeVButton.addObserver(this);

        matchSizeBButton = new CpToolBarButtonLc(CpToolkit.getImage(matchSizeBId), "Match Size", "Makes the selected items the same size");
        matchSizeBButton.reshape(429, 5, 23, 22);
        toolbar.add(matchSizeBButton);
        matchSizeBButton.addObserver(this);

        distributeHButton = new CpToolBarButtonLc(CpToolkit.getImage(distributeHId), "Distribute Horizontally", "Evenly space the selected items horizontally");
        distributeHButton.reshape(460, 5, 23, 22);
        toolbar.add(distributeHButton);
        distributeHButton.addObserver(this);

        distributeVButton = new CpToolBarButtonLc(CpToolkit.getImage(distributeVId), "Distribute Vertically", "Evenly space the selected items vertically");
        distributeVButton.reshape(483, 5, 23, 22);
        toolbar.add(distributeVButton);
        distributeVButton.addObserver(this);

        toggleGridButton = new CpToolBarButtonLc(CpToolkit.getImage(toggleGridId), "Toggle Grid", "Toggle the background grid on/off");
        toggleGridButton.reshape(514, 5, 23, 22);
        toolbar.add(toggleGridButton);
        toggleGridButton.addObserver(this);

        scaleDownButton = new CpToolBarButtonLc(CpToolkit.getImage(scaleDownId), "Scale Down", "Scale the drawing to half its magnification");
        scaleDownButton.reshape(545, 5, 23, 22);
        toolbar.add(scaleDownButton);
        scaleDownButton.addObserver(this);

        scaleUpButton = new CpToolBarButtonLc(CpToolkit.getImage(scaleUpId), "Scale Up", "Scale the drawing to double its magnification");
        scaleUpButton.reshape(568, 5, 23, 22);
        toolbar.add(scaleUpButton);
        scaleUpButton.addObserver(this);
    }

    void createPalette()
    {
        palette = new CpPanelLc();

        arrowButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(arrowId), "Select", "Sets the selection tool");
        palette.add(arrowButton);
        arrowButton.reshape(6, 5, 25, 24);
        arrowButton.addObserver(this);

        buttonButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(buttonId), "Button", "Loads the cursor with a Button");
        palette.add(buttonButton);
        buttonButton.reshape(6, 29, 25, 24);
        buttonButton.addObserver(this);

        entryButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(entryId), "Entry Field", "Loads the cursor with an Entry Field");
        palette.add(entryButton);
        entryButton.reshape(6, 53, 25, 24);
        entryButton.addObserver(this);

        groupBoxButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(groupBoxId), "Group Box", "Loads the cursor with an Group Box");
        palette.add(groupBoxButton);
        groupBoxButton.reshape(6, 77, 25, 24);
        groupBoxButton.addObserver(this);

        hscrollButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(hscrollId), "Horizontal Scroll Bar", "Loads the cursor with a Horizontal Scroll Bar");
        palette.add(hscrollButton);
        hscrollButton.reshape(6, 101, 25, 24);
        hscrollButton.addObserver(this);

        notebookButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(notebookId), "Notebook", "Loads the cursor with a Notebook");
        palette.add(notebookButton);
        notebookButton.reshape(6, 125, 25, 24);
        notebookButton.addObserver(this);

        staticTextButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(staticTextId), "Static Text", "Loads the cursor with a Static Text");
        palette.add(staticTextButton);
        staticTextButton.reshape(31, 5, 25, 24);
        staticTextButton.addObserver(this);

        checkBoxButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(checkBoxId), "Check Box", "Loads the cursor with a Check Box");
        palette.add(checkBoxButton);
        checkBoxButton.reshape(31, 29, 25, 24);
        checkBoxButton.addObserver(this);

        graphicsButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(graphicsId), "Graphic", "Loads the cursor with a Graphic");
        palette.add(graphicsButton);
        graphicsButton.reshape(31, 53, 25, 24);
        graphicsButton.addObserver(this);

        listButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(listId), "List Box", "Loads the cursor with an List Box");
        palette.add(listButton);
        listButton.reshape(31, 77, 25, 24);
        listButton.addObserver(this);

        vscrollButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(vscrollId), "Vertical Scroll Bar", "Loads the cursor with a Vertical Scroll Bar");
        palette.add(vscrollButton);
        vscrollButton.reshape(31, 101, 25, 24);
        vscrollButton.addObserver(this);

        gridButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(gridId), "Spreadsheet", "Loads the cursor with a Spreadsheet");
        palette.add(gridButton);
        gridButton.reshape(31, 125, 25, 24);
        gridButton.addObserver(this);
    }

    void createStatusText()
    {
        statusText = new CpReadOnlyTextAreaLc("");
        statusText.setFont(CpFonts.dialogPlainTen());
        statusText.setVerticalAlignment(statusText.ALIGN_CENTER);
	}

    public void addOmtLcs(CpDiagram diagram)
    {
        diagram.setBackgroundLc(null);

        Vector v = new Vector();
        Vector methods = new Vector();
        methods.addElement("void setOrigin(int x, int y)");
        methods.addElement("void setExtent(int w, int h)");
        methods.addElement("Point getCenter()");
        Vector variables = new Vector();
        variables.addElement("Point origin");
        variables.addElement("Point corner");
        CpOmtLc omt1 = new CpOmtLc("Rectangle", variables, methods, false);
        omt1.setBackground(new Color(255, 255, 204));
        omt1.move(70, 50);
        omt1.resize(omt1.preferredSize());
        v.addElement(omt1);

        methods = new Vector();
        methods.addElement("void setX(int x)");
        methods.addElement("void setY(int y)");
        methods.addElement("Dimension distanceTo(int x, int y)");
        variables = new Vector();
        variables.addElement("int x");
        variables.addElement("int y");
        CpOmtLc omt = new CpOmtLc("Point", variables, methods, false);
        omt.setBackground(new Color(255, 255, 204));
        omt.move(250, 50);
        omt.resize(omt.preferredSize());
        v.addElement(omt);

        methods = new Vector();
        variables = new Vector();
        variables.addElement("origin = (10, 15)");
        variables.addElement("corner = (25, 30)");
        CpOmtLc omt2 = new CpOmtLc("Rectangle: rect", variables, methods, true);
        omt2.setBackground(new Color(166, 202, 240));
        omt2.move(50, 200);
        omt2.resize(omt2.preferredSize());
        v.addElement(omt2);

        methods = new Vector();
        variables = new Vector();
        variables.addElement("x = 30");
        variables.addElement("y = 40");
        omt = new CpOmtLc("Point: p1", variables, methods, true);
        omt.setBackground(new Color(166, 202, 240));
        omt.move(250, 200);
        omt.resize(omt.preferredSize());
        v.addElement(omt);

        methods = new Vector();
        variables = new Vector();
        variables.addElement("x = 30");
        variables.addElement("y = 40");
        omt = new CpOmtLc("Point: p1", variables, methods, true);
        omt.setBackground(new Color(166, 202, 240));
        omt.move(400, 200);
        omt.resize(omt.preferredSize());
        v.addElement(omt);

        diagram.setLcs(v);

	    CpDependentLineLc connectionLine = new CpDependentLineLc();
	    connectionLine.startDecoration = connectionLine.DIAMOND;
	    connectionLine.endDecoration = connectionLine.ARROW;

	    CpDependentConnector connector;
        connector = new CpDependentConnector(connectionLine);
// 		connector.setOverlap(new Dimension(0, 30));
 	//	connector.setLinesFromCenter(true, false);

        connector.connect(omt1, omt2);
    }

    public void addBuilderLcs(CpDiagram diagram)
    {
        diagram.setBackgroundLc(new CpWindowBackgroundLc(5, 5, 450, 300));

        Vector v = new Vector();
     /*   CpLabeledImageButtonLc button = new CpLabeledImageButtonLc("Button");
        button.setBackground(Color.lightGray);
        CpDiagrammableWrapperLc wrapper = new CpDiagrammableWrapperLc(button);

        wrapper.move(50, 50);
        wrapper.resize(100, 30);
        v.addElement(wrapper);
        */

        diagram.setLcs(v);
    }


    void addOrgChartLcs(CpDiagram diagram)
    {
        diagram.setBackgroundLc(null);

        CpEmployee person;
        CpOrgChartEmployeeLc ceoLc, vpSalesLc, vpDevelopmentLc, distribMgrLc, marketingMgrLc;
        Dimension preferredSize;
        int currentYear = new Date().getYear();

        Vector v = new Vector();

        int age = 45;
        person = new CpEmployee(
            "Otis Lambert",
            "President and CEO",
            "3035466827",
            "3034427665",
            age,
            25,
            "565842206",
            new Date(71, 10, 14),
            true,
            ptBarnumId);

        ceoLc = new CpOrgChartEmployeeLc(person);
        preferredSize = ceoLc.preferredSize();
        ceoLc.reshape(111, 21, 187, 70);
        v.addElement(ceoLc);


        age = 35;
        person = new CpEmployee(
            "Mike Griffith",
            "VP Development",
            "3035466801",
            "3034437125",
            age,
            12,
            "235875698",
            new Date(81, 3, 23),
            false,
            guy2Id);
        vpDevelopmentLc = new CpOrgChartEmployeeLc(person);
        vpDevelopmentLc.reshape(7, 144, 168, 73);
        v.addElement(vpDevelopmentLc);



        age = 35;
        person = new CpEmployee(
            "Emmett Clempsey",
            "VP Sales",
            "3035466803",
            "3035452172",
            age,
            12,
            "432125678",
            new Date(75, 10, 9),
            false,
            sailorId);
        vpSalesLc = new CpOrgChartEmployeeLc(person);
        vpSalesLc.reshape(234, 144, 176, 73);
        v.addElement(vpSalesLc);

        age = 25;
        person = new CpEmployee(
            "Henry Bipps",
            "Marketing Mgr.",
            "3035466879",
            "3035452172",
            age,
            8,
            "432125382",
            new Date(77, 10, 20),
            false,
            guyId);
        marketingMgrLc = new CpOrgChartEmployeeLc(person);
        marketingMgrLc.reshape(130, 267, 176, 73);
        v.addElement(marketingMgrLc);

        age = 29;
        person = new CpEmployee(
            "Elmer Keen",
            "Distribution Mgr.",
            "3035466877",
            "3035452172",
            age,
            8,
            "432125111",
            new Date(78, 6, 11),
            true,
            inventorId);
        distribMgrLc = new CpOrgChartEmployeeLc(person);
        distribMgrLc.reshape(355, 267, 176, 73);
        v.addElement(distribMgrLc);

        diagram.setLcs(v);

        CpDependentLineLc connectionLine = new CpDependentLineLc();
        connectionLine.startDecoration = connectionLine.NONE;
	    connectionLine.endDecoration = connectionLine.ARROW;
 		CpDependentConnector connector = new CpDependentConnector(connectionLine, true, true);
 		connector.connect(ceoLc, vpDevelopmentLc);

        connectionLine = new CpDependentLineLc();
        connectionLine.startDecoration = connectionLine.NONE;
	    connectionLine.endDecoration = connectionLine.ARROW;
 		connector = new CpDependentConnector(connectionLine, true, true);
 		connector.connect(ceoLc, vpSalesLc);

        connectionLine = new CpDependentLineLc();
        connectionLine.startDecoration = connectionLine.NONE;
	    connectionLine.endDecoration = connectionLine.ARROW;
 	    connector = new CpDependentConnector(connectionLine, true, true);
 		connector.connect(vpSalesLc, marketingMgrLc);

        connectionLine = new CpDependentLineLc();
        connectionLine.startDecoration = connectionLine.NONE;
	    connectionLine.endDecoration = connectionLine.ARROW;
 		connector = new CpDependentConnector(connectionLine, true, true);
 		connector.connect(vpSalesLc, distribMgrLc);


    }


	private void initializeRichTextPane(CpRichTextPane richpane)
	{
	    CpParagraph para;

        para = new CpParagraph("Here is some rich text.  This is acting as a header for:");
        para.setFormat(13, 9, CpCharFormat.getBold());
        para.setFormat(45, 6, CpCharFormat.getItalic());
	    richpane.addParagraph(para);

        para = new CpParagraph("");
	    richpane.addParagraph(para);

        para = new CpParagraph("A 3D Bar Chart");
        para.setBulletMode(true);
        para.setMargins(50, 0, 0);
	    richpane.addParagraph(para);

        para = new CpParagraph("A Read-only Spreadsheet");
        para.setBulletMode(true);
        para.setMargins(50, 0, 0);
	    richpane.addParagraph(para);

        para = new CpParagraph("");
	    richpane.addParagraph(para);

        para = new CpParagraph("This rich-text is set to wrapToWidth mode.  When you resize this text it will wrap to the new width.  This is simply a read-only version of the word processor.");
        para.setFormat(25, 11, CpCharFormat.getUnderline());
	    richpane.addParagraph(para);

	    richpane.rewrap();
	}

    void addLayoutLcs(CpDiagram diagram)
    {
        diagram.setBackgroundLc(new CpPageBackgroundLc(10, 10, 545, 710));

        Vector v = new Vector();

        CpReadOnlyTextAreaLc text = new CpReadOnlyTextAreaLc("Cooper & Peters Page Layout Tool");
        text.setFont(new Font("Helvetica", Font.BOLD, 24));
        text.setForeground(Color.orange);
        text.setBackground(null);
        text.resize(text.preferredSize());
        CpDiagrammableWrapperLc wrapper = new CpDiagrammableWrapperLc(text);
        wrapper.reshape(79, 64, 396, 30);
        v.addElement(wrapper);

        CpAbstractChartLc chartLc = CpDemoExamples.createExampleChart();
        chartLc.reshape(285, 122, 200, 150);
        v.addElement(chartLc);

        CpRichTextEditorLc richtext = new CpRichTextEditorLc(false, false);
        richtext.setBorderStyle(richtext.BORDER_NONE);
    	CpRichTextPane richpane = richtext.getPane();
    	richpane.setWrapToWidth(true);
    	initializeRichTextPane(richpane);
        wrapper = new CpDiagrammableWrapperLc(richtext);
        wrapper.reshape(70, 122, 196, 257);
    //    wrapper.setKeepCachedImage(true);
        v.addElement(wrapper);

        CpGridLc gridLc = CpDemoExamples.createExampleSpreadSheet();
        wrapper = new CpDiagrammableWrapperLc(gridLc);
        wrapper.reshape(76, 380, 402, 82);
        wrapper.setKeepCachedImage(true);
        v.addElement(wrapper);

        diagram.setLcs(v);
    }

    public void selectApp(int newApp)
    {

        CpLightweightComponent rootLc = getRootPanel().getLc();
        rootLc.enableDamageRepair(false);
        currentApp = newApp;

     //   CpSizingHandle.xorDefault = (newApp == BUILDER || newApp == LAYOUT);

        switch (newApp) {
            case OMT: {
                addOmtLcs(diagram);
                break;
            }
            case ORGCHART: {
                addOrgChartLcs(diagram);
                break;
            }
            case BUILDER: {
                addBuilderLcs(diagram);
                break;
            }
            case LAYOUT: {
                addLayoutLcs(diagram);
                break;
            }
            default: diagram.setLcs(new Vector());
        }
        if (newApp == BUILDER) {
            palAttachments.setWidth(60);
        } else {
            palAttachments.setWidth(0);
        }
        if (newApp == ORGCHART) {
            propAttachments.setWidth(200);
        } else {
            propAttachments.setWidth(0);
        }
        scaleUpButton.setIsDisabled(newApp == BUILDER);
        scaleDownButton.setIsDisabled(newApp == BUILDER);

        rootLc.resize(0, 0);
        getRootPanel().layout();
        diagram.scrollTo(0, 0);
        rootLc.enableDamageRepair(true);
        ((CpAbstractLc)rootLc).repaint();
    }

    public String getStatusText()
    {
        Vector selectedLcs = diagram.getSelectedLcs();
        if (selectedLcs.isEmpty()) {
            return "No selection";
        } else if (selectedLcs.size() > 1) {
            return "" + selectedLcs.size() + " items selected";
        } else {
            CpDiagrammableLc selectedLc = diagram.getFirstSelected();
            if (selectedLc instanceof CpOmtLc) {
                return "" + ((CpOmtLc)selectedLc).getName() + " selected";
            } else if (selectedLc instanceof CpOrgChartEmployeeLc) {
                return "" + ((CpOrgChartEmployeeLc)selectedLc).getName() + " selected";
            } else if (selectedLc instanceof CpDiagrammableWrapperLc) {
                return "" + ((CpDiagrammableWrapperLc)selectedLc).getWrappedLc().getClass().getName() + " selected";
            } else {
                return "" + selectedLc.getClass().getName() + " selected";
            }
        }
    }

    public void update(CpObservable o, int facet, Object arg)
    {
        switch (facet) {
            case OBJECT_CHANGED : {
                if (o == combo) {
                    selectApp(combo.getSelectedIndex());
                    break;
                }
                break;
            }
            case CpEvent.DIAGRAM_SELECTION_MOVESIZE : {
             //   if (diagram.getFirstSelected() instanceof CpPersonLc) {
               //     Rectangle r = diagram.getFirstSelected().boundsDiagram();
                 //   statusText.setText("DJSAFLKJSD:LFKJLS:DJF");
                 //   statusText.repaint();
                //    statusText.setText("x = " + r.x + " y = " + r.y + " w = " + r.width + " h = " + r.height);
               // }
               // break;
            }
            case CpEvent.HELP_TEXT_CHANGED : {
                if (arg == null || ((String)arg).equals("")) {
                    statusText.setText(getStatusText());
                } else {
                    statusText.setText((String)arg);
                }
                break;
            }
            case CpEvent.BUTTON_CLICKED : {
                if (o == newButton) {
                    diagram.newDocument(this);
                }
                if (o == openButton || o == saveButton || o == undoButton || o == printButton || o == previewButton) {
                    CpMessageDialog p = new CpMessageDialog(getFrame(), "Alert:", "This feature is disabled in this applet demo.");
                    p.show();
                }
                /*
                if (o == openButton) {
                    CpMessageDialog p = new CpMessageDialog(getFrame(), "Alert:", "This feature is disabled in this applet demo.");
                    p.show();
                }
                if (o == saveButton) {
                    CpMessageDialog p = new CpMessageDialog(getFrame(), "Alert:", "This feature is disabled in this applet demo.");
                    p.show();
                } */
                if (o == scaleUpButton) {
                    diagram.scaleUp();
                }
                if (o == scaleDownButton) {
                    diagram.scaleDown();
                }
                if (o == cutButton) {
                    diagram.menuCut();
                  //  pasteButton.setIsDisabled(false);
                }
                if (o == copyButton) {
                    diagram.menuCopy();
                  //  pasteButton.setIsDisabled(false);
                }
                if (o == pasteButton) {
                    diagram.menuPaste();
                }
                if (o == alignLeftButton) {
                    diagram.menuAlign(diagram.LEFT);
                }
                if (o == alignRightButton) {
                    diagram.menuAlign(diagram.RIGHT);
                }
                if (o == alignCenterHButton) {
                    diagram.menuAlign(diagram.CENTER_X);
                }
                if (o == alignTopButton) {
                    diagram.menuAlign(diagram.TOP);
                }
                if (o == alignBottomButton) {
                    diagram.menuAlign(diagram.BOTTOM);
                }
                if (o == alignCenterVButton) {
                    diagram.menuAlign(diagram.CENTER_Y);
                }
                if (o == matchSizeHButton) {
                    diagram.menuMatchSize(diagram.HORIZONTAL);
                }
                if (o == matchSizeVButton) {
                    diagram.menuMatchSize(diagram.VERTICAL);
                }
                if (o == matchSizeBButton) {
                    diagram.menuMatchSize(diagram.BOTH);
                }
                if (o == distributeHButton) {
                    diagram.menuDistribute(diagram.HORIZONTAL);
                }
                if (o == distributeVButton) {
                    diagram.menuDistribute(diagram.VERTICAL);
                }
                if (o == toggleGridButton) {
                    diagram.toggleGrid();
                }
                if (o == arrowButton) {
                    diagram.unloadCursor();
                    selectTool(arrowButton);
                }
                if (o == buttonButton) {
                    CpLabeledImageButtonLc button = new CpLabeledImageButtonLc("Button");
                    button.resize(100, 28);
                    addLcToDiagram(button);
                    selectTool(buttonButton);
                }
                if (o == entryButton) {
                    CpEntryFieldLc entry = new CpEntryFieldLc("text");
                    entry.setBorderStyle(entry.BORDER_INWARD3D);
                    entry.resize(120, entry.preferredSize().height + 2);
                    addLcToDiagram(entry);
                    entry.setBackground(Color.white);
                    selectTool(entryButton);
                }
                if (o == groupBoxButton) {
                    CpPanelLc panel = new CpPanelLc();
                    panel.setBorderStyle(panel.BORDER_LABELED_ETCHED);
                    panel.setText("Group Box");
                    panel.resize(100, 100);
                    addLcToDiagram(panel);
                    selectTool(groupBoxButton);
                }
                if (o == hscrollButton) {
                    CpScrollbarLc hbar = new CpScrollbarLc(CpScrollbarLc.HORIZONTAL);
                    hbar.resize(120, 16);
                    addLcToDiagram(hbar);
                    selectTool(hscrollButton);
                }
                if (o == notebookButton) {
                    Cp3DNotebookLc notebook = new Cp3DNotebookLc();
                    notebook.setFont(CpFonts.helveticaPlainTwelve());
                    notebook.addPage("Page 1", new CpReadOnlyTextAreaLc(" "));
                    notebook.resize(140, 100);
                    addLcToDiagram(notebook);
                    selectTool(notebookButton);
                }
                if (o == staticTextButton) {
                    CpReadOnlyTextAreaLc text = new CpReadOnlyTextAreaLc("Static Text");
                    text.resize(text.preferredSize().width + 15, text.preferredSize().height);
                    addLcToDiagram(text);
                    selectTool(staticTextButton);
                }
                if (o == checkBoxButton) {
                    CpCheckboxLc checkbox = new CpCheckboxLc("Check Box");
                    checkbox.resize(checkbox.preferredSize().width + 10, checkbox.preferredSize().height);
                    addLcToDiagram(checkbox);
                    selectTool(checkBoxButton);
                }
                if (o == graphicsButton) {
                    CpImageLc image = new CpImageLc(CpToolkit.getImage(ptBarnumId));
                    image.resize(image.preferredSize());
                    addLcToDiagram(image);
                    selectTool(graphicsButton);
                }
                if (o == listButton) {
                    CpListboxLc listbox = new CpListboxLc(false);
                    Vector v = new Vector();
                    v.addElement("Item 1");
                    v.addElement("Item 2");
                    v.addElement("Item 3");
                    listbox.setItems(v);
                    listbox.resize(100, 100);
                    addLcToDiagram(listbox);
                    selectTool(listButton);
                }
                if (o == vscrollButton) {
                    CpScrollbarLc vbar = new CpScrollbarLc(CpScrollbarLc.VERTICAL);
                    vbar.resize(16, 120);
                    addLcToDiagram(vbar);
                    selectTool(vscrollButton);
                }
                if (o == gridButton) {
                    CpListboxLc grid = new CpListboxLc();
                    Vector v = new Vector();
                    v.addElement(" ");
                    grid.setItems(v);
                    grid.resize(100, 100);
                    addLcToDiagram(grid);
                    selectTool(gridButton);
                }
                break;
            }
            case CpEvent.DIAGRAM_SELECTION_CHANGED : {
                // Set the status lines, reconfigure menu dimming based on mult/sing select, etc.
                Vector selectedGos = (Vector)arg;
                statusText.setText(getStatusText());
                if (!selectedGos.isEmpty()) {
                    CpHasProperties lc = (CpHasProperties)selectedGos.firstElement();
                    if (lc instanceof CpOrgChartEmployeeLc) {
//                        propertySheet.setModel(lc);
                        propertySheet.setPropertiesAndModel(lc.getProperties(), lc);
                    } else {
                        propertySheet.setPropertiesAndModel(new Vector(), null);
                    }
                } else {
                    propertySheet.setPropertiesAndModel(new Vector(), null);
                }

                toolbar.enableDamageRepair(false);
                cutButton.setIsDisabled(diagram.getSelectedLcs().isEmpty());
                copyButton.setIsDisabled(diagram.getSelectedLcs().isEmpty());

                boolean multipleSelection = selectedGos.size() <= 1;
                alignLeftButton.setIsDisabled(multipleSelection);
                alignRightButton.setIsDisabled(multipleSelection);
                alignCenterHButton.setIsDisabled(multipleSelection);
                alignTopButton.setIsDisabled(multipleSelection);
                alignBottomButton.setIsDisabled(multipleSelection);
                alignCenterVButton.setIsDisabled(multipleSelection);
                matchSizeHButton.setIsDisabled(multipleSelection);
                matchSizeVButton.setIsDisabled(multipleSelection);
                matchSizeBButton.setIsDisabled(multipleSelection);

                distributeHButton.setIsDisabled(selectedGos.size() <= 2);
                distributeVButton.setIsDisabled(selectedGos.size() <= 2);
                toolbar.enableDamageRepair(true);
                toolbar.repairDamage();
                break;
            }
            case CpEvent.DIAGRAM_UNLOADED_CURSOR : {
                selectTool(arrowButton);
                break;
            }
            // Others MOVE_OR_SIZE?, MOUSE_MOVE?
        }
    }

    public void addLcToDiagram(CpLightweightComponent lc)
    {
        lc.setBackground(Color.lightGray);
        CpDiagrammableWrapperLc wrapper = new CpDiagrammableWrapperLc(lc);
        wrapper.resize(lc.size());
        diagram.menuAdd(wrapper);
    }

    public void selectTool(CpToolBarTwoStateButtonLc  button)
    {
        button.setState(true);
        for (int i = 0; i < palette.getChildren().size(); i++) {
            CpToolBarTwoStateButtonLc b = (CpToolBarTwoStateButtonLc)palette.getChildren().elementAt(i);
            if (b != button) {
                b.setState(false);
            }
        }
    }
}

