net.sourceforge.webcompmath.awt
Class VariableJSlider

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JSlider
                  extended by net.sourceforge.webcompmath.awt.VariableJSlider
All Implemented Interfaces:
java.awt.event.AdjustmentListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible, javax.swing.SwingConstants, InputObject, Tieable, Value
Direct Known Subclasses:
WCMVariableJSliderBean

public class VariableJSlider
extends javax.swing.JSlider
implements InputObject, Tieable, Value, java.awt.event.AdjustmentListener

A VariableJSlider is a slider (implemented as a JSlider) 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 VariableJSlider 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 VariableJSlider never throws WCMErrors. If an error occurs when min or max is evaluated, the value of the variable associated with this VariableJSlider 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.)

You can also display value labels for the slider by using setPaintLabels(true). This labels the two ends and the middle of the slider. If you want more labels or you want tick marks, you will need to dig into using the JSlider methods directly.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JSlider
javax.swing.JSlider.AccessibleJSlider
 
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 slider (Unless integerValued is true.)
protected  Value intervalsValue
          The number of possible values of the slider, represented as a Value.
protected  Value max
          The max value.
protected  double maxVal
          The value found for max the last time checkInput() was called.
protected  Value min
          The Values that specify the range of values represented by the slider.
protected  double minVal
          The value 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 slider 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.VariableJSlider.VS variable
          The variable associated with this VariableJSlider.
 
Fields inherited from class javax.swing.JSlider
changeEvent, changeListener, majorTickSpacing, minorTickSpacing, orientation, sliderModel, snapToTicks
 
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 javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
VariableJSlider()
          Create a horizontal variable slider with no name and with a default value range of -5 to 5.
VariableJSlider(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).
VariableJSlider(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).
VariableJSlider(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 VariableJSlider'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 VariableJSlider restricts ranges of values to integers, false otherwise.
 int getIntervals()
          Get the number of discrete slider steps
 Value getIntervalsValue()
          Get the number of slider steps as a Value object.
 Value getMax()
          Get the Value object that gives the value of the variable when the slider is at the right (or top) of the slider.
 Value getMin()
          Get the Value object that gives the value of the variable when the slider is at the left (or bottom) of the slider.
 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 slider 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 VariableJSlider.
 Variable getVariable()
          Return the variable associated with this VariableJSlider.
 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 setIntervals(int intervals)
          Set the number of discrete slider steps
 void setIntervalsValue(Value v)
          Set the number of slider steps as a Value object.
 void setMax(Value v)
          Set the value that the variable has when the slider is at the right (or top) of the slider.
 void setMin(Value v)
          Set the value that the variable has when the slider is at the left (or bottom) of the slider.
 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.JSlider
addChangeListener, createChangeListener, createStandardLabels, createStandardLabels, fireStateChanged, getAccessibleContext, getChangeListeners, getExtent, getInverted, getLabelTable, getMajorTickSpacing, getMaximum, getMinimum, getMinorTickSpacing, getModel, getOrientation, getPaintLabels, getPaintTicks, getPaintTrack, getSnapToTicks, getUI, getUIClassID, getValue, getValueIsAdjusting, paramString, removeChangeListener, setExtent, setFont, setInverted, setLabelTable, setMajorTickSpacing, setMaximum, setMinimum, setMinorTickSpacing, setModel, setOrientation, setPaintLabels, setPaintTicks, setPaintTrack, setSnapToTicks, setUI, setValue, setValueIsAdjusting, updateLabelUIs, 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, getMaximumSize, getMinimumSize, 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, setEnabled, setFocusTraversalKeys, 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.VariableJSlider.VS variable
The variable associated with this VariableJSlider. 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 max value.


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 slider (Unless integerValued is true.)


intervalsValue

protected Value intervalsValue
The number of possible values of the slider, represented as a Value.


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 slider 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 value found for min the last time checkInput() was called.


maxVal

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

Constructor Detail

VariableJSlider

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


VariableJSlider

public VariableJSlider(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

VariableJSlider

public VariableJSlider(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

VariableJSlider

public VariableJSlider(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 slider 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 slider.HORIZONTAL or slider.VERTICAL. It specifies whether this is a horizontal or vertical slider. 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 VariableJSlider.
min - minimum value for slider.
max - maximum value for slider.
p - register VariableJSlider with this Parser.
intervals - discrete positions on slider.
orientation - slider.HORIZONTAL or slider.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 VariableJSlider'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 VariableJSlider.

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 slider 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 VariableJSlider 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 slider. 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 slider. 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 slider. 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 slider. 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 VariableJSlider. (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 slider 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

getIntervals

public int getIntervals()
Get the number of discrete slider steps

Returns:
the intervals

setIntervals

public void setIntervals(int intervals)
Set the number of discrete slider steps

Parameters:
intervals - the intervals to set

getIntervalsValue

public Value getIntervalsValue()
Get the number of slider steps as a Value object.

Returns:
the intervals as a Value

setIntervalsValue

public void setIntervalsValue(Value v)
Set the number of slider steps as a Value object.

Parameters:
v - the Value for the number of steps