Listing Thirteen

package mybeans;

import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.GridLayout;
import java.awt.Panel;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

import java.beans.PropertyChangeSupport;
import java.beans.PropertyChangeListener;

//
//
// TextCaseCustomPropertyEditorGUI
//
// This class provides the user interface for the textCase property's
// custom property editor.
//

public class TextCaseCustomPropertyEditorGUI extends Panel implements ItemListener {
  protected static final  String tags[] = { "As Is",
                                            "Upper Case",
                                            "Lower Case",
                                            "First Letter Capitalized"};

  //----------------------------------------------------------------------------
  // Instance variables
  CheckboxGroup group;
  Checkbox[] boxes;
  PropertyChangeSupport pcs;                // used to support listeners
  int selectedIndex;                        // Holds the current property value

  //----------------------------------------------------------------------------
  // Constructor
  // Lays out the user interface with four checkboxes in a group.
  public TextCaseCustomPropertyEditorGUI() {
    pcs = new PropertyChangeSupport(this);
    setLayout(new GridLayout(4, 1));
    group = new CheckboxGroup();
    boxes = new Checkbox[tags.length];
    for (int i = 0; i < tags.length; i++)
    {
      boxes[i] = new Checkbox(tags[i], false, group);
      boxes[i].addItemListener(this);       // Add ourself as an item listener
                                            // to get notification of selection
                                            // changes.
      add(boxes[i]);
    }
    group.setSelectedCheckbox(boxes[0]);
    selectedIndex = 0;
  }

  //---------------------------------------------------------------------------
  // Allows tool to listen for changes to the value of the property.
  public void addPropertyChangeListener(PropertyChangeListener listener) {
    pcs.addPropertyChangeListener(listener);
  }

  //---------------------------------------------------------------------------
  // Used by tools to remove themselves as property change listeners.
  public void removePropertyChangeListener(PropertyChangeListener listener) {
    pcs.removePropertyChangeListener(listener);
  }

  //---------------------------------------------------------------------------
  // Sets the value of the property and inform listeners (the tool) of the
  // property value change.
  public void setValue(int k) {
    int oldValue = selectedIndex;
    if (k >= tags.length) return;         // Ignore the change if its invalid.
                                          // Should not occur.
    group.setSelectedCheckbox(boxes[k]);  // Update the user interface.
    selectedIndex = k;                    // Save the new value
    pcs.firePropertyChange (CaseAwareTextDisplay.TEXT_CASE,
                            new Integer(oldValue),
                            new Integer(k));
  }

  //---------------------------------------------------------------------------
  // Returns the current value of the property.
  public int getValue() {
    return selectedIndex;
  }

  //---------------------------------------------------------------------------
  // Invoked when checkbox selection changes. Sets the new value of the
  // property.
  public void itemStateChanged(ItemEvent e) {
    Checkbox box = group.getSelectedCheckbox();
    for (int i = 0; i < tags.length; i++)
      if (box == boxes[i])                // If this is the selected checkbox
        setValue(i);                      // then update the property value
  }
}
