package cnp.ew.dialog;

import java.awt.*;
import java.util.*;

import cnp.ew.lightweight.*;
import cnp.ew.button.*;
import cnp.ew.text.*;
import cnp.ew.util.*;
import cnp.ew.layout.*;
import cnp.ew.misc.*;
import cnp.ew.list.*;

public class CpListChooserDialog extends CpLcDialog
{
    static int infoId;
    static int spacing = 20;

    static {
         infoId = CpToolkit.registerImageName("info.gif");
    }

    CpReadOnlyTextAreaLc label;
    CpLabeledImageButtonLc okButton;
    CpLabeledImageButtonLc cancelButton;
    CpListboxLc listbox;
    Object result=null;

    public CpListChooserDialog(Frame parentFrame, String title, String description, Vector items)
    {
	    super(parentFrame, title, true);

        CpAttachments attachments;

        setBackground(Color.lightGray);

        CpPanelLc rootLc = new CpPanelLc();
        rootLc.setBorderMargin(10);
        rootLc.setDrawOffscreen(true);
        setLc(rootLc);
        CpAttachmentsLayout layout = new CpAttachmentsLayout();
        rootLc.setLayout(layout);

        label = new CpReadOnlyTextAreaLc(description);
        label.setFont(CpFonts.dialogPlainTen());
        label.setShouldWrap(true);
        label.setCharsPerLineToNumLinesRatio(5);
        attachments = new CpAttachments();
        attachments.setTopAttachment(0);
        attachments.setLeftAttachment(0);
        layout.setAttachments(label, attachments);
        rootLc.add(label);

        listbox = new CpListboxLc(false);
        listbox.setItems(items);
        listbox.setBackground(Color.white);
        listbox.setIsTabStop(true);
        attachments = new CpAttachments();
        attachments.setTopAttachment(0);
        attachments.setLeftAttachment(10, label);
        attachments.setWidth(170);
        attachments.setHeight(120);
        layout.setAttachments(listbox, attachments);
        rootLc.add(listbox);

        cancelButton = new CpLabeledImageButtonLc("Cancel");
        cancelButton.addObserver(this);
        cancelButton.setIsCancelButton(true);
        cancelButton.setIsTabStop(true);
        attachments = new CpAttachments();
        attachments.setTopAttachment(10, listbox);
        attachments.setRightAttachment(CpAttachments.ATTACH_SIBLING_OPPOSITE, 15, listbox);
        attachments.setWidth(60);
        attachments.setHeight(25);
        layout.setAttachments(cancelButton, attachments);
        rootLc.add(cancelButton);

        okButton = new CpLabeledImageButtonLc("OK");
        okButton.addObserver(this);
        okButton.setIsDefaultButton(true);
        okButton.setIsTabStop(true);
        attachments = new CpAttachments();
        attachments.setTopAttachment(10, listbox);
        attachments.setRightAttachment(8, cancelButton);
        attachments.setWidth(60);
        attachments.setHeight(25);
        layout.setAttachments(okButton, attachments);
        add(okButton);
    }



    public void update(CpObservable o, int facet, Object arg)
    {
        if (facet == CpEvent.BUTTON_CLICKED) {
            if (o == okButton) {
                result = listbox.getSelectedItem();
                notifyObservers(CpEvent.OBJECT_CHANGED, result);
            }
            closeWindow();
        }
    }

	public boolean handleEvent (Event evt) {

		switch(evt.id) {
		    case Event.WINDOW_DESTROY: {
		        closeWindow();
            }
		}
		return super.handleEvent(evt);
	}

	public Object getResult()
	{
	    return result;
	}
}


