package cnp.ew.kdemo;

import java.awt.*;
import cnp.ew.spin.*;
import cnp.ew.misc.*;
import cnp.ew.layout.*;
import cnp.ew.converter.*;
import cnp.ew.lightweight.*;
import cnp.ew.text.*;
import cnp.ew.util.*;

public class CpSpinEditorPanel extends CpPanelLc
{
    CpSpinEditorLc spinEditor;
    CpEntryFieldLc formatTextField;
    CpReadOnlyTextAreaLc formattedText;

    public CpSpinEditorPanel(String label, CpSpinEditorLc newSpinEditor, Object initialValue, String initialFormat)
    {
        super(CpPanelLc.BORDER_LABELED_ETCHED);
        setBorderMargin(5);
        setText(label);
        CpAttachments attachments;
        CpReadOnlyTextAreaLc label1, label2;

        CpAttachmentsLayout layout = new CpAttachmentsLayout();
        setLayout(layout);

        label1 = new CpReadOnlyTextAreaLc("Value");
        add(label1);
	    attachments = new CpAttachments(0, -1, -1, -1);
	    attachments.setVerticalCentering();
        layout.setAttachments(label1, attachments);

        spinEditor = newSpinEditor;
        add(spinEditor);
        spinEditor.addObserver(this);
        spinEditor.setIsTabStop(true);
	    attachments = new CpAttachments();
        layout.setAttachments(spinEditor, attachments);
        attachments.setLeftAttachment(10, label1);
        attachments.setVerticalCentering(label1);

        label2 = new CpReadOnlyTextAreaLc("Format");
        add(label2);
	    attachments = new CpAttachments();
        layout.setAttachments(label2, attachments);
        attachments.setLeftAttachment(CpAttachments.ATTACH_POSITION, 25);
        attachments.setVerticalCentering(spinEditor);

        formatTextField = new CpEntryFieldLc(initialFormat);
        formatTextField.setBorderStyle(BORDER_INWARD3D);
        formatTextField.setBorderMargin(1);
        formatTextField.setBackground(Color.white);
        add(formatTextField);
        formatTextField.setIsTabStop(true);
        formatTextField.setSelectAllOnGettingFocus(true);
        formatTextField.addObserver(this);
	    attachments = new CpAttachments();
        layout.setAttachments(formatTextField, attachments);
        attachments.setLeftAttachment(10, label2);
        attachments.setVerticalCentering(label2);
        attachments.setWidth(130);


        formattedText = new CpReadOnlyTextAreaLc();
        formattedText.setFont(new Font("Dialog", Font.PLAIN, 12));
        add(formattedText);
	    attachments = new CpAttachments();
        layout.setAttachments(formattedText, attachments);
        attachments.setLeftAttachment(10, formatTextField);
        attachments.setVerticalCentering(formatTextField);
        attachments.setRightAttachment(0);

        spinEditor.setObject(initialValue);
        fillInFormattedText();
	}

	void fillInFormattedText()
	{
        Object object = spinEditor.getObject();
        CpToStringConverter converter = CpDefaultToStringConverter.converterFor(object);
        try {
            converter.setFormatString(formatTextField.getText());
            formattedText.setText(converter.convert(object));
        }
        catch (IllegalArgumentException e) {
            formattedText.setText("*** Bad Format String ***");
        }
	}

    public void update(CpObservable o, int facet, Object arg)
    {
	    if ((o == formatTextField && facet == OBJECT_CHANGING) ||
	        (o == spinEditor && facet == OBJECT_CHANGED)) {
	        fillInFormattedText();
	    }
	}
}