/* * NumSlider * * Version 1.1 - Java 1.1 compatible * * Dec 6 2001 * * Copyright 2001 David Joiner and the Shodor Education Foundation */ import java.awt.*; import java.awt.event.*; import com.helplets.awt.*; //import org.shodor.utils.*; /** * NumSlider extends the functionality of a Slider by including * a NumInput for users to type into directly. */ public class NumSlider extends Panel implements ActionListener,AdjustmentListener { protected ActionListener actionListener; protected Slider theSlider; protected NumInput theInput; protected int sigDigits = 3; /* * Constructors */ public NumSlider(double sliderValue, double sliderMin, double sliderMax) { this(20,Slider.STYLE_LINEAR,sliderValue,sliderMin,sliderMax); } public NumSlider() { this(20,Slider.STYLE_LINEAR,0.0,0.0,10.0); } public NumSlider(int sliderStyle, double sliderValue, double sliderMin, double sliderMax) { this(20,sliderStyle,sliderValue,sliderMin,sliderMax); } public NumSlider(int border, int sliderStyle, double sliderValue, double sliderMin, double sliderMax) { super(); theSlider = new Slider(sliderStyle, sliderValue, sliderMin, sliderMax); theInput = new NumInput( new String(""+sliderValue),10,NumInput.DOUBLE); setLayout(new PercentLayout()); add("left=0, right="+border+", top=0, bottom=100",theInput); add("left="+(border+1)+", right=100, top=0, bottom=100",theSlider); theInput.addActionListener(this); theSlider.addAdjustmentListener(this); } /* * Set and Get methods */ public void setRealValue(double realValue) { theInput.setText(""+realValue); theSlider.setRealValue(realValue); postNumSliderChanged(); } public double getRealValue() { return theInput.getDouble(); } public void setRealMax(double realMax) { theSlider.setRealMax(realMax); } public double getRealMax() { return theSlider.getRealMax(); } public void setRealMin(double realMin) { theSlider.setRealMin(realMin); } public double getRealMin() { return theSlider.getRealMin(); } /* * Event handling */ public void adjustmentValueChanged(AdjustmentEvent e) { if(e.getAdjustable() == theSlider) { theInput.setText(""+ Double.valueOf( StrLib.round(theSlider.getRealValue(),sigDigits)). doubleValue() ); postNumSliderChanged(); } } public void actionPerformed(ActionEvent e) { if(e.getSource() == theInput) { theInput.setText(""+ Double.valueOf( StrLib.round(theInput.getDouble(),sigDigits)). doubleValue() ); theSlider.setRealValue(theInput.getDouble()); postNumSliderChanged(); } } public void addActionListener(ActionListener l) { actionListener = AWTEventMulticaster.add(actionListener, l); } public void postNumSliderChanged() { if (actionListener != null) actionListener.actionPerformed( new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "NumSlider Changed")); } }