net.sourceforge.webcompmath.awt
Class VariableSlider

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JScrollBar
                  extended by net.sourceforge.webcompmath.awt.VariableSlider
All Implemented Interfaces:
java.awt.Adjustable, java.awt.event.AdjustmentListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible, InputObject, Tieable, Value

public class VariableSlider
extends javax.swing.JScrollBar
implements InputObject, Tieable, Value, java.awt.event.AdjustmentListener

A VariableSlider is a slider (implemented as a JScrollbar) 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:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JScrollBar
javax.swing.JScrollBar.AccessibleJScrollBar
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
protected  boolean integerValued
          If this is true, then the value of the variable associated with this slider is an integer.
protected  int intervals
          The number of possible value of the scrollbar (Unless integerValued is true.)
protected  Value max
          The Value that specify the max represented by the slider.
protected  double maxVal
          The values found for max the last time checkInput() was called.
protected  Value min
          The Value that specify the min represented by the slider.
protected  double minVal
          The values found for min the last time checkInput() was called.
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.
protected  int oldPosition
          This is the position of the scrollbar the last time getVal() or setVal() was called.
protected  long serialNumber
          This increases every time the value of the variable changes.
protected  net.sourceforge.webcompmath.awt.VariableSlider.VS variable
          The variable associated with this VariableSlider.
 
Fields inherited from class javax.swing.JScrollBar
blockIncrement, model, orientation, unitIncrement
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.Adjustable
HORIZONTAL, NO_ORIENTATION, VERTICAL
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
VariableSlider()
          Create a horizontal variable slider with no name and with a default value range of -5 to 5.
VariableSlider(java.lang.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).
VariableSlider(java.lang.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).
VariableSlider(Value min, Value max)
          Create a horizontal variable slider with no name and with the specified range of values.
 
Method Summary
 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).
 void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt)
          Overridden to call onUserAction.compute() if onUserAction is non-null.
 void checkInput()
          From the InputObject interface.
 boolean getIntegerValued()
          Return a boolean which is true if the VariableSlider restricts ranges of values to integers, false otherwise.
 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.
 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.
 Controller getOnUserAction()
          Return the Controller, if any, that is notified when the user adjusts the position of the scroll bar.
 java.awt.Dimension getPreferredSize()
          Modify getPreferredSize to return a width of 200, if the scrollbar is horzontal, or a height of 200, if it is vertical.
 long getSerialNumber()
          Return this object's serial number, which is increased every time the value changes.
 double getVal()
          Get the value of this VariableSlider.
 Variable getVariable()
          Return the variable associated with this VariableSlider.
 void notifyControllerOnChange(Controller c)
          Method required by InputObject interface; in this class, it simply calls setOnUserAction(c).
 void setIntegerValued(boolean b)
          If set to true, restrict the values of the variable associated with this slider to be integers.
 void setMax(Value v)
          Set the value that the variable has when the slider is at the right (or top) of the scrollbar.
 void setMin(Value v)
          Set the value that the variable has when the slider is at the left (or bottom) of the scrollbar.
 void setName(java.lang.String name)
          Set the name of the associated variable.
 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.
 void setVal(double x)
          Set the value of the variable to x.
 void sync(Tie tie, Tieable newest)
          Change the value and serial number of this object to match those of newest.
 
Methods inherited from class javax.swing.JScrollBar
addAdjustmentListener, fireAdjustmentValueChanged, getAccessibleContext, getAdjustmentListeners, getBlockIncrement, getBlockIncrement, getMaximum, getMaximumSize, getMinimum, getMinimumSize, getModel, getOrientation, getUI, getUIClassID, getUnitIncrement, getUnitIncrement, getValue, getValueIsAdjusting, getVisibleAmount, paramString, removeAdjustmentListener, setBlockIncrement, setEnabled, setMaximum, setMinimum, setModel, setOrientation, setUI, setUnitIncrement, setValue, setValueIsAdjusting, setValues, setVisibleAmount, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

variable

protected net.sourceforge.webcompmath.awt.VariableSlider.VS variable
The variable associated with this VariableSlider. VS is a nested private class, defined below.


min

protected Value min
The Value that specify the min represented by the slider. min does not have to be less than max.


max

protected Value max
The Value that specify the max 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 the last time checkInput() was called.


maxVal

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

Constructor Detail

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.

Parameters:
min - min value for range
max - max value for range

VariableSlider

public VariableSlider(java.lang.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.

Parameters:
name - name of slider
min - min value for range
max - max value for range
p - parser to use

VariableSlider

public VariableSlider(java.lang.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 Detail

setName

public void setName(java.lang.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 java.awt.Component
Parameters:
name - name of the variable

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).

Parameters:
p - parser to use

getVariable

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

Returns:
the associated variable

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.

Parameters:
b - true or false

getIntegerValued

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

Returns:
true or false

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.

Parameters:
v - value to set

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.

Parameters:
v - value to set

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.

Returns:
value

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.

Returns:
value

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.

Parameters:
c - the controller to use

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
Parameters:
c - controller to notify

getOnUserAction

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

Returns:
the controller

getSerialNumber

public long getSerialNumber()
Return this object's serial number, which is increased every time the value changes.

Specified by:
getSerialNumber in interface Tieable
Returns:
the serial number

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
Parameters:
tie - the tie that connects the objects
newest - the object to synch with

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
Returns:
value of the slider

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.

Parameters:
x - the value to set

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 java.awt.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 javax.swing.JComponent
Returns:
the dimension

adjustmentValueChanged

public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt)
Overridden to call onUserAction.compute() if onUserAction is non-null. This is not meant to be called directly.

Specified by:
adjustmentValueChanged in interface java.awt.event.AdjustmentListener
Parameters:
evt - the event created when user adjusts the slider