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 CpProgressDialog extends CpLcDialog
{
    CpReadOnlyTextAreaLc label;
    CpLabeledImageButtonLc cancelButton;
    CpValueViewerLc gauge;
    String result=null;

    public CpProgressDialog(Frame parentFrame, String title, String prompt, int min, int max, int value)
    {
	    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(prompt);
        label.setFont(CpFonts.dialogPlainTen());
        label.setShouldWrap(true);
        attachments = new CpAttachments();
        attachments.setLeftAttachment(0);
        attachments.setTopAttachment(0);
        attachments.setWidth(250);
        attachments.setHeight(label.wrappedPreferredHeightFor(250));
        layout.setAttachments(label, attachments);
        rootLc.add(label);

        gauge = new CpValueViewerLc();
        gauge.setOrientation(gauge.ORIENT_HORZ);
        gauge.setValues(min, max, value);
        attachments = new CpAttachments();
        attachments.setLeftAttachment(CpAttachments.ATTACH_SIBLING_OPPOSITE, 0, label);
        attachments.setRightAttachment(CpAttachments.ATTACH_SIBLING_OPPOSITE, 0, label);
        attachments.setTopAttachment(10, label);
        attachments.setHeight(20);
        layout.setAttachments(gauge, attachments);
        rootLc.add(gauge);

        cancelButton = new CpLabeledImageButtonLc("Cancel");
        cancelButton.setIsCancelButton(true);
        cancelButton.addObserver(this);
        attachments = new CpAttachments();
        attachments.setTopAttachment(10, gauge);
        attachments.setRightAttachment(CpAttachments.ATTACH_SIBLING_OPPOSITE, 0, gauge);
        attachments.setWidth(60);
        attachments.setHeight(25);
        layout.setAttachments(cancelButton, attachments);
        rootLc.add(cancelButton);
    }

    public void setValue(int newValue)
    {
        gauge.setValue(newValue);
    }

    public void update(CpObservable o, int facet, Object arg)
    {
        if (facet == CpEvent.BUTTON_CLICKED) {
            if (o == cancelButton) {
                notifyObservers(CpEvent.OBJECT_CHANGED, null);
            }
            closeWindow();
        }
    }

	public boolean handleEvent (Event evt) {

		switch(evt.id) {
		    case Event.WINDOW_DESTROY: {
		        closeWindow();
            }
		}
		return super.handleEvent(evt);
	}

	public String getResult()
	{
	    return result;
	}
}


