/* * Created on 20-Jul-2004 by Ryan McNally */ package com.ryanm.config.swing.imp; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JTextField; import javax.swing.border.TitledBorder; import com.ryanm.config.Configurator; import com.ryanm.config.swing.Widget; /** * A widget for editing a String. Depending on the bounds, it will * either be a textfield, or a combobox. * * @author ryanm */ public class StringWidget extends Widget implements ActionListener, ItemListener { private JTextField textField; private JComboBox combo; private JComponent currentComponent = null; private String oldValue; /** * Standard constructor * * @param conf * The configurator object * @param name * The name of the variable to bind to */ public StringWidget( Configurator conf, String name ) { super( conf, name ); setBorder( new TitledBorder( name ) ); setLayout( new BorderLayout() ); String value = ( String ) conf.getValue( name ); oldValue = value == null ? null : new String( value ); setRange( conf.getRange( name ) ); setEnabled( conf.isGUIEnabled( name ) ); } /** * * */ public StringWidget() { } @Override public void updateRange( Object r ) { Object range = conf.getRange( name ); if( currentComponent != null ) { remove( currentComponent ); } String value = ( String ) conf.getValue( name ); if( value != null ) { value = value.intern(); } if( range == null ) { textField = new JTextField(); textField.setText( value ); currentComponent = textField; add( textField, BorderLayout.CENTER ); textField.addActionListener( StringWidget.this ); } else { String[] pv; if( range instanceof String ) { pv = new String[] { ( String ) range }; } else if( range instanceof String[] ) { pv = ( String[] ) range; } else { throw new IllegalArgumentException( "Non-string range object " + range + " found for variable " + conf.getPath() + "/" + name + "\n" ); } for( int i = 0; i < pv.length; i++ ) { pv[ i ] = pv[ i ].intern(); } if( combo != null ) { combo.removeItemListener( StringWidget.this ); } combo = new JComboBox( pv ); combo.setSelectedItem( value ); currentComponent = combo; add( combo, BorderLayout.CENTER ); combo.addItemListener( StringWidget.this ); } currentComponent.setEnabled( conf.isGUIEnabled( name ) ); validate(); } @Override public void updateValue() { synchronized( lock ) { Object value = conf.getValue( name ); if( value != null ) { oldValue = new String( ( String ) value ); if( textField != null ) { textField.removeActionListener( this ); textField.setText( ( String ) value ); textField.addActionListener( this ); } else { combo.removeItemListener( this ); combo.setSelectedItem( value ); combo.addItemListener( this ); } } } } public void actionPerformed( final ActionEvent e ) { assert e.getSource() == textField; if( oldValue == null || !oldValue.equals( textField.getText() ) ) { oldValue = textField.getText(); applyChange( conf, name, textField.getText() ); } } public void itemStateChanged( final ItemEvent e ) { assert e.getSource() == combo : e.getSource() + " != " + combo; if( oldValue == null || !oldValue.equals( combo.getSelectedItem() ) ) { oldValue = ( String ) combo.getSelectedItem(); applyChange( conf, name, combo.getSelectedItem() ); } } @Override public void setEnabled( boolean b ) { if( b ) { b = conf.isGUIEnabled( name ); } if( combo != null ) { combo.setEnabled( b ); } else { textField.setEnabled( b ); } super.setEnabled( b ); } @Override public Widget newWidget( Configurator conf, String name, Class runtimeType ) { return new StringWidget( conf, name ); } @Override public Class getType() { return String.class; } }