package cnp.ew.dialog;

import java.awt.*;

import cnp.ew.lightweight.*;
import cnp.ew.button.*;
import cnp.ew.text.*;
import cnp.ew.util.*;
import cnp.ew.layout.*;
import cnp.ew.misc.*;

public class CpPrompterDialog extends CpLcDialog
{
    static int infoId;
    static int spacing = 20;

    static {
         infoId = CpToolkit.registerImageName("info.gif");
    }

    CpReadOnlyTextAreaLc label;
    CpLabeledImageButtonLc okButton;
    CpLabeledImageButtonLc cancelButton;
    CpEntryFieldLc entryField;
    String result=null;

    public CpPrompterDialog(Frame parentFrame, String title, String prompt, String initialValue)
    {
	    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);

        CpAttachmentsLayout innerLayout = new CpAttachmentsLayout();
        CpPanelLc innerPanel = new CpPanelLc();
        innerPanel.setLayout(innerLayout);
        attachments = new CpAttachments(0, 0, -1, -1);
        layout.setAttachments(innerPanel, attachments);
        rootLc.add(innerPanel);

        CpImageLc imageLc = new CpImageLc(CpToolkit.getImage(infoId));
        attachments = new CpAttachments();
        attachments.setVerticalCentering();
        attachments.setLeftAttachment(0);
        innerLayout.setAttachments(imageLc, attachments);
        innerPanel.add(imageLc);

        label = new CpReadOnlyTextAreaLc(prompt);
        label.setFont(CpFonts.dialogPlainTen());
        label.setShouldWrap(true);
        attachments = new CpAttachments();
        attachments.setLeftAttachment(5, imageLc);
        attachments.setVerticalCentering(imageLc);
        innerLayout.setAttachments(label, attachments);
        innerPanel.add(label);

        entryField = new CpEntryFieldLc();
        entryField.setBorderMargin(2);
        entryField.setBorderStyle(CpAbstractLc.BORDER_LINE);
        entryField.setBackground(Color.white);
        entryField.setText(initialValue);
        entryField.setIsTabStop(true);
        entryField.setSelectAllOnGettingFocus(true);
        attachments = new CpAttachments();
        attachments.setLeftAttachment(CpAttachments.ATTACH_SIBLING_OPPOSITE, 0, innerPanel);
        attachments.setRightAttachment(CpAttachments.ATTACH_SIBLING_OPPOSITE, 0, innerPanel);
        attachments.setTopAttachment(10, innerPanel);
        layout.setAttachments(entryField, attachments);
        rootLc.add(entryField);


        cancelButton = new CpLabeledImageButtonLc("Cancel");
        cancelButton.addObserver(this);
        cancelButton.setIsCancelButton(true);
        cancelButton.setIsTabStop(true);
        attachments = new CpAttachments();
        attachments.setTopAttachment(10, entryField);
        attachments.setRightAttachment(CpAttachments.ATTACH_SIBLING_OPPOSITE, 0, entryField);
        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, entryField);
        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 = entryField.getText();
                notifyObservers(CpEvent.OBJECT_CHANGED, result);
            }
            closeWindow();
        }
    }

	public boolean handleEvent (Event evt) {

		switch(evt.id) {
		    case Event.WINDOW_DESTROY: {
		        closeWindow();
            }
		}
		return super.handleEvent(evt);
	}

	public String getResult()
	{
	    return result;
	}
}


