import java.awt.*;
import java.io.*;
import java.applet.*;
import java.util.*;

import cnp.ew.richtext.*;
import cnp.ew.util.*;
import cnp.ew.lightweight.*;
import cnp.ew.layout.*;
import cnp.ew.misc.*;
import cnp.ew.list.*;


public class CpJavaEditor extends CpLcApplet
{
    CpListboxLc listbox;
    CpRichTextEditorLc richtext;
    CpRichTextPane richpane;

    String selectedMethod;
    Hashtable methods;

    public void init()
    {
        Vector items;
        CpAttachments attachments;
        CpAttachmentsLayout layout;
        CpPanelLc mainPanelLc;
        CpSplitbarLc hSplitbar;

	    super.init();

	    setLc(mainPanelLc = new CpPanelLc());
	    mainPanelLc.setLayout(layout = new CpAttachmentsLayout());
	    mainPanelLc.setDrawOffscreen(true);
	    mainPanelLc.setBackground(Color.lightGray);
	    mainPanelLc.setBorderMargin(5);

        listbox = new CpListboxLc(false);
        listbox.addObserver(this);
        mainPanelLc.add(listbox);

        richtext = new CpRichTextEditorLc();
        richtext.setDrawOffscreen(true);
    	richpane = richtext.getPane();
    	richpane.setJavaMode(true);
    	richpane.setWrapWidth(2000);
    	richpane.setFont(new Font("Courier", Font.PLAIN, 12));
        richpane.addObserver(this);
        mainPanelLc.add(richtext);

        hSplitbar = new CpSplitbarLc(CpSplitbarLc.HORIZONTAL);
        hSplitbar.setMinComponent(listbox);
        hSplitbar.setMaxComponent(richtext);
        mainPanelLc.add(hSplitbar);

        attachments = new CpAttachments();
        attachments.setLeftAttachment(0);
        attachments.setRightAttachment(0);
        attachments.setTopAttachment(CpAttachments.ATTACH_POSITION, 30);
        layout.setAttachments(hSplitbar, attachments);
        hSplitbar.setAttachments(attachments);

        attachments = new CpAttachments();
        attachments.setLeftAttachment(0);
        attachments.setRightAttachment(0);
        attachments.setTopAttachment(0);
        attachments.setBottomAttachment(0, hSplitbar);
        layout.setAttachments(listbox, attachments);

        attachments = new CpAttachments();
        attachments.setLeftAttachment(0);
        attachments.setRightAttachment(0);
        attachments.setTopAttachment(0, hSplitbar);
        attachments.setBottomAttachment(0);
        layout.setAttachments(richtext, attachments);


        selectedMethod = "public void init()";
        methods = new Hashtable();

        Vector paragraphs = new Vector();
        paragraphs.addElement(new CpParagraph("public void init()"));
	    paragraphs.addElement(new CpParagraph("{"));
	    paragraphs.addElement(new CpParagraph("    /**"));
	    paragraphs.addElement(new CpParagraph("    * Generally set up the application.  Initialize the MediaTracker, size the"));
	    paragraphs.addElement(new CpParagraph("    * applet viewing space, initialize the graphic context and initialize"));
	    paragraphs.addElement(new CpParagraph("    * the parameters of the scrolling text string."));
	    paragraphs.addElement(new CpParagraph("    */"));
	    paragraphs.addElement(new CpParagraph(""));
	    paragraphs.addElement(new CpParagraph("    mTracker = new MediaTracker(this);"));
	    paragraphs.addElement(new CpParagraph(""));
	    paragraphs.addElement(new CpParagraph("    bannerImage = createImage(maxWidth, bannerHeight);"));
	    paragraphs.addElement(new CpParagraph("    bannerGraphic = bannerImage.getGraphics();"));
	    paragraphs.addElement(new CpParagraph(""));
	    paragraphs.addElement(new CpParagraph("    textImage = createImage(maxWidth, bannerHeight);"));
	    paragraphs.addElement(new CpParagraph("    textGC = textImage.getGraphics();"));
	    paragraphs.addElement(new CpParagraph(""));
	    paragraphs.addElement(new CpParagraph("    offScrImage = createImage(maxWidth, maxHeight);"));
	    paragraphs.addElement(new CpParagraph("    offScrGC = offScrImage.getGraphics();"));
	    paragraphs.addElement(new CpParagraph(""));
	    paragraphs.addElement(new CpParagraph("    createBannerParams();"));
	    paragraphs.addElement(new CpParagraph(""));
	    paragraphs.addElement(new CpParagraph("    //Set the start box rectangle..."));
	    paragraphs.addElement(new CpParagraph("    d = new Dimension();"));
	    paragraphs.addElement(new CpParagraph("    d = size();"));
	    paragraphs.addElement(new CpParagraph("    rect = new Rectangle((d.width-100)/2, d.height-10, 100, 7);"));
	    paragraphs.addElement(new CpParagraph(""));
	    paragraphs.addElement(new CpParagraph("    //Set the font of the start box..."));
	    paragraphs.addElement(new CpParagraph("    boxFont = new Font(" + '"' + "Helvetica" + '"' + ", Font.BOLD, 9);"));
	    paragraphs.addElement(new CpParagraph("    setFont(boxFont);"));
	    paragraphs.addElement(new CpParagraph("}"));
        methods.put("public void init()", paragraphs);

        paragraphs = new Vector();
        paragraphs.addElement(new CpParagraph("public void start()"));
	    paragraphs.addElement(new CpParagraph("{"));
	    paragraphs.addElement(new CpParagraph("    /**"));
	    paragraphs.addElement(new CpParagraph("    * Start the main applet thread."));
	    paragraphs.addElement(new CpParagraph("    */"));
	    paragraphs.addElement(new CpParagraph(""));
	    paragraphs.addElement(new CpParagraph("    if(engine == null)"));
	    paragraphs.addElement(new CpParagraph("    {"));
	    paragraphs.addElement(new CpParagraph("        engine = new Thread(this);"));
	    paragraphs.addElement(new CpParagraph("        engine.start();"));
	    paragraphs.addElement(new CpParagraph("    }"));
	    paragraphs.addElement(new CpParagraph(""));
	    paragraphs.addElement(new CpParagraph("    getAnimImages();"));
	    paragraphs.addElement(new CpParagraph("}"));
        methods.put("public void start()", paragraphs);

        paragraphs = new Vector();
        paragraphs.addElement(new CpParagraph("private boolean getAnimImages()"));
	    paragraphs.addElement(new CpParagraph("{"));
	    paragraphs.addElement(new CpParagraph("    //Use the first frame of the animation as the starting image"));
	    paragraphs.addElement(new CpParagraph("    try {"));
	    paragraphs.addElement(new CpParagraph("        im[i] = getImage(getDocumentBase(), \"FN\"+i+\".gif\");"));
	    paragraphs.addElement(new CpParagraph("    } catch(Exception e2) {"));
	    paragraphs.addElement(new CpParagraph("        System.out.println(e1);"));
	    paragraphs.addElement(new CpParagraph("    }"));
	    paragraphs.addElement(new CpParagraph("    "));
	    paragraphs.addElement(new CpParagraph("    for(int i = 0; i < 15; i++)"));
	    paragraphs.addElement(new CpParagraph("    {"));
	    paragraphs.addElement(new CpParagraph("    try {"));
	    paragraphs.addElement(new CpParagraph("        im[i] = getImage(getDocumentBase(), \"FN\"+i+\".gif\");"));
	    paragraphs.addElement(new CpParagraph("    } catch(Exception e2) {"));
	    paragraphs.addElement(new CpParagraph("        System.out.println(e2);"));
	    paragraphs.addElement(new CpParagraph("    }"));
	    paragraphs.addElement(new CpParagraph("    "));
	    paragraphs.addElement(new CpParagraph("    mTracker.addImage(im[i], i);"));
	    paragraphs.addElement(new CpParagraph("    "));
	    paragraphs.addElement(new CpParagraph("    try {"));
	    paragraphs.addElement(new CpParagraph("        mTracker.checkID(i, true);"));
	    paragraphs.addElement(new CpParagraph("        mTracker.waitForID(i);"));
	    paragraphs.addElement(new CpParagraph("        showStatus(\"Loading image FN\" + i + \".gif\");"));
	    paragraphs.addElement(new CpParagraph("    }  catch(Exception e3) {"));
	    paragraphs.addElement(new CpParagraph("        System.out.println(e3);"));
	    paragraphs.addElement(new CpParagraph("    }"));
	    paragraphs.addElement(new CpParagraph("    "));
        paragraphs.addElement(new CpParagraph("    //Set the blinking images"));
        paragraphs.addElement(new CpParagraph("    imPic[0] = im[13]; //final animation first frame"));
        paragraphs.addElement(new CpParagraph("    imPic[1] = im[14]; //final animation second frame"));
        paragraphs.addElement(new CpParagraph("    "));
        paragraphs.addElement(new CpParagraph("    return true;"));
	    paragraphs.addElement(new CpParagraph("}"));
        methods.put("private boolean getAnimImages()", paragraphs);

        paragraphs = new Vector();
        paragraphs.addElement(new CpParagraph("public synchronized void update(Graphics g)"));
        paragraphs.addElement(new CpParagraph("{"));
        paragraphs.addElement(new CpParagraph("    /**"));
        paragraphs.addElement(new CpParagraph("     * Call the paint function"));
        paragraphs.addElement(new CpParagraph("     */"));
        paragraphs.addElement(new CpParagraph("     "));
        paragraphs.addElement(new CpParagraph("    paint(g);"));
        paragraphs.addElement(new CpParagraph("}"));
        methods.put("public synchronized void update(Graphics g)", paragraphs);

        paragraphs = new Vector();
        paragraphs.addElement(new CpParagraph("public boolean mouseDown(java.awt.Event evt, int x, int y)"));
        paragraphs.addElement(new CpParagraph("{"));
        paragraphs.addElement(new CpParagraph("    /**"));
        paragraphs.addElement(new CpParagraph("     * When the user clicks anywhere on the applet screen, the \"go\" variable"));
        paragraphs.addElement(new CpParagraph("     * is set and the animation begins."));
        paragraphs.addElement(new CpParagraph("     * @param go  A boolean indicator which, when true, signals the animation to begin."));
        paragraphs.addElement(new CpParagraph("     * @param x   The x-location of the mouse when the user clicks - not used."));
        paragraphs.addElement(new CpParagraph("     * @param y   The y-location of the mouse when the user clicks - not used."));
        paragraphs.addElement(new CpParagraph("     * @param evt The event object generated when the mouse was clicked - not used."));
        paragraphs.addElement(new CpParagraph("     */"));
        paragraphs.addElement(new CpParagraph("     "));
        paragraphs.addElement(new CpParagraph("    if(rect.inside(x, y) == true) {"));
        paragraphs.addElement(new CpParagraph("        if(!go) {"));
        paragraphs.addElement(new CpParagraph("            go = true;"));
        paragraphs.addElement(new CpParagraph("        }"));
        paragraphs.addElement(new CpParagraph("    }"));
        paragraphs.addElement(new CpParagraph("    "));
        paragraphs.addElement(new CpParagraph("    return true;"));
        paragraphs.addElement(new CpParagraph("}"));
        methods.put("public boolean mouseDown(java.awt.Event evt, int x, int y)", paragraphs);

        paragraphs = new Vector();

        paragraphs.addElement(new CpParagraph("public synchronized void nextPos()"));
        paragraphs.addElement(new CpParagraph("{"));
        paragraphs.addElement(new CpParagraph("    /**"));
        paragraphs.addElement(new CpParagraph("     * The nextPos method moves the text banner a single \"step\" to the left."));
        paragraphs.addElement(new CpParagraph("     */"));
        paragraphs.addElement(new CpParagraph("     "));
        paragraphs.addElement(new CpParagraph("     msgX -= 10;               //scroll left 10 pixels"));
        paragraphs.addElement(new CpParagraph("     if((msgX + msgW) < 0) {"));
        paragraphs.addElement(new CpParagraph("          msgX = lastS.width;"));
        paragraphs.addElement(new CpParagraph("     }"));
        paragraphs.addElement(new CpParagraph("     repaint();"));
        paragraphs.addElement(new CpParagraph("}"));
        methods.put("public synchronized void nextPos()", paragraphs);

        Vector methodNames = new Vector();
        for (Enumeration e = methods.keys() ; e.hasMoreElements() ;) {
            methodNames.addElement(e.nextElement());
        }
        listbox.setItems(methodNames);


    	fillInRichPane();

        layout();
        listbox.requestFocus();
        listbox.select(0);
	}

    public void fillInRichPane()
    {
        Vector paragraphs = (Vector)methods.get(selectedMethod);
        richpane.setParagraphs(paragraphs);
    }

    public void update(CpObservable o, int facet, Object arg)
    {
        if (o == listbox && facet == OBJECT_CHANGED) {
            selectedMethod = (String)listbox.getSelectedItem();
            fillInRichPane();
        }

    }
}
