com.metamatrix.toolbox.ui.widget
Class IntegerSpinner

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JPanel
                  extended by com.metamatrix.toolbox.ui.widget.IntegerSpinner
All Implemented Interfaces:
java.awt.event.FocusListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible, javax.swing.event.DocumentListener

public class IntegerSpinner
extends javax.swing.JPanel
implements javax.swing.event.DocumentListener, java.awt.event.FocusListener

The IntegerSpinner is a panel consisting of a JTextField and two buttons. The textfield allows only integer characters to be entered. It defaults to non-editable but can be changed to editable. The two buttons are the "up" arrow and the "down" arrow. The "up" arrow increments the value in the textfield and the "down" arrow decrements the value. An increment value passed in during constructions dictates how much is incremented or decremented.

For an editable textfield, if the value entered is greater than the maximum allowed value, the value will be set to the maximum. And if the value is less than the minimum allowed value, the value will be set to the minimum. If the spinner is wrapping and the next increment would cause the value to be greater than the maximum, the value is set to the minimum. If wrapping is off, the value would be set to the maximum. If wrapping is on and the increment would cause the value to be less than the minimum, the value is set to the maximum. If wrapping is off, the value would be set to the minimum. IntegerSpinner allows blanks to be set into the spinner

See Also:
Serialized Form

Nested Class Summary
 class IntegerSpinner.SpinnerKeyListener
           
 
Nested classes/interfaces inherited from class javax.swing.JPanel
javax.swing.JPanel.AccessibleJPanel
 
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  com.metamatrix.toolbox.ui.widget.IntegerSpinner.IgnoreButton btnDown
           
protected  com.metamatrix.toolbox.ui.widget.IntegerSpinner.IgnoreButton btnUp
           
protected  javax.swing.text.Document doc
          The document used in the textfield.
protected  boolean docUpdateInProgress
          Indicates if a document update is in progress.
protected  com.metamatrix.toolbox.ui.widget.IntegerSpinner.IntegerSpinnerMouseListener downMouseListener
           
protected  int increment
          The amount of each increment or decrement.
protected  int max
          The maximum allowed value.
protected  int min
          The minimum allowed value.
protected  int minWidth
           
protected  boolean pad
          Indicates if leading zeroes will be displayed.
protected  javax.swing.JPanel pnlButtons
           
protected  TextFieldWidget txf
           
protected  com.metamatrix.toolbox.ui.widget.IntegerSpinner.IntegerSpinnerMouseListener upMouseListener
           
protected  int value
          The current value.
protected  int width
          The length of a string representation of the maximum value.
protected  boolean wrap
          Indicates if the spinner should wrap from max to min (and vice versa) values.
 
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.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
IntegerSpinner(int theMin, int theMax, int theIncrement)
          Constructs an IntegerSpinner using the given parameters and sets the initial value to min.
IntegerSpinner(int theMin, int theMax, int theIncrement, int theInitialValue, boolean theWrapFlag)
          Constructs an IntegerSpinner using the given parameters.
 
Method Summary
 void addChangeListener(javax.swing.event.ChangeListener theListener)
           
 void addFocusListener(java.awt.event.FocusListener theListener)
           
 void changedUpdate(javax.swing.event.DocumentEvent theEvent)
          Required by the DocumentListener interface.
 boolean decrement()
           
 void focusGained(java.awt.event.FocusEvent theEvent)
          Needed by the FocusListener interface.
 void focusLost(java.awt.event.FocusEvent theEvent)
          Processes the given event by updating the value and the textfield of editable spinners.
 java.lang.String format()
          Gets a formatted string representation if padding is set.
 int getMaxValue()
          Gets the maximum value.
 int getMaxWidth()
           
 int getMinValue()
          Gets the minimum value.
 java.awt.Dimension getPreferredSize()
          Gets the preferred size of the spinner.
 java.lang.String getTextValue()
           
 java.lang.String getToolTipText()
           
 int getValue()
          Gets the current value.
 boolean increment()
          Increments the current value by the increment value this spinner was constructed with.
 void insertUpdate(javax.swing.event.DocumentEvent theEvent)
          Required by the DocumentListener interface.
 boolean isEditable()
           
 void keyListenerKeyPressed(java.awt.event.KeyEvent theEvent)
          Processes key events where the key pressed is either the up or down arrows.
static void main(java.lang.String[] theArgs)
           
 java.lang.String paramString()
           
 void removeChangeListener(javax.swing.event.ChangeListener theListener)
           
 void removeFocusListener(java.awt.event.FocusListener theListener)
           
 void removeUpdate(javax.swing.event.DocumentEvent theEvent)
          Required by the DocumentListener interface.
 void requestFocus()
          Requests focus to the spinner's textfield.
 void setEditable(boolean theEditableFlag)
          Modifies the editable state of the spinner's textfield.
 void setEnabled(boolean theEnableFlag)
          Sets the enable state.
 void setMaxValue(int theNewMax)
          Sets the maximum spinner value to the given value.
 void setMinValue(int newMin)
          Sets the minimum spinner value to the given value.
 void setPad(boolean pad)
          Sets the enable state.
 void setPreferredSize(java.awt.Dimension preferredSize)
          Set the preferred size of the spinner.
 void setReadonly(boolean value)
           
 boolean setValue(int newValue)
          Sets the current value.
 java.lang.String toString()
          Gets a formatted string representation.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, setUI, 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, 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, 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, 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, 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, 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, setName, setSize, setSize, show, show, size, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

doc

protected javax.swing.text.Document doc
The document used in the textfield.


docUpdateInProgress

protected boolean docUpdateInProgress
Indicates if a document update is in progress. This is needed because a write lock would occur when a setValue was called from outside this class. This causes the document to be updated and a setValue to be called again.


max

protected int max
The maximum allowed value.


min

protected int min
The minimum allowed value.


increment

protected int increment
The amount of each increment or decrement.


pad

protected boolean pad
Indicates if leading zeroes will be displayed.


value

protected int value
The current value.


width

protected int width
The length of a string representation of the maximum value.


wrap

protected boolean wrap
Indicates if the spinner should wrap from max to min (and vice versa) values.


minWidth

protected int minWidth

downMouseListener

protected com.metamatrix.toolbox.ui.widget.IntegerSpinner.IntegerSpinnerMouseListener downMouseListener

upMouseListener

protected com.metamatrix.toolbox.ui.widget.IntegerSpinner.IntegerSpinnerMouseListener upMouseListener

btnDown

protected com.metamatrix.toolbox.ui.widget.IntegerSpinner.IgnoreButton btnDown

btnUp

protected com.metamatrix.toolbox.ui.widget.IntegerSpinner.IgnoreButton btnUp

pnlButtons

protected javax.swing.JPanel pnlButtons

txf

protected TextFieldWidget txf
Constructor Detail

IntegerSpinner

public IntegerSpinner(int theMin,
                      int theMax,
                      int theIncrement)
Constructs an IntegerSpinner using the given parameters and sets the initial value to min.

Parameters:
theMin - the minimum value allowed
theMax - the maximum value allowed
theIncrement - the amount of increment

IntegerSpinner

public IntegerSpinner(int theMin,
                      int theMax,
                      int theIncrement,
                      int theInitialValue,
                      boolean theWrapFlag)
Constructs an IntegerSpinner using the given parameters.

Parameters:
theMin - the minimum value allowed
theMax - the maximum value allowed
theIncrement - the amount of increment
theInitialValue - the initial value
theWrapFlag - indicates if wrapping is used
Method Detail

addChangeListener

public void addChangeListener(javax.swing.event.ChangeListener theListener)

addFocusListener

public void addFocusListener(java.awt.event.FocusListener theListener)
Overrides:
addFocusListener in class java.awt.Component

changedUpdate

public void changedUpdate(javax.swing.event.DocumentEvent theEvent)
Required by the DocumentListener interface.

Specified by:
changedUpdate in interface javax.swing.event.DocumentListener
Parameters:
theEvent - the event being processed
See Also:
DocumentListener

decrement

public boolean decrement()

getMaxWidth

public int getMaxWidth()

increment

public boolean increment()
Increments the current value by the increment value this spinner was constructed with.


insertUpdate

public void insertUpdate(javax.swing.event.DocumentEvent theEvent)
Required by the DocumentListener interface.

Specified by:
insertUpdate in interface javax.swing.event.DocumentListener
Parameters:
theEvent - the event being processed
See Also:
DocumentListener

isEditable

public boolean isEditable()

focusGained

public void focusGained(java.awt.event.FocusEvent theEvent)
Needed by the FocusListener interface.

Specified by:
focusGained in interface java.awt.event.FocusListener

focusLost

public void focusLost(java.awt.event.FocusEvent theEvent)
Processes the given event by updating the value and the textfield of editable spinners.

Specified by:
focusLost in interface java.awt.event.FocusListener
Parameters:
theEvent - the event being processed

format

public java.lang.String format()
Gets a formatted string representation if padding is set. Otherwise the toString representation is returned.

Returns:
a string representation

getMaxValue

public int getMaxValue()
Gets the maximum value.

Returns:
the maximum value

getMinValue

public int getMinValue()
Gets the minimum value.

Returns:
the minimum value

getPreferredSize

public java.awt.Dimension getPreferredSize()
Gets the preferred size of the spinner. The height is set to the height of the textfield.

Overrides:
getPreferredSize in class javax.swing.JComponent
Returns:
the preferred size of this spinner

getTextValue

public java.lang.String getTextValue()

getToolTipText

public java.lang.String getToolTipText()
Overrides:
getToolTipText in class javax.swing.JComponent

getValue

public int getValue()
Gets the current value.

Returns:
the current value

keyListenerKeyPressed

public void keyListenerKeyPressed(java.awt.event.KeyEvent theEvent)
Processes key events where the key pressed is either the up or down arrows. The up arrow increments while the down arrow decrements the value in the textfield.

Parameters:
theEvent - the event being processed

removeChangeListener

public void removeChangeListener(javax.swing.event.ChangeListener theListener)

removeFocusListener

public void removeFocusListener(java.awt.event.FocusListener theListener)
Overrides:
removeFocusListener in class java.awt.Component

removeUpdate

public void removeUpdate(javax.swing.event.DocumentEvent theEvent)
Required by the DocumentListener interface.

Specified by:
removeUpdate in interface javax.swing.event.DocumentListener
Parameters:
theEvent - the event being processed
See Also:
DocumentListener

requestFocus

public void requestFocus()
Requests focus to the spinner's textfield.

Overrides:
requestFocus in class javax.swing.JComponent

paramString

public java.lang.String paramString()
Overrides:
paramString in class javax.swing.JPanel

setEditable

public void setEditable(boolean theEditableFlag)
Modifies the editable state of the spinner's textfield.

Parameters:
theEditableFlag - sets the editable state of the textfield

setEnabled

public void setEnabled(boolean theEnableFlag)
Sets the enable state.

Overrides:
setEnabled in class javax.swing.JComponent
Parameters:
theEnableFlag - indicates if this spinner should be enabled

setMaxValue

public void setMaxValue(int theNewMax)
Sets the maximum spinner value to the given value. If the given value is less than the minimum value, the maximum is set equal to the minimum value. If the current spinner value is greater than the new maximum value, the current value is set to the maximum value.

Parameters:
theNewMax - the proposed value for the maximum value

setMinValue

public void setMinValue(int newMin)
Sets the minimum spinner value to the given value. If the given value is greater than the maximum value, the minimum is set equal to the maximum value. If the current spinner value is less than the new minimum value, the current value is set to the minimum value.

Parameters:
newMin - the proposed value for the minimum value

setPad

public void setPad(boolean pad)
Sets the enable state.

Parameters:
pad - indicates if leading zeros are displayed

setPreferredSize

public void setPreferredSize(java.awt.Dimension preferredSize)
Set the preferred size of the spinner.

Overrides:
setPreferredSize in class javax.swing.JComponent
Parameters:
preferredSize - the preferred size

setReadonly

public void setReadonly(boolean value)

setValue

public boolean setValue(int newValue)
Sets the current value. If the value is less than the minimum and wrap is on, the value will be set to the maximum. If wrap is off, the value is set to the minimum. If the value is greater than the maximum and wrap is on, the value is set to the minimum. If wrap is off, the value is set to the maximum.

Parameters:
value - the proposed new current value
Returns:
true if the value was changed; false otherwise.

toString

public java.lang.String toString()
Gets a formatted string representation.

Overrides:
toString in class java.awt.Component
Parameters:
the - string representation
See Also:
format()

main

public static void main(java.lang.String[] theArgs)


Copyright © 2009. All Rights Reserved.