import java.awt.*;
import java.util.*;
import java.io.*;
import	cnp.ew.misc.*;
import cnp.ew.util.*;
import cnp.ew.displayer.*;
import cnp.ew.spin.*;
import cnp.ew.misc.*;
import cnp.ew.lightweight.*;
import cnp.ew.image.*;
import cnp.ew.layout.*;
import cnp.ew.text.*;
import cnp.ew.button.*;

public class CpPackedImageBuilder extends CpLcApplet implements CpObserver
{
    CpEntryFieldLc pathEntryField;
    CpLabeledImageButtonLc button;
    CpImageLc example;
    CpPanelLc mainPanel;

    public void init()
    {
        super.init();

        mainPanel = new CpPanelLc();
        CpAttachmentsLayout layout = new CpAttachmentsLayout();
        CpAttachments attachments;

        setLc(mainPanel);
        mainPanel.setLayout(layout);
        mainPanel.setDrawOffscreen(true);

        pathEntryField = new CpEntryFieldLc();
        pathEntryField.setBorderStyle(CpAbstractLc.BORDER_LINE);
        pathEntryField.setBorderMargin(1);
        pathEntryField.setText("C:\\src\\cnp\\ew\\images\\lc");
        attachments = new CpAttachments(-1, 10, -1, -1);
        attachments.setHorizontalCentering();
        attachments.setWidth(200);
        layout.setAttachments(pathEntryField, attachments);
        mainPanel.add(pathEntryField);

        button = new CpLabeledImageButtonLc("Create Packed Image");
        button.addObserver(this);
        attachments = new CpAttachments();
        attachments.setTopAttachment(10, pathEntryField);
        attachments.setHorizontalCentering(pathEntryField);
        attachments.setWidth(150);
        layout.setAttachments(button, attachments);
        mainPanel.add(button);

        example = new CpImageLc();
        CpEdgeBorderDisplayer edgeDisplayer = new CpEdgeBorderDisplayer(2, 2, 2, 2);
        edgeDisplayer.borderColor = Color.red;
        example.setBorderDisplayer(edgeDisplayer);
        attachments = new CpAttachments();
        attachments.setHorizontalCentering(pathEntryField);
        attachments.setTopAttachment(10, button);
        layout.setAttachments(example, attachments);
        mainPanel.add(example);
    }

    public void update(CpObservable target, int facet, Object arg)
    {
        if ((target == button && facet == BUTTON_CLICKED) || (target == pathEntryField && facet == OBJECT_CHANGED)) {
            String path = pathEntryField.getText();
            CpPackedImage packedImage = CpPackedImage.createPackedImage(this, path);
            if (packedImage != null) {
                example.setImage(packedImage.getPackedImage());
                mainPanel.layout();
                mainPanel.repaint();
                genCodeForPackedImage(path, packedImage);
            }
        }
    }

    void genCodeForPackedImage(String path, CpPackedImage packedImage)
    {
        String fileName = path + "\\" + (new File(path)).getName() + ".txt";
        try {
            PrintStream outputStream = new PrintStream(new FileOutputStream(fileName));
            packedImage.genCode(outputStream);
            outputStream.close();
        }
        catch (IOException e) {
        }
    }
}


