/* * NumAnimatedSlider * * 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.*; /** * NumAnimatedSlider extends the capabilities of an * AnimatedSlider by providing a NumInput box for users * to type input into directly. */ public class NumAnimatedSlider extends Panel implements ActionListener,AdjustmentListener { protected ActionListener actionListener; protected AnimatedSlider theSlider; protected NumInput theInput; protected int sigDigits = 3; /* * Constructors */ public NumAnimatedSlider(double sliderValue, double sliderMin, double sliderMax) { super(); theSlider = new AnimatedSlider(sliderValue, sliderMin, sliderMax); theInput = new NumInput( new String(""+sliderValue),10,NumInput.DOUBLE); setLayout(new PercentLayout()); add("left=0, right=20, top=0, bottom=100",theInput); add("left=21, right=100, top=0, bottom=100",theSlider); theInput.addActionListener(this); theSlider.addAdjustmentListener(this); } public NumAnimatedSlider() { this(0.0,0.0,10.0); } /* * Set and Get methods */ public void setRealValue(double realValue) { theInput.setText(""+realValue); theSlider.setRealValue(realValue); postNumAnimatedSliderChanged(); } public double getRealValue() { return theInput.getDouble(); } public void setRealMax(double realMax) { theSlider.setRealMax(realMax); } public double getRealMax() { return theSlider.getRealValue(); } 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( StrLib.round(theSlider.getRealValue(),sigDigits) ); } postNumAnimatedSliderChanged(); } public void actionPerformed(ActionEvent e) { if(e.getSource() == theInput) { setRealValue(theInput.getDouble()); } postNumAnimatedSliderChanged(); } public void addActionListener(ActionListener l) { actionListener = AWTEventMulticaster.add(actionListener, l); } public void postNumAnimatedSliderChanged() { if (actionListener != null) actionListener.actionPerformed( new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "NumAnimatedSlider Changed")); } }