/* * Created on 20-Jul-2004 by Ryan McNally */ package com.ryanm.config.swing.imp; import java.awt.BorderLayout; import javax.swing.border.TitledBorder; import com.ryanm.config.Configurator; import com.ryanm.config.swing.Widget; import com.ryanm.util.swing.FloatChooser; /** * A widget for editing an integer value * * @author ryanm */ public class IntWidget extends Widget implements FloatChooser.Listener { private FloatChooser chooser; /** * Standard constructor * * @param conf * The configurator object * @param name * The name of the variable to bind to */ public IntWidget( Configurator conf, String name ) { super( conf, name ); Number value = ( Number ) conf.getValue( name ); chooser = new FloatChooser( null, null, value.floatValue(), true ); chooser.setSpinnerStepSize( 1.0f ); setRange( null ); setLayout( new BorderLayout() ); add( chooser, BorderLayout.CENTER ); chooser.addListener( this ); setBorder( new TitledBorder( name ) ); addMouseListener( chooser.getRangeAdjustListener() ); setEnabled( conf.isGUIEnabled( name ) ); } /** * * */ public IntWidget() { } @Override public void updateRange( Object range ) { range = conf.getRange( name ); Float min; Float max; if( range instanceof int[] ) { int[] array = ( int[] ) range; min = array.length > 0 ? new Float( array[ 0 ] ) : null; max = array.length > 1 ? new Float( array[ 1 ] ) : null; } else if( range instanceof float[] ) { float[] array = ( float[] ) range; min = array.length > 0 && !Float.isNaN( array[ 0 ] ) ? new Float( ( int ) array[ 0 ] ) : null; max = array.length > 1 && !Float.isNaN( array[ 1 ] ) ? new Float( ( int ) array[ 1 ] ) : null; } else if( range instanceof double[] ) { double[] array = ( double[] ) range; min = array.length > 0 && !Double.isNaN( array[ 0 ] ) ? new Float( ( int ) array[ 0 ] ) : null; max = array.length > 1 && !Double.isNaN( array[ 1 ] ) ? new Float( ( int ) array[ 1 ] ) : null; } else if( range instanceof Number[] ) { Number[] array = ( Number[] ) range; min = array.length > 0 && array[ 0 ] != null ? new Float( array[ 0 ].intValue() ) : null; max = array.length > 1 && array[ 1 ] != null ? new Float( array[ 1 ].intValue() ) : null; } else if( range == null ) { min = null; max = null; } else { throw new IllegalArgumentException( "Invalid range object " + range + " found for variable " + conf.getPath() + "/" + name ); } chooser.setRange( min, max ); } @Override public void updateValue() { synchronized( lock ) { Object value = conf.getValue( name ); chooser.removeListener( this ); chooser.setValue( ( ( Number ) value ).intValue() ); chooser.addListener( this ); } } public void valueChanged( float v ) { Integer value = new Integer( ( int ) v ); applyChange( conf, name, value ); } public void rangeChanged( float low, float high ) { // not sure if I care } @Override public void setEnabled( boolean b ) { if( b ) { b = conf.isGUIEnabled( name ); } chooser.setEnabled( b ); super.setEnabled( b ); } @Override public Widget newWidget( Configurator conf, String name, Class runtimeType ) { return new IntWidget( conf, name ); } @Override public Class getType() { return int.class; } }