import javax.swing.*;
import java.awt.event.*;

/** A listener that you attach to a checkbox or radio button,
 *  that, that toggles the enabled status of some other 
 *  component when the entry's state is changed. The 
 *  defaultState option determines if the component associated
 *  with the checkbox is initially enabled (and thus checking
 *  the checkbox disables it), or initially disabled (and thus
 *  checking the checkbox enables it).
 *
 *  Taken from Core Web Programming from 
 *  Prentice Hall and Sun Microsystems Press,
 *  http://www.corewebprogramming.com/.
 *  &copy; 2001 Marty Hall and Larry Brown;
 *  may be freely used or adapted.
 */

public class DisableListener implements ItemListener {
  private JComponent target;
  private boolean defaultState;

  private static void addListener(AbstractButton source, 
                                  JComponent target,
				                      boolean defaultState) {
    DisableListener enablerOrDisabler =
      new DisableListener(target, defaultState);
    source.addItemListener(enablerOrDisabler);
  }

  public static void addDisabler(AbstractButton source, 
                                 JComponent target) {
    addListener(source, target, true);
  }

  public static void addEnabler(AbstractButton source, 
                                JComponent target) {
    addListener(source, target, false);
  }

  public DisableListener(JComponent target, 
                         boolean defaultState) {
    this.target = target;
    this.defaultState = defaultState;
    if (!defaultState)
      target.setEnabled(false);
  }

  public void itemStateChanged(ItemEvent event) {
    if (event.getStateChange() == ItemEvent.SELECTED)
      target.setEnabled(!defaultState);
    else
      target.setEnabled(defaultState);
  }
}
