Listing Twelve

package mybeans;

import java.awt.*;
import java.beans.*;

//
//
// TextCaseCustomPropertyEditor
//
// This class implements a custom property editor for the textCase property.
// The custom editor is a separate dialog where the user makes the desired selection.
// This dialog is accessed via the bean's property sheet.
//

public class TextCaseCustomPropertyEditor extends PropertyEditorSupport
  implements PropertyChangeListener {

  protected static final  String tags[] = { "As Is",
                                            "Upper Case",
                                            "Lower Case",
                                            "First Letter Capitalized"};

  //------------------------------------------------------------------------
  // Instance variables
  protected TextCaseCustomPropertyEditorGUI gui;    // instance of the user interface
  protected CaseAwareTextDisplayC painterBean;      // an instance of the bean

  //------------------------------------------------------------------------
  // Constructor
  // Instantiates the user interface and registers itself
  // as a property change listener on the user interface to keep track of
  // the user's selections. It also creates an instance of the bean which
  // is used to reflect the property's value in the bean's property sheet.
  public TextCaseCustomPropertyEditor() {
    gui = new TextCaseCustomPropertyEditorGUI();
    gui.addPropertyChangeListener(this);
    painterBean = new CaseAwareTextDisplayC();
    painterBean.setText("wordA wordB");
    setValue(new Integer(gui.getValue()));
  }

  //------------------------------------------------------------------------
  // Updates the painter bean to reflect the user's selection.
  public void propertyChange(PropertyChangeEvent e) {
    setValue(getValue());
  }

  //------------------------------------------------------------------------
  // Updates the painter bean and the user interface to reflect the
  // specified value. It also informs the tool of the property change by
  // firing a property change event.
  public void setValue(Object value) {
    gui.setValue(((Integer)value).intValue());
    painterBean.setTextCase(((Integer)value).intValue());
    firePropertyChange();
  }

  //------------------------------------------------------------------------
  // Gets the currently selected property value in the user interface.
  public Object getValue() {
    return new Integer(gui.getValue());
  }

  //------------------------------------------------------------------------
  // Indicates this property editor is a custom property editor.
  public boolean supportsCustomEditor() {
    return true;
  }

  //------------------------------------------------------------------------
  // Returns the instance of the user interface for this custom editor.
  public Component getCustomEditor() {
    return gui;
  }

  //------------------------------------------------------------------------
  // Indicates this property editor can paint itself in the bean's
  // property sheet.
  public boolean isPaintable() {
    return true;
  }

  //------------------------------------------------------------------------
  // Updates the bean's property sheet by having the painter bean
  // paint itself in the specified rectangle.
  public void paintValue(Graphics g, Rectangle clipRect)
  {
    g.clipRect(clipRect.x, clipRect.y, clipRect.width, clipRect.height);
    g.translate(clipRect.x, clipRect.y);
    g.setColor(Color.white);
    g.fillRect(0, 0, clipRect.width, clipRect.height);
    g.setColor(Color.black);
    painterBean.paint(g);
  }

  //--------------------------------------------------------------------------------
  // Translate the current value of the property to a string and return the string.
  // This method is needed because Visual Cafe does not call paintValue and relies
  // solely on the editor returning the value of the property as a 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. This method is needed because Visual Cafe does not call
  // paintValue and relies solely on the editor returning the value of the property
  // as a string.
  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());
  }
}
