import java.awt.*;
import java.io.*;
import java.applet.*;
import java.util.*;

import cnp.ew.richtext.*;
import cnp.ew.image.*;
import cnp.ew.button.*;
import cnp.ew.util.*;
import cnp.ew.lightweight.*;
import cnp.ew.displayer.*;
import cnp.ew.misc.*;
import cnp.ew.tdemo.*;
import cnp.ew.text.*;
import cnp.ew.properties.*;
import cnp.ew.layout.*;
import cnp.ew.list.*;
import cnp.ew.diagram.*;
import cnp.ew.dialog.*;
import cnp.ew.kdemo.*;
import cnp.ew.charts.*;
import cnp.ew.grid.*;


public class CpWordProcessorDemo  extends CpLcApplet
{
    private String fonts[];

    CpWpPanelLc toolBar, rulerBar;
    CpWpPanelLc formatBar;
    CpPanelLc buttons, statusBar;

    boolean showToolBar, showFormatBar, showRulerBar, showStatusBar;
    CpRuler ruler;
	CpDropDownListLc fontCombo, sizeCombo;

    CpRichTextEditorLc richtext;
    CpRichTextPane richpane;

    CpToolBarButtonLc newButton, addGifButton, openButton, saveButton, printButton;
    CpToolBarButtonLc previewButton, findButton, cutButton, copyButton, pasteButton, undoButton, insertDateButton;

    CpToolBarTwoStateButtonLc  colorButton, boldButton, italicButton, underlineButton, leftButton, rightButton, centerButton, bulletButton;

    CpReadOnlyTextAreaLc statusText, statusNumbers;

    CpColorPalette pal, colorPal;

    boolean dontPopup = false;

    CpListChooserDialog lcList;

    static int newId;
    static int openId;
    static int saveId;
    static int printId;
    static int previewId;
    static int findId;
    static int cutId;
    static int copyId;
    static int pasteId;
    static int undoId;
    static int insertDateId;
    static int addGifId;
    static int boldId;
    static int italicId;
    static int underlineId;
    static int colorId;
    static int leftId;
    static int rightId;
    static int centerId;
    static int bulletId;
    static int leftMarginId;
    static int firstIndentId;
    static int rightMarginId;

    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");
         findId = CpToolkit.registerImageName("wpfind.gif");
         cutId = CpToolkit.registerImageName("wpcut.gif");
         copyId = CpToolkit.registerImageName("wpcopy.gif");
         pasteId = CpToolkit.registerImageName("wppaste.gif");
         undoId = CpToolkit.registerImageName("wpundo.gif");
         insertDateId = CpToolkit.registerImageName("wpinsertdate.gif");
         addGifId = CpToolkit.registerImageName("wpaddgif.gif");
         boldId = CpToolkit.registerImageName("wpbold.gif");
         italicId = CpToolkit.registerImageName("wpitalic.gif");
         underlineId = CpToolkit.registerImageName("wpunderline.gif");
         colorId = CpToolkit.registerImageName("wpcolor.gif");
         leftId = CpToolkit.registerImageName("wpleft.gif");
         rightId = CpToolkit.registerImageName("wpright.gif");
         centerId = CpToolkit.registerImageName("wpcenter.gif");
         bulletId = CpToolkit.registerImageName("wpbullet.gif");
         leftMarginId = CpToolkit.registerImageName("wpleftmargin.gif");
         firstIndentId = CpToolkit.registerImageName("wpfirstindent.gif");
         rightMarginId = CpToolkit.registerImageName("wprightmargin.gif");

         ptBarnumId = CpToolkit.registerImageName("ptbarnum.gif");
    }

    public void init()
    {
	   // super("Document - JavaPad");

        super.init();

        setBackground(Color.lightGray);

        CpPanelLc panel;

        setLc(panel = new CpPanelLc());
        panel.setDrawOffscreen(true);

        CpAttachmentsLayout layout = new CpAttachmentsLayout();
        panel.setLayout(layout);

	    showToolBar = showStatusBar = showFormatBar = showRulerBar = true;

    	fonts = getToolkit().getFontList();

        richtext = CpDemoExamples.createExampleRichText();
        richpane = richtext.getPane();

        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);

        createRulerBar(panel);
        attachments = new CpAttachments(10, -1, 10, -1);
        attachments.setTopAttachment(0, formatBar);
        attachments.setHeight(33);
        layout.setAttachments(rulerBar, attachments);

        createStatusBar();
        attachments = new CpAttachments(13, -1, 10, 10);
        attachments.setHeight(20);
        layout.setAttachments(statusBar, attachments);
        panel.add(statusBar);

        richpane.addObserver(this);
        attachments = new CpAttachments(10, -1, 10, -1);
        attachments.setTopAttachment(0, rulerBar);
        attachments.setBottomAttachment(0, statusBar);
        layout.setAttachments(richtext, attachments);
        panel.add(richtext);

        layout();
        richpane.requestFocus();
        update(richpane, CpRichTextPane.CURSOR_MOVED, new Boolean(true));
	}

    void createToolBar(CpPanelLc panel)
    {
        toolBar = new CpWpPanelLc(321, true);
        panel.add(toolBar);

        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);

        findButton = new CpToolBarButtonLc(CpToolkit.getImage(findId), "Find", "Finds the specified text");
        findButton.reshape(137, 5, 23, 22);
        toolBar.add(findButton);
        findButton.addObserver(this);

        cutButton = new CpToolBarButtonLc(CpToolkit.getImage(cutId), "Cut", "Cuts the selection and puts it on the clipboard");
        cutButton.reshape(168, 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(191, 5, 23, 22);
        toolBar.add(copyButton);
        copyButton.addObserver(this);

        pasteButton = new CpToolBarButtonLc(CpToolkit.getImage(pasteId), "Paste", "Inserts Clipboard contents");
        pasteButton.reshape(214, 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(237, 5, 23, 22);
        toolBar.add(undoButton);
        undoButton.addObserver(this);

        insertDateButton = new CpToolBarButtonLc(CpToolkit.getImage(insertDateId), "Insert Date", "Inserts today's date");
        insertDateButton.reshape(268, 5, 23, 22);
        toolBar.add(insertDateButton);
        insertDateButton.addObserver(this);

        addGifButton = new CpToolBarButtonLc(CpToolkit.getImage(addGifId), "Insert Object", "Inserts an object");
        addGifButton.reshape(291, 5, 23, 22);
        toolBar.add(addGifButton);
        addGifButton.addObserver(this);
    }

    void createFormatBar(CpPanelLc panel)
    {
        formatBar = new CpWpPanelLc(518, false);
        panel.add(formatBar);

        CpAttachmentsLayout layout = new CpAttachmentsLayout();
        formatBar.setLayout(layout);

        fontCombo = new CpDropDownListLc();
    	fonts = getToolkit().getFontList();
    	for (int i = 0; i < fonts.length; i++)
    	{
    	    if (!fonts[i].equals("Dialog") && !fonts[i].equals("DialogInput")) {
        	    fontCombo.addItem(fonts[i]);
        	}
    	}
        CpAttachments attachments = new CpAttachments(5, 5, -1, -1);
        attachments.setWidth(233);
        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(50);
        layout.setAttachments(sizeCombo, attachments);
        formatBar.add(sizeCombo);
        sizeCombo.addObserver(this);


        boldButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(boldId), "Bold", "Makes the selection bold (toggle).");
        boldButton.reshape(312, 5, 23, 22);
        formatBar.add(boldButton);
        boldButton.addObserver(this);

        italicButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(italicId), "Italic", "Makes the selection italic (toggle)");
        italicButton.reshape(335, 5, 23, 22);
        formatBar.add(italicButton);
        italicButton.addObserver(this);

        underlineButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(underlineId), "Underline", "Formats the selection with a continuous underline (toggle)");
        underlineButton.reshape(358, 5, 23, 22);
        formatBar.add(underlineButton);
        underlineButton.addObserver(this);

        colorButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(colorId), "Set Color", "Formats the selection with a color");
        colorButton.reshape(381, 5, 23, 22);
        formatBar.add(colorButton);
        colorButton.addObserver(this);

        leftButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(leftId), "Align Left", "Left-justifies paragraph");
        leftButton.reshape(412, 5, 23, 22);
        formatBar.add(leftButton);
        leftButton.addObserver(this);

        centerButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(centerId), "Center", "Center-justifies paragraph");
        centerButton.reshape(435, 5, 23, 22);
        formatBar.add(centerButton);
        centerButton.addObserver(this);

        rightButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(rightId), "Align Right", "Right-justifies paragraph");
        rightButton.reshape(458, 5, 23, 22);
        formatBar.add(rightButton);
        rightButton.addObserver(this);

        bulletButton = new CpToolBarTwoStateButtonLc(CpToolkit.getImage(bulletId), "Bullets", "Inserts a bullet on this line");
        bulletButton.reshape(489, 5, 23, 22);
        formatBar.add(bulletButton);
        bulletButton.addObserver(this);
    }

    void createRulerBar(CpPanelLc panel)
    {
        rulerBar = new CpWpPanelLc(0, true);
        panel.add(rulerBar);

	    ruler = new CpRuler(richpane);
	    ruler.reshape(4, 3, 10000, 25);
	    rulerBar.add(ruler);
	}

    void createStatusBar()
    {
        statusBar = new CpPanelLc();

        CpAttachmentsLayout layout = new CpAttachmentsLayout();
        statusBar.setLayout(layout);

        statusBar.add(statusNumbers = new CpReadOnlyTextAreaLc(""));
        CpAttachments attachments = new CpAttachments(-1, 1, 0, 0);
        attachments.setWidth(150);
        layout.setAttachments(statusNumbers, attachments);
        statusNumbers.setBorderMargin(2, 0);
        statusNumbers.setBorderStyle(statusNumbers.BORDER_LIGHTINWARD3D);
        statusNumbers.setVerticalAlignment(statusNumbers.ALIGN_CENTER);

        statusBar.add(statusText = new CpReadOnlyTextAreaLc(" "));
        attachments = new CpAttachments(0, 2, 150, -1);
     //   attachments.setRightAttachment(0, statusNumbers);
        layout.setAttachments(statusText, attachments);
        statusBar.setFont(CpFonts.dialogPlainTen());
        statusText.setVerticalAlignment(statusText.ALIGN_CENTER);
	}

    void updateJustificationButtons()
    {
        CpParagraph para = richpane.getCurrentParagraph();
        leftButton.setState(para.getHorizontalAlignment() == CpParagraph.LEFT);
        rightButton.setState(para.getHorizontalAlignment() == CpParagraph.RIGHT);
        centerButton.setState(para.getHorizontalAlignment() == CpParagraph.CENTER);
    }

    public void update(CpObservable o, int facet, Object arg)
    {
        if (facet == CpRichTextPane.CURSOR_MOVED) {
            cutButton.setIsDisabled(!richpane.isAnySelection());
            copyButton.setIsDisabled(!richpane.isAnySelection());
            statusNumbers.setText("Paragraph " + (richpane.getParagraphNum() + 1) + ", Line " + (richpane.getLineNum() + 1));
            if (((Boolean)arg).booleanValue()) {  // arg says whether the para or format has changed
                toolBar.enableDamageRepair(false);
                CpCharFormat format = richpane.getCurrentFormat();
                boldButton.setState(format.isBold());
                underlineButton.setState(format.isUnderline());
                italicButton.setState(format.isItalic());
                fontCombo.select(format.getFontName());
                sizeCombo.select("" + format.getPointSize());
                updateJustificationButtons();
                CpParagraph para = richpane.getCurrentParagraph();
                bulletButton.setState(para.getBulletMode());
                toolBar.enableDamageRepair(true);
                toolBar.repairDamage();
            }
        }
        if (facet == CpEvent.HELP_TEXT_CHANGED) {
            if (((String)arg).equals("")) {
                statusText.setText("For Help, press F1");
            } else {
                statusText.setText((String)arg);
            }
        }
        if (facet == CpEvent.TEXT_FIND) {
            if (!richpane.menuFind((String)arg)) {
                (new CpMessageDialog(getFrame(), "JavaPad", "JavaPad has finished searching the document.")).show();
            }
        }
        if (facet == CpEvent.BUTTON_CLICKED) {
            if (o == newButton) {
                richpane.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();
//                    richpane.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();
//                    richpane.saveDocument(this);
            }
            */
            if (o == cutButton) {
                richpane.menuCut();
                pasteButton.setIsDisabled(false);
            }
            if (o == copyButton) {
                richpane.menuCopy();
                pasteButton.setIsDisabled(false);
            }
            if (o == pasteButton) {
                richpane.menuPaste();
            }
            if (o == insertDateButton) {
                richpane.menuInsertDate();
            }
            if (o == findButton) {
                CpFindTextDialog d;
                d = new CpFindTextDialog(this);
                d.show();
            }
            if (o == boldButton) {
                if (boldButton.getState()) {
    	            richpane.mergeFormat(CpCharFormat.getBold());
    	        } else {
    	            CpCharFormat format = new CpCharFormat();
    	            format.setBold(false);
    	            richpane.mergeFormat(format);
    	        }
            }
            if (o == underlineButton) {
                if (underlineButton.getState()) {
    	            richpane.mergeFormat(CpCharFormat.getUnderline());
    	        } else {
    	            CpCharFormat format = new CpCharFormat();
    	            format.setUnderline(false);
    	            richpane.mergeFormat(format);
    	        }
            }
            if (o == italicButton) {
                if (italicButton.getState()) {
    	            richpane.mergeFormat(CpCharFormat.getItalic());
    	        } else {
    	            CpCharFormat format = new CpCharFormat();
    	            format.setItalic(false);
    	            richpane.mergeFormat(format);
    	        }
            }
         /*   if (o == addGifButton) {
                int anId = CpToolkit.registerImageName("envelope.gif");
                Image gif = CpToolkit.getImage(anId);
                richpane.insertImage(gif);
            } */
            if (o == colorButton) {
                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.popUpLcAt(pal, new Rectangle(0, colorButton.size().height, pal.preferredSize().width, pal.preferredSize().height));
                    pal.requestFocus();
                }
            }
            if (o == leftButton) {
                richpane.setHorizontalAlignment(CpParagraph.LEFT);
                updateJustificationButtons();
            }
            if (o == centerButton) {
                richpane.setHorizontalAlignment(CpParagraph.CENTER);
                updateJustificationButtons();
            }
            if (o == rightButton) {
                richpane.setHorizontalAlignment(CpParagraph.RIGHT);
                updateJustificationButtons();
            }
            if (o == bulletButton) {
                richpane.setBulletMode(bulletButton.getState());
            }
            if (o == addGifButton) {
              Vector choices = new Vector();
              choices.addElement("Chart");
              choices.addElement("Spreadsheet");
		      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 word processor.", choices);
    	        lcList.addObserver(this);
    	        lcList.show();
            }
        }
        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 (facet == OBJECT_CHANGED) {
            if (o == pal) {
                CpCharFormat format = new CpCharFormat();
           	    format.setColor((Color)arg);
                richpane.mergeFormat(format);
            }
            if (o == lcList) {
           	    String lcName = (String)arg;
               	if (lcName.equals("Image")) {
               	    richpane.insertImage(CpToolkit.getImage(ptBarnumId));
               	}
           	    if (lcName.equals("Button")) {
                    CpLabeledImageButtonLc example = new CpLabeledImageButtonLc("Howdy");
                    example.resize(100, 50);
                    example.setBackground(Color.lightGray);
           	        richpane.insertLc(example);
           	    }
           	    if (lcName.equals("Diagram")) {
           	        richpane.insertLc(CpDiagramLc.example());
           	    }
           	    if (lcName.equals("Chart")) {
           	        CpAbstractLc chart = CpDemoExamples.createExampleChart();
           	        chart.resize(200, 150);
           	        richpane.insertLc(chart);
           	    }
           	    if (lcName.equals("Spreadsheet")) {
           	        CpGridLc spreadsheet = CpDemoExamples.basicCreateExampleSpreadSheet(true, true);
                    new CpChartGridAdapter(spreadsheet.getGridPane(), CpDemoExamples.createExampleChartModel());
           	        spreadsheet.resize(302, 150);
           	        richpane.insertLc(spreadsheet);
           	    }
           	}
            if (o == fontCombo) {
                CpCharFormat format = new CpCharFormat();
                format.setFontName(fontCombo.getSelectedItem());
                richpane.mergeFormat(format);
                richpane.requestFocus();
            }
            if (o == sizeCombo) {
                CpCharFormat format = new CpCharFormat();
                format.setPointSize((new Integer(sizeCombo.getSelectedItem())).intValue());
                richpane.mergeFormat(format);
                richpane.requestFocus();
            }
        }
    }


/*
    public synchronized void layout()
    {
        super.layout();

        if (bigone != null) {
            bigone.reshape(0, 0, size().width, size().height);
        }

        Insets insets = new Insets(10, 10, 10, 10);

        int w = size().width - insets.left - insets.right;
        int h = size().height - insets.top - insets.bottom;

        int toolBarHeight = 0;
        if (showToolBar) {
            toolBar.reshape(insets.left, insets.top, w, 30);
            toolBarHeight = toolBar.size().height;
        }
        int formatBarHeight = 0;
        if (showFormatBar) {
            formatBar.reshape(insets.left, insets.top + 30, w, 30);
            formatBarHeight = formatBar.size().height;
            fontCombo.reshape(23, 45, 233, 21);
            sizeCombo.reshape(262, 45, 50, 21);
        }
        int rulerBarHeight = 0;
        if (showRulerBar) {
            rulerBar.reshape(insets.left + 5, insets.top + 60, w, 28);
            rulerBarHeight = rulerBar.size().height;
        }
        int statusBarHeight = 0;
        if (showStatusBar) {
            statusBar.reshape(insets.left, insets.top + h - 17, w, 17);
            statusBarHeight = statusBar.size().height;
            statusText.reshape(0, 0, statusBar.size().width - 150, statusBar.size().height);
            statusNumbers.reshape(statusBar.size().width - 147, 1, 147, statusBar.size().height - 1);
        }

        if (richtext != null) {
            richtext.reshape(insets.left, insets.top + toolBarHeight + formatBarHeight + rulerBarHeight, w, h - toolBarHeight - formatBarHeight - rulerBarHeight - statusBarHeight);
        }
	}
*/
	public boolean handleEvent (Event evt) {

		switch(evt.id) {
		    case Event.WINDOW_DESTROY: {
		        hide();
		    //    dispose();
			//    System.exit(0);
            }
		}
		return super.handleEvent(evt);
	}
}
