Class VariableSlider

All Implemented Interfaces:
InputObject, Tieable, Value, Adjustable, ImageObserver, MenuContainer, Serializable, Accessible

public class VariableSlider extends Scrollbar implements InputObject, Tieable, Value
A VariableSlider is a slider (implemented as a Scrollbar) whose position represents the value of an associated variable. The range of values represented by the slider is given by a pair of Value objects. They can be specified in the constructor or later with the setMin and setMax methods. A VariableSlider has an associated variable that represents the value of the slider. Note that the value of the variable can change only when the setInput() or checkInput() method is called. If you want the value of the variable to track the position of the slider as it is is manipulated by the user, add the slider to a Controller and set the Controller as the Slider's "onUserAction" property. This allows other objects that depend on the values of the slider to be recomputed by the controller when the value changes, as long as they are also registered with the Controller.

Some points to note: 1) setVal() can set a value outside the range from min to max, which will persist until the next time checkInput() or setVal() is called again. 2) If the value of min or max changes, the value of this variable will not change EXCEPT that it is clamped to the range between min and max. 3) Min does not have to be less than max. 4) The checkInput() routine only sets the needValueCheck flag to true. (The setVal() and getVal() routines both set this flag to false.) This "lazy evaluation" is used because checkInput() can't compute the new value itself. (The max and min might depend on Values that are themselves about to change when some other object's checkInput() mehtod is called.) 5) getVal() returns the current value, as stored in the variable, UNLESS needValueCheck is true. In that case, it recomputes the value first. getSerialNumber() works similarly. 6) A VariableSlider never throws JCMErrors. If an error occurs when min or max is evaluated, the value of the variable associated with this VariableSlider becomes undefined. (The point is, it doesn't generate any errors of its own. The error would be caused by other InputObjects which should throw their own errors when their checkInput() methods are called.)

See Also:
  • Field Details

    • variable

      protected edu.hws.jcm.awt.VariableSlider.VS variable
      The variable associated with this VariableSlider. VS is a nested private class, defined below.
    • min

      protected Value min
      The Values that specify the range of values represented by the slider. min does not have to be less than max.
    • max

      protected Value max
      The Values that specify the range of values represented by the slider. min does not have to be less than max.
    • integerValued

      protected boolean integerValued
      If this is true, then the value of the variable associated with this slider is an integer. Furthermore, the number of intervals on the slider is set to be the same as the range of possible values (unless this range is too big).
    • intervals

      protected int intervals
      The number of possible value of the scrollbar (Unless integerValued is true.)
    • serialNumber

      protected long serialNumber
      This increases every time the value of the variable changes.
    • needsValueCheck

      protected boolean needsValueCheck
      This is set to true when checkInput() is called to indicate that the min and max values must be checked the next time getVal() is called.
    • oldPosition

      protected int oldPosition
      This is the position of the scrollbar the last time getVal() or setVal() was called. It is used to check whether the user has repositioned the slider.
    • minVal

      protected double minVal
      The values found for min and max the last time checkInput() was called.
    • maxVal

      protected double maxVal
      The values found for min and max the last time checkInput() was called.
  • Constructor Details

    • VariableSlider

      public VariableSlider()
      Create a horizontal variable slider with no name and with a default value range of -5 to 5.
    • VariableSlider

      public VariableSlider(Value min, Value max)
      Create a horizontal variable slider with no name and with the specified range of values. If min is null, a default value -5 is used. If max is null, a default value 5 is used.
    • VariableSlider

      public VariableSlider(String name, Value min, Value max, Parser p)
      Create a horizontal variable slider with the given name and range of values, and register it with the given parser (but only if both name and p are non-null). If min is null, a default value -5 is used. If max is null, a default value 5 is used.
    • VariableSlider

      public VariableSlider(String name, Value min, Value max, Parser p, int intervals, int orientation)
      Create a variable slider with the given name and range of values, and register it with the given parser (but only if both name and p are non-null). The "intervals" parameter specifes how many different positions there are on the slider. (The value of the scrollbar ranges from 0 to intervals.) If intervals is <= 0, it will be set to 1000. If it is between 1 and 9, it will be set to 10. The orientation must be either Scrollbar.HORIZONTAL or Scrollbar.VERTICAL. It specifies whether this is a horizontal or vertical scrollbar. If min is null, a default value -5 is used. If max is null, a default value 5 is used.
      Parameters:
      name - name for this VariableSlider.
      min - minimum value for slider.
      max - maximum value for slider.
      p - register VariableSlider with this Parser.
      intervals - discrete positions on slider.
      orientation - Scrollbar.HORIZONTAL or Scrollbar.VERTICAL.
  • Method Details

    • setName

      public void setName(String name)
      Set the name of the associated variable. You shouldn't do this if it has been added to a parser. If name is non-null, then the name of this Component is also set to the specified name.
      Overrides:
      setName in class Component
    • addTo

      public void addTo(Parser p)
      A convenience method that registers this VariableSlider's variable with p (but only if both p and the name of the variable are non-null).
    • getVariable

      public Variable getVariable()
      Return the variable associated with this VariableSlider.
    • setIntegerValued

      public void setIntegerValued(boolean b)
      If set to true, restrict the values of the variable associated with this slider to be integers. Furthermore, the number of intervals on the scrollbar will be set to be the same as the size of the range from min to max (unless this range is too big). The setVal() method can still set the value of the variable to be a non-integer.
    • getIntegerValued

      public boolean getIntegerValued()
      Return a boolean which is true if the VariableSlider restricts ranges of values to integers, false otherwise.
    • setMin

      public void setMin(Value v)
      Set the value that the variable has when the slider is at the left (or bottom) of the scrollbar. If v is null, -5 is used as the default value.
    • setMax

      public void setMax(Value v)
      Set the value that the variable has when the slider is at the right (or top) of the scrollbar. If v is null, 5 is used as the default value.
    • getMin

      public Value getMin()
      Get the Value object that gives the value of the variable when the slider is at the left (or bottom) of the scrollbar. The Value is always non-null.
    • getMax

      public Value getMax()
      Get the Value object that gives the value of the variable when the slider is at the right (or top) of the scrollbar. The Value is always non-null.
    • setOnUserAction

      public void setOnUserAction(Controller c)
      If the Controller, c, is non-null, then its compute method will be called whenever the user adjusts the position of the scroll bar.
    • notifyControllerOnChange

      public void notifyControllerOnChange(Controller c)
      Method required by InputObject interface; in this class, it simply calls setOnUserAction(c). This is meant to be called by JCMPanel.gatherInputs().
      Specified by:
      notifyControllerOnChange in interface InputObject
    • getOnUserAction

      public Controller getOnUserAction()
      Return the Controller, if any, that is notified when the user adjusts the position of the scroll bar.
    • getSerialNumber

      public long getSerialNumber()
      Return this object's serial number, which is increased every time the value changes.
      Specified by:
      getSerialNumber in interface Tieable
    • sync

      public void sync(Tie tie, Tieable newest)
      Change the value and serial number of this object to match those of newest. See the Tie class for more information. This is not meant to be called directly
      Specified by:
      sync in interface Tieable
    • getVal

      public double getVal()
      Get the value of this VariableSlider. (If needsValueCheck is true, then the value is recomputed. Otherwise, the current value is returned.)
      Specified by:
      getVal in interface Value
    • setVal

      public void setVal(double x)
      Set the value of the variable to x. If possible, set the value on the scroll bar to match.
    • checkInput

      public void checkInput()
      From the InputObject interface. This will force the slider to recompute its max and min values, and possibly clamp its value between these two extremes) the next time the value or serial number is checked. This is ordinarily called by a Controller.
      Specified by:
      checkInput in interface InputObject
    • getPreferredSize

      public Dimension getPreferredSize()
      Modify getPreferredSize to return a width of 200, if the scrollbar is horzontal, or a height of 200, if it is vertical. This is not meant to be called directly.
      Overrides:
      getPreferredSize in class Component
    • processAdjustmentEvent

      public void processAdjustmentEvent(AdjustmentEvent evt)
      Overridden to call onUserAction.compute() if onUserAction is non-null. This is not meant to be called directly.
      Overrides:
      processAdjustmentEvent in class Scrollbar