Listing Six

package mybeans;

import java.beans.PropertyEditorSupport;

//
//
// TextCaseTextPropertyEditor
//
// This class implements a string editor for the textCase property.
// This editor will appear in the bean's property sheet. The user must
// type in the text exactly as it appears in the tags class constant.
//
public class TextCaseTextPropertyEditor extends PropertyEditorSupport
{
  protected static final  String tags[] = { "As Is",
                                            "Upper Case",
                                            "Lower Case",
                                            "First Letter Capitalized"};

  //--------------------------------------------------------------------------------
  // Use a text field into which user must type string choices
  public String[] getTags()
  {
    return null;
  }

  //--------------------------------------------------------------------------------
  // Translate the current value of the property to a string and return the string.
  public String getAsText() {
    if (getValue() == null)
      return null;
    int value = ((Integer) getValue()).intValue();
    if (value < 0 || value >= tags.length)
      return "Illegal value";                 // should not occur
    return tags[value];
  }

  //--------------------------------------------------------------------------------
  // Translates a string to its corresponding value and make it the new
  // value of the property.
  public void setAsText(String text) throws IllegalArgumentException {
    for (int i = 0; i < tags.length; i++) {
      if (tags[i].equals(text)) {
        setValue (new Integer(i));
        return;
      }
    }
    throw new IllegalArgumentException(text + " is not a valid value for textCase.");
  }

  //---------------------------------------------------------------------------------
  // Returns the java initialization string used by tools which generate source code.
  public String getJavaInitializationString() {
    if (getValue() == null)
      return "";
    return (getValue().toString());
  }
}

