/* * Created on 20-Jul-2004 by Ryan McNally */ package com.ryanm.config.swing.imp; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JCheckBox; import com.ryanm.config.Configurator; import com.ryanm.config.swing.Widget; /** * Controls a boolean variable * * @author ryanm */ public class BooleanWidget extends Widget implements ActionListener { private JCheckBox checkBox; private boolean oldValue; /** * Constructs a widget to manipulate a boolean variable * * @param conf * The configurator that contains the variable * @param name * The name of the variable */ public BooleanWidget( Configurator conf, String name ) { super( conf, name ); checkBox = new JCheckBox( name ); Boolean b = ( Boolean ) conf.getValue( name ); assert b != null : name; checkBox.setSelected( b.booleanValue() ); oldValue = b.booleanValue(); add( checkBox ); checkBox.addActionListener( this ); setEnabled( conf.isGUIEnabled( name ) ); } /** * Used for the ghost instance in the factory */ public BooleanWidget() { } public void actionPerformed( final ActionEvent e ) { final Boolean value = new Boolean( checkBox.isSelected() ); if( value.booleanValue() != oldValue ) { oldValue = value.booleanValue(); applyChange( conf, name, value ); } } @Override public void updateValue() { Boolean value = ( Boolean ) conf.getValue( name ); checkBox.setSelected( value.booleanValue() ); oldValue = value.booleanValue(); } @Override public void setEnabled( boolean enabled ) { if( enabled ) { enabled = conf.isGUIEnabled( name ); } checkBox.setEnabled( enabled ); } @Override public void updateRange( Object range ) { } @Override public Widget newWidget( Configurator conf, String name, Class runtimeType ) { return new BooleanWidget( conf, name ); } @Override public Class getType() { return boolean.class; } }