com.jstatcom.component
Class NumSelector

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.text.JTextComponent
                  extended by javax.swing.JTextField
                      extended by com.jstatcom.component.IValTextField
                          extended by com.jstatcom.component.NumSelector
All Implemented Interfaces:
SymbolListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible, javax.swing.Scrollable, javax.swing.SwingConstants

public final class NumSelector
extends IValTextField
implements SymbolListener

This component allows for number input that is automatically checked according to a NumberRange object. The range can be specified by constants or via symbol names for the upper and lower bounds. By default the number is stored in a symbol of type NUMBER, but this can be changed to INT. If the underlying symbol is of the wrong type, then an exception is thrown. The type of the bounds must be the same as the type for the number.

Usage Note:
If the number format type is set to NumberFormatTypes.DEFAULT, then the precision set for this selector defines, how displayed numbers are rounded. Any additional precision is lost, after a number was selected. If, for example, only int numbers should be possible, the precision can be set to 0, which is also the default. The resulting number is then stored as a double that is equal to an int.
If the number format type is set to NumberFormatTypes.SCIENTIFIC, then the precision is used only for the display. No rounding is performed and a precision of 0 does not mean that values are equal to an int.

This class fires a "number" property change, if a new number was set.

Author:
Markus Kraetzig
See Also:
NumberRange, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.text.JTextComponent
javax.swing.text.JTextComponent.AccessibleJTextComponent, javax.swing.text.JTextComponent.DropLocation, javax.swing.text.JTextComponent.KeyBinding
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.BaselineResizeBehavior
 
Field Summary
 
Fields inherited from class javax.swing.JTextField
notifyAction
 
Fields inherited from class javax.swing.text.JTextComponent
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY
 
Fields inherited from class javax.swing.JComponent
TOOL_TIP_TEXT_KEY, 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
NumSelector()
          Default constructor initializes range to (-Infinity,+Infinity) and sets the displayed value to 0.
 
Method Summary
 void addNotify()
          Overrides addNotify to retrieve data objects as soon as the component hierarchy is established.
 int getIntNumber()
          Gets the currently displayed number as an int.
 double getNumber()
          Gets the currently displayed number as double.
 NumberFormatTypes getNumberFormatType()
          Gets the current number format type.
 NumberRange getNumberRange()
          Gets the range that the input is checked against.
 int getPrecision()
          Gets the precision for the displayed numbers.
 java.lang.String getRangeExpr()
          Gets the current number range displayed as a string, for example [1,100] or ["lowerBound_p", 20].
 java.lang.String getSymbolName()
          Gets the symbol identifier for the data object of the selected number.
 Scope getSymbolScope()
          Gets the symbol scope.
 void handleError(java.lang.String msg)
          Error handling routine if validation failed.
 boolean isIntType()
          Gets whether the underlying symbol is stored as JSCTypes.INT or as JSCTypes.NUMBER.
 void setIntType(boolean b)
          Sets whether the underlying symbol is stored as JSCTypes.INT or as JSCTypes.NUMBER.
 void setNumber(double val)
          Sets the currently displayed number with checking whether it conforms to the specified range.
 void setNumberFormatType(NumberFormatTypes newNumberFormatType)
          Sets the current number format type.
 void setNumberRange(NumberRange newNumberRange)
          Sets the range that the number input is checked against.
 void setPrecision(int newPrecision)
          Sets number of digits right to decimal point.
 void setRangeExpr(java.lang.String expr)
          Sets the range to be checked against by specifying a string of the form ["(", "["] lowerBound "," upperBound + ["]", ")"], where lowerbound/upperBound are either a number or a symbolname, for example [lowerBound_p, upperBound_p] or (0, upperBound_x] or [1,10].
 void setSymbolName(java.lang.String newSymbolName)
          Sets the symbol name to retrieve and store data from.
 void setSymbolScope(Scope scope)
          Sets the symbol scope.
 java.lang.String validateInput()
          Validates input against the specified range whenever necessary.
 void valueChanged(SymbolEvent evt)
          Updates display when symbol changed.
 
Methods inherited from class com.jstatcom.component.IValTextField
isEditing, setTextCarefully
 
Methods inherited from class javax.swing.JTextField
addActionListener, getAccessibleContext, getAction, getActionListeners, getActions, getColumns, getHorizontalAlignment, getHorizontalVisibility, getPreferredSize, getScrollOffset, getUIClassID, isValidateRoot, postActionEvent, removeActionListener, scrollRectToVisible, setAction, setActionCommand, setColumns, setDocument, setFont, setHorizontalAlignment, setScrollOffset
 
Methods inherited from class javax.swing.text.JTextComponent
addCaretListener, addInputMethodListener, addKeymap, copy, cut, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getDropLocation, getDropMode, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getPrintable, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, moveCaretPosition, paste, print, print, print, read, removeCaretListener, removeKeymap, removeNotify, replaceSelection, select, selectAll, setCaret, setCaretColor, setCaretPosition, setComponentOrientation, setDisabledTextColor, setDragEnabled, setDropMode, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setText, setUI, updateUI, viewToModel, write
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, paint, paintImmediately, paintImmediately, print, printAll, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, 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, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, contains, createImage, createImage, createVolatileImage, createVolatileImage, dispatchEvent, enable, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, 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, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

NumSelector

public NumSelector()
Default constructor initializes range to (-Infinity,+Infinity) and sets the displayed value to 0.

Method Detail

valueChanged

public void valueChanged(SymbolEvent evt)
Updates display when symbol changed. This method does nothing if the symbol is cleared.

Specified by:
valueChanged in interface SymbolListener
Parameters:
evt - the symbol event

addNotify

public void addNotify()
Overrides addNotify to retrieve data objects as soon as the component hierarchy is established.

Overrides:
addNotify in class IValTextField

getIntNumber

public int getIntNumber()
Gets the currently displayed number as an int.

Returns:
current number

getNumber

public double getNumber()
Gets the currently displayed number as double.

Returns:
the selected number as double

getNumberFormatType

public NumberFormatTypes getNumberFormatType()
Gets the current number format type.

Returns:
the format type in use

getNumberRange

public NumberRange getNumberRange()
Gets the range that the input is checked against.

Returns:
current number range

getPrecision

public int getPrecision()
Gets the precision for the displayed numbers.

Returns:
number count right to the decimal point

getRangeExpr

public java.lang.String getRangeExpr()
Gets the current number range displayed as a string, for example [1,100] or ["lowerBound_p", 20].

Returns:
string representation of number range

getSymbolName

public java.lang.String getSymbolName()
Gets the symbol identifier for the data object of the selected number.

Returns:
the symbol name

handleError

public void handleError(java.lang.String msg)
Error handling routine if validation failed. Displays error message and sets back number value to the last valid one..

Overrides:
handleError in class IValTextField
Parameters:
msg - an error message

setNumber

public void setNumber(double val)
Sets the currently displayed number with checking whether it conforms to the specified range. This method fires a "number" property change with the old value and the changed value, if a new number was set.

Parameters:
val - the number to be displayed
Throws:
java.lang.IllegalArgumentException - if val is not contained in range

setNumberFormatType

public void setNumberFormatType(NumberFormatTypes newNumberFormatType)
Sets the current number format type.

Parameters:
newNumberFormatType - format type to use

setNumberRange

public void setNumberRange(NumberRange newNumberRange)
Sets the range that the number input is checked against. The currently displayed number is automatically validated against the new range. If the current number is not valid, it automatically set to a legal value within newNumberRange. This also updates the symbol.

Parameters:
newNumberRange - the number range for validation, if null then a range of (-Infinity,+Infinity) is set.

setPrecision

public void setPrecision(int newPrecision)
Sets number of digits right to decimal point.

Parameters:
newPrecision - new number precision
Throws:
java.lang.IllegalArgumentException - if newPrecision < 0

setRangeExpr

public void setRangeExpr(java.lang.String expr)
Sets the range to be checked against by specifying a string of the form ["(", "["] lowerBound "," upperBound + ["]", ")"], where lowerbound/upperBound are either a number or a symbolname, for example [lowerBound_p, upperBound_p] or (0, upperBound_x] or [1,10].

If the range is specified without symbol names, it is set immediately. If the range depends on symbols to be loaded from a symbol table, this is postponed to the next FOCUS_GAINED event. In the meantime, the default non-validating (-Infinity,Infinity) range is used.

Parameters:
expr - the expression to be parsed
Throws:
java.lang.IllegalArgumentException - if expr == null or if it cannot be parsed to a valid range

setSymbolName

public void setSymbolName(java.lang.String newSymbolName)
Sets the symbol name to retrieve and store data from.

Parameters:
newSymbolName - the symbol name
Throws:
java.lang.IllegalArgumentException - if (JSCConstants.isValidName(newSymbolName) != null)

validateInput

public java.lang.String validateInput()
Validates input against the specified range whenever necessary. This method is invoked by the superclass and does not need to be called manually.

Specified by:
validateInput in class IValTextField
Returns:
null if ok, an error message otherwise

getSymbolScope

public Scope getSymbolScope()
Gets the symbol scope.

Returns:
scope

setSymbolScope

public void setSymbolScope(Scope scope)
Sets the symbol scope.

Parameters:
scope -
Throws:
java.lang.IllegalArgumentException - if (scope == null)

isIntType

public boolean isIntType()
Gets whether the underlying symbol is stored as JSCTypes.INT or as JSCTypes.NUMBER.

Returns:
true if JSCTypes.INT

setIntType

public void setIntType(boolean b)
Sets whether the underlying symbol is stored as JSCTypes.INT or as JSCTypes.NUMBER. The default is JSCTypes.NUMBER. It will only take effect after a new symbol name or scope has been set.

Parameters:
b - if true then JSCTypes.INT if false then JSCTypes.NUMBER