/* * 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 a float value * * @author ryanm */ public class FloatWidget extends Widget implements FloatChooser.Listener { /** * The number of steps to aim for in the spinner */ private static final int spinnerSteps = 100; private FloatChooser chooser; /** * Standard constructor * * @param conf * The configurator object * @param name * The name of the variable to bind to */ public FloatWidget( Configurator conf, String name ) { super( conf, name ); Number value = ( Number ) conf.getValue( name ); assert value != null : "null value for " + conf.getPath() + "/" + name; chooser = new FloatChooser( null, null, value.floatValue() ); setRange( null ); setLayout( new BorderLayout() ); add( chooser, BorderLayout.CENTER ); chooser.addListener( this ); setBorder( new TitledBorder( name ) ); addMouseListener( chooser.getRangeAdjustListener() ); setEnabled( conf.isGUIEnabled( name ) ); } /** * * */ public FloatWidget() { } @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( array[ 0 ] ) : null; max = array.length > 1 && !Float.isNaN( array[ 1 ] ) ? new Float( array[ 1 ] ) : null; } else if( range instanceof double[] ) { double[] array = ( double[] ) range; min = array.length > 0 && !Double.isNaN( array[ 0 ] ) ? new Float( array[ 0 ] ) : null; max = array.length > 1 && !Double.isNaN( array[ 1 ] ) ? new Float( array[ 1 ] ) : null; } else if( range instanceof Number[] ) { Number[] array = ( Number[] ) range; min = array.length > 0 && array[ 0 ] != null ? new Float( array[ 0 ].floatValue() ) : null; max = array.length > 1 && array[ 1 ] != null ? new Float( array[ 1 ].floatValue() ) : null; } else if( range == null ) { min = null; max = null; } else { throw new IllegalArgumentException( "Invalid range object " + range + " found for variable " + conf.getPath() + "/" + name ); } if( min != null && max != null ) { float numberRange = max.floatValue() - min.floatValue(); float stepSize = numberRange / spinnerSteps; chooser.setSpinnerStepSize( stepSize ); } else { chooser.setSpinnerStepSize( 0.1f ); } chooser.setRange( min, max ); } @Override public void updateValue() { synchronized( lock ) { Object value = conf.getValue( name ); chooser.removeListener( this ); chooser.setValue( ( ( Number ) value ).floatValue() ); chooser.addListener( this ); } } public void valueChanged( float value ) { applyChange( conf, name, new Float( 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 FloatWidget( conf, name ); } @Override public Class getType() { return float.class; } }