package cnp.ew.properties;

import java.awt.*;
import java.util.*;

import cnp.ew.util.*;
import cnp.ew.misc.*;
import cnp.ew.displayer.*;
import cnp.ew.lightweight.*;
import cnp.ew.layout.*;
import cnp.ew.button.*;
import cnp.ew.image.*;
import cnp.ew.properties.*;

public class CpColorEditorLc extends CpAbstractLc implements CpEditor
{
    static int downArrowId;

    static {
        downArrowId = CpToolkit.registerImageName("darrow12.gif");
    }

    Color color;

    CpLabeledImageButtonLc downButton;
    CpPanelLc colorPanel;
    CpColorPalette pal;

    public CpColorEditorLc()
    {
        CpAttachmentsLayout layout = new CpAttachmentsLayout();
        setLayout(layout);

        downButton = new CpLabeledImageButtonLc(CpToolkit.getImage(downArrowId));
        downButton.setBorderStyle(BORDER_OUTWARDREVERSE3D);
        downButton.setUsesFocus(false);
        downButton.addObserver(this);
        CpAttachments attachments = new CpAttachments(-1, 0, 0, 0);
        layout.setAttachments(downButton, attachments);
        add(downButton);

        colorPanel = new CpPanelLc();
        colorPanel.setBorderStyle(BORDER_LINE);
        attachments = new CpAttachments(2, 2, -1, 2);
        attachments.setRightAttachment(2, downButton);
        layout.setAttachments(colorPanel, attachments);
        add(colorPanel);

    }

    public void setObject(Object o)
    {
        color = (Color)o;
        colorPanel.setBackground(color);
    }

    public Object getObject()
    {
        return color;
    }

    public boolean mouseDown(Event e, int x, int y)
    {
        update(null, BUTTON_CLICKED, null);
        return false;
    }

    public void update(CpObservable o, int facet, Object arg)
    {
        switch (facet) {
            case BUTTON_CLICKED : {
                if (pal != null) {
                    popDownPoppedUpLc();
                    pal = null;
                } else {
                    pal = new CpColorPalette(4, 16, 16);
                    pal.addObserver(this);
                    popUpLcAt(pal, new Rectangle(size().width - pal.preferredSize().width, size().height, pal.preferredSize().width, pal.preferredSize().height));
                }
                break;
            }
            case COLOR_SELECTED : {
           	    setObject((Color)arg);
           	    notifyObservers(OBJECT_CHANGING);
           	    notifyObservers(OBJECT_CHANGED);
                pal = null;
            }
        }
    }

    public int getVerticalAlignment()
    {
        return ALIGN_TOP;
    }

    public void setVerticalAlignment(int verticalAlignment) {}

    public int getHorizontalAlignment()
    {
        return ALIGN_LEFT;
    }

    public void setHorizontalAlignment(int horizontalAlignment) {}



}


