package cnp.ew.spin;

import java.util.*;
import java.awt.*;
import cnp.ew.util.*;
import cnp.ew.button.*;
import cnp.ew.list.*;
import cnp.ew.layout.*;
import cnp.ew.displayer.*;


public class CpChoiceLc extends CpSpinEditorLc
{
    static int buttonArrowId;

    static {
        buttonArrowId = CpToolkit.registerImageName("darrow12.gif");
    }

    CpLabeledImageButtonLc button;
    //CpListboxLc popup;


    public CpChoiceLc()
    {
        super("");

        CpSpinFieldLc field;
        model = new CpStringListAndSelectionModel();
        ((CpObservable)model).addObserver(this);
        addSpinField(field = new CpStringListSpinFieldLc());
        setSelectedField(field);
        field.setFont(CpFonts.helveticaPlainTwelve());

        button = new CpLabeledImageButtonLc();
        button.setImage(CpToolkit.getImage(buttonArrowId), CpLabeledImageButtonLc.IMAGE_OUT);
        CpAttachments attachments = new CpAttachments();
        attachments.setLeftAttachment(0, field);
        attachments.setTopAttachment(0);
        ((CpAttachmentsLayout)getLayout()).setAttachments(button, attachments);
        add(button);
        button.addObserver(this);
        initializePopup();
    }

    public void setStringList(Vector stringList)
    {
        ((CpStringListAndSelectionModel)model).setStringList(stringList);
    }

    public void addItem(String item)
    {
        CpStringListAndSelectionModel m = (CpStringListAndSelectionModel)getModel();
        m.addItem(item);
    }

    public String getSelection()
    {
        return ((CpStringListAndSelectionModel)getModel()).getSelection();
    }

    public Object getObject()
    {
        return getSelection();
    }

    public void setObject(Object object)
    {
        setSelection((String)object);
    }

    public int getSelectedIndex()
    {
        return ((CpStringListAndSelectionModel)getModel()).getSelectionIndex();
    }

    public void setSelection(String newSelection)
    {
        ((CpStringListAndSelectionModel)getModel()).setSelection(newSelection);
    }

    public void select(String newSelection)
    {
        setSelection(newSelection);
    }

    public void select(int newIndex)
    {
        setSelectionIndex(newIndex);
    }

    public void setSelectionIndex(int newSelectionIndex)
    {
        ((CpStringListAndSelectionModel)getModel()).setSelectionIndex(newSelectionIndex);
    }

    public void update(CpObservable o, int facet, Object arg)
    {
        /*
        if (o == button && facet == BUTTON_PRESS && popup == null) {
            popupListbox();
        }
        if (o == popup && facet == LC_POPPED_DOWN) {
            popup = null;
        }
        */
    }

    public Vector getItems()
    {
        return ((CpStringListAndSelectionModel)getModel()).getStringList();
    }

    void initializePopup()
    {/*
        popup = new CpListboxLc(false);
        popup.addObserver(this);
		popup.setFont(CpFonts.helveticaPlainTwelve());
		popup.setBackground(Color.white);
		popup.getModel().setEditor(new CpStringEditor());
		((CpGeneralStringDisplayer)popup.getModel().displayer).selectionDisplayStyle = CpGeneralStringDisplayer.SELECT_CELL;
        popup.setItems(getItems());
        */
    }

    void popupListbox()
    {
        //popUpLcAt(popup, new Rectangle(0, size().height + 1, size().width, popup.preferredSize().height));
    }

}
