package com.MyCompany.examplebean;

import java.beans.*;

public class MyLineTransparencyEditor extends PropertyEditorSupport {
    // these are the labels for the possible property values
    protected static final String    OPAQUE_TAG        = "Opaque",
                                    TRANSPARENT_TAG    = "Transparent";
    
    // this is the array of tags that getTags() returns.
    protected static final String[] tags = { OPAQUE_TAG, TRANSPARENT_TAG };

    // these are the possible property values. Note that they are Integers, not
    // ints. The unwrapping of these objects is performed by the builder tool.
    protected static final Integer OPAQUE_VALUE = new Integer( MyLine.OPAQUE ),
                                TRANSPARENT_VALUE = new Integer( MyLine.TRANSPARENT );    
    
    // returns an array of labels for all of the possible property values.
    public String[] getTags() {
        return tags;
    }
    
    // translates the supplied String to a property value, and caches the result
    // for later retrieval by getValue().
    public void setAsText( String tag ) {
        if (tag.equals( OPAQUE_TAG )) {
            setValue( OPAQUE_VALUE );
        } else if (tag.equals( TRANSPARENT_TAG )) {
            setValue( TRANSPARENT_VALUE );
        } else {
            throw new IllegalArgumentException();
        }
    }
    // returns the label for the current value stored by the PropertyEditor.
    public String getAsText() {
        // PropertyEditorSupport.getValue() returns an Object, so we coerce.
        Integer value = (Integer)getValue();
        
        // return the label that corresponds to our stored value.
        if (value.equals( OPAQUE_VALUE )) {
            return OPAQUE_TAG;
        } else {
            return TRANSPARENT_TAG;
        }
    }
}
