
import java.awt.*;
import java.io.*;
import java.util.*;
import cnp.ew.scrolling.*;
import cnp.ew.diagram.*;
import cnp.ew.button.*;
import cnp.ew.grid.*;
import cnp.ew.layout.*;
import cnp.ew.lightweight.*;
import cnp.ew.charts.*;
import java.applet.*;
import cnp.ew.misc.*;
import cnp.ew.displayer.*;
import cnp.ew.text.*;
import cnp.ew.util.*;
import cnp.ew.properties.*;
import cnp.ew.list.*;
import cnp.ew.kdemo.*;

public class CpChartDemo extends CpLcApplet
{

    CpDiagram diagram;
    CpDropDownListLc chartTypeChoice;
    CpChartDataModel dataModel;
    CpAbstractChartLc currentChartLc;
    CpReadOnlyTextAreaLc label, chartTitleLabel, categoriesTitleLabel, seriesTitleLabel;
    CpEntryFieldLc chartTitle, categoriesTitle, seriesTitle;
    CpPanelLc panelLc;
    CpChartGridAdapter adapter;

	public void init () {

        CpAttachments attachments;

        super.init();

        CpAttachmentsLayout layout = new CpAttachmentsLayout();
        setLc(panelLc = new CpPanelLc());
        panelLc.setDrawOffscreen(true);
        panelLc.setLayout(layout);

        label = new CpReadOnlyTextAreaLc("Chart Type");
        attachments = new CpAttachments(30, 10, -1, -1);
        layout.setAttachments(label, attachments);
        panelLc.add(label);

        chartTypeChoice = new CpDropDownListLc();
        chartTypeChoice.addItem("Bar Chart");
        chartTypeChoice.addItem("3D Stacked Column Chart");
        chartTypeChoice.addItem("Line Chart");
        chartTypeChoice.addItem("3D Pie Chart");
        chartTypeChoice.addItem("Pie Chart");
        chartTypeChoice.setBackground(Color.white);
        chartTypeChoice.addObserver(this);
        chartTypeChoice.setIsTabStop(true);
        attachments = new CpAttachments();
        attachments.setLeftAttachment(4, label);
        attachments.setVerticalCentering(label);
        layout.setAttachments(chartTypeChoice, attachments);
        panelLc.add(chartTypeChoice);

        chartTitleLabel = new CpReadOnlyTextAreaLc("Chart Title");
        attachments = new CpAttachments();
        attachments.setLeftAttachment(8, chartTypeChoice);
        attachments.setVerticalCentering(label);
        layout.setAttachments(chartTitleLabel, attachments);
        panelLc.add(chartTitleLabel);

        chartTitle = new CpEntryFieldLc();
        chartTitle.setBorderStyle(CpAbstractLc.BORDER_LINE);
        chartTitle.setWantsCr(true);
        chartTitle.setBorderMargin(2);
        chartTitle.setBackground(Color.white);
        chartTitle.addObserver(this);
        chartTitle.setIsTabStop(true);
        chartTitle.setSelectAllOnGettingFocus(true);
        attachments = new CpAttachments();
        attachments.setLeftAttachment(4, chartTitleLabel);
        attachments.setVerticalCentering(label);
        attachments.setWidth(200);
        layout.setAttachments(chartTitle, attachments);
        panelLc.add(chartTitle);

        categoriesTitleLabel = new CpReadOnlyTextAreaLc("Categories");
        attachments = new CpAttachments();
        attachments.setRightAttachment(CpAttachments.ATTACH_SIBLING_OPPOSITE, 0, label);
        attachments.setTopAttachment(16, label);
        layout.setAttachments(categoriesTitleLabel, attachments);
        panelLc.add(categoriesTitleLabel);

        categoriesTitle = new CpEntryFieldLc();
        categoriesTitle.setWantsCr(true);
        categoriesTitle.setBorderStyle(CpAbstractLc.BORDER_LINE);
        categoriesTitle.setBorderMargin(2);
        categoriesTitle.setBackground(Color.white);
        categoriesTitle.addObserver(this);
        categoriesTitle.setIsTabStop(true);
        categoriesTitle.setSelectAllOnGettingFocus(true);
        attachments = new CpAttachments();
        attachments.setLeftAttachment(CpAttachments.ATTACH_SIBLING_OPPOSITE, 0, chartTypeChoice);
        attachments.setRightAttachment(CpAttachments.ATTACH_SIBLING_OPPOSITE, 0, chartTypeChoice);
        attachments.setVerticalCentering(categoriesTitleLabel);
        layout.setAttachments(categoriesTitle, attachments);
        panelLc.add(categoriesTitle);

        seriesTitleLabel = new CpReadOnlyTextAreaLc("Series");
        attachments = new CpAttachments();
        attachments.setRightAttachment(CpAttachments.ATTACH_SIBLING_OPPOSITE, 0, chartTitleLabel);
        attachments.setVerticalCentering(categoriesTitleLabel);
        layout.setAttachments(seriesTitleLabel, attachments);
        panelLc.add(seriesTitleLabel);

        seriesTitle = new CpEntryFieldLc();
        seriesTitle.setWantsCr(true);
        seriesTitle.setBorderStyle(CpAbstractLc.BORDER_LINE);
        seriesTitle.setBorderMargin(2);
        seriesTitle.setBackground(Color.white);
        seriesTitle.addObserver(this);
        seriesTitle.setIsTabStop(true);
        seriesTitle.setSelectAllOnGettingFocus(true);
        attachments = new CpAttachments();
        attachments.setLeftAttachment(CpAttachments.ATTACH_SIBLING_OPPOSITE, 0, chartTitle);
        attachments.setRightAttachment(CpAttachments.ATTACH_SIBLING_OPPOSITE, 0, chartTitle);
        attachments.setVerticalCentering(categoriesTitleLabel);
        layout.setAttachments(seriesTitle, attachments);
        panelLc.add(seriesTitle);


        CpGridLc grid = CpDemoExamples.basicCreateExampleSpreadSheet(false, false);
        panelLc.add(grid);
        grid.setIsTabStop(true);
        attachments = new CpAttachments(10, -1, 10, 10);
        attachments.setTopAttachment(CpAttachments.ATTACH_SELF, 140);
        layout.setAttachments(grid, attachments);


        CpDiagramLc diagramFrame = new CpDiagramLc(false, false);
        diagram = diagramFrame.getDiagram();
        panelLc.add(diagramFrame);
        attachments = new CpAttachments(10, -1, 10, -1);
        attachments.setTopAttachment(10, seriesTitle);
        attachments.setBottomAttachment(10, grid);
        layout.setAttachments(diagramFrame, attachments);

        createDataModel();
        adapter = new CpChartGridAdapter(grid.getGridPane(), dataModel);
        addLc(new CpBarChartLc());
        addLegend();
        addChartTitle();

        setBackground(Color.lightGray);
        label.setBackground(Color.lightGray);
        layout();
	}


	void createDataModel()
	{
	    dataModel = CpDemoExamples.createExampleChartModel();
        chartTitle.setText(dataModel.getChartLabel());
        categoriesTitle.setText(dataModel.getCategoriesLabel());
        seriesTitle.setText(dataModel.getSeriesLabel());
    }


    public void update(CpObservable target, int facet, Object arg)
    {
        if (facet == OBJECT_CHANGED) {
            if (target == chartTypeChoice) {
                addChartType(chartTypeChoice.getSelectedIndex());
            }
            if (target == chartTitle) {
                diagram.selectLcs(new Vector());
                dataModel.setChartLabel(chartTitle.getText());
            }
            if (target == categoriesTitle) {
                dataModel.setCategoriesLabel(categoriesTitle.getText());
            }
            if (target == seriesTitle) {
                dataModel.setSeriesLabel(seriesTitle.getText());
            }
            if (panelLc != null) {
                panelLc.repairDamage();
            }
        }
    }

    void addChartType(int type)
    {
        panelLc.enableDamageRepair(false);
        currentChartLc.remove();

        switch (type) {
        case 0:
            addLc(new CpBarChartLc());
            break;
        case 1:
            addLc(new Cp3DBarChartLc());
            break;
        case 2:
            addLc(new CpLineChartLc());
            break;
        case 3:
            addLc(new Cp3DPieChartLc());
            break;
        case 4:
            addLc(new CpPieChartLc());
            break;
        }
        panelLc.enableDamageRepair(true);
        panelLc.repairDamage();
    }

    void addLegend()
    {
        CpLegendLc legendLc = new CpLegendLc();
        legendLc.setChartDataModel(dataModel);
        diagram.add(legendLc);
        legendLc.reshape(375, 59, 156, 73);
    }

    void addChartTitle()
    {
        CpChartTitleLc titleLc = new CpChartTitleLc();
        titleLc.setChartDataModel(dataModel);
        diagram.add(titleLc);
        Dimension preferredSize = titleLc.preferredSize();
        titleLc.reshape(191, 11, preferredSize.width, preferredSize.height);
    }

    void addLc(CpAbstractChartLc newLc)
    {
        Rectangle bounds;
        if (currentChartLc != null) {
            bounds = currentChartLc.bounds();
        } else {
            bounds = new Rectangle(28, 58, 306, 145);
        }
        diagram.selectLcs(new Vector());
        currentChartLc = newLc;
        currentChartLc.setChartDataModel(dataModel);
        newLc.addObserver(adapter);
        diagram.add(currentChartLc);
        currentChartLc.reshape(bounds.x, bounds.y, bounds.width, bounds.height);
        currentChartLc.sendToBack();
    }
}

