Class HexIntTextField

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Canvas
              |
              +--HexIntTextField
All Implemented Interfaces:
Accessible, EventListener, ImageObserver, KeyListener, MenuContainer, Serializable

public class HexIntTextField
extends Canvas
implements KeyListener

A light-weight component representing a text field which only accepts digits of its currently active number system, or six additional letters, in case of hexadecimal mode.

Author:
Christian Liebchen
See Also:
Serialized Form

Field Summary
static int BIN
           
private  int caretPosition
           
private static int CHARWIDTH
           
static int DEC
           
private static int DIST
           
private  Font font
           
private  String[] fontnames
           
private static int FONTSIZE
           
static int HEX
           
private  Vector listeners
           
private  int mode
           
static int OCT
           
private  StringBuffer shown
           
 
Fields inherited from class java.awt.Canvas
 
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
HexIntTextField()
           
 
Method Summary
private  boolean accept(char c)
          Tells whether a given character is allowed as input in the current mode of our HexIntTextField.
 void addActionListener(ActionListener l)
           
 void fireActionEvent(String command)
          Tells anyone interested in ActionEvents occuring on this HexIntTextField, when an event took place.
 int getMode()
          Provides the current mode of this HexIntTextField.
private  char getModifiedChar(char c)
          Provide ability to re-interprete characters before inserting them into our HexIntTextField.
 String getText()
           
 long getValue()
           
private  void init()
           
 void insertChar(char c)
          Tries to insert a given character at the current caret position.
 void keyPressed(KeyEvent e)
          Location of our main work.
 void keyReleased(KeyEvent e)
          Without effect.
 void keyTyped(KeyEvent e)
          Without effect.
 void moveEnd()
           
 void moveHome()
           
 void paint(Graphics g)
          Paints this Canvas to appear as a text field!
 void removeActionListener(ActionListener l)
           
 void removeChar(boolean forward)
          Removes the character left or right beside the current caret position.
 void setMode(int m)
          Sets the new mode for this HexIntTextField, ie the base of the number system to be modelled by this text field.
 void setText(String text)
          Sets the text for this HexIntTextField.
 void setValue(long value)
           
 void stepCaret(boolean forward)
          Moves the caret one step left of right.
 
Methods inherited from class java.awt.Canvas
addNotify, createBufferStrategy, createBufferStrategy, getAccessibleContext, getBufferStrategy, update
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

FONTSIZE

private static final int FONTSIZE
See Also:
Constant Field Values

CHARWIDTH

private static final int CHARWIDTH
See Also:
Constant Field Values

DIST

private static final int DIST
See Also:
Constant Field Values

BIN

public static final int BIN
See Also:
Constant Field Values

OCT

public static final int OCT
See Also:
Constant Field Values

DEC

public static final int DEC
See Also:
Constant Field Values

HEX

public static final int HEX
See Also:
Constant Field Values

shown

private StringBuffer shown

caretPosition

private int caretPosition

font

private Font font

fontnames

private String[] fontnames

listeners

private Vector listeners

mode

private int mode
Constructor Detail

HexIntTextField

public HexIntTextField()
Method Detail

init

private void init()

getText

public String getText()

setText

public void setText(String text)
             throws IllegalArgumentException
Sets the text for this HexIntTextField.

Parameters:
text - the new text for this HexIntTextField
Throws:
IllegalArgumentException - if an invalid String is given, where currently we do only accept Strings encoding integers in binary, octal, decimal, or hexadecimal representation, see getMode(), with an optional minus sign at the beginning
See Also:
getMode()

getValue

public long getValue()

setValue

public void setValue(long value)

getMode

public int getMode()
Provides the current mode of this HexIntTextField.

Returns:
the current mode of this HexIntTextField, ie the base of the number system, but symbolic constants are used
See Also:
BIN, OCT, DEC, HEX

setMode

public void setMode(int m)
             throws IllegalArgumentException
Sets the new mode for this HexIntTextField, ie the base of the number system to be modelled by this text field.

Parameters:
m - the new mode for this HexIntTextField, where symbolic constants should be used
Throws:
IllegalArgumentException - if an unknown mode is given, where known modes currently are BIN, OCT, DEC, and HEX
See Also:
BIN, OCT, DEC, HEX

addActionListener

public void addActionListener(ActionListener l)

removeActionListener

public void removeActionListener(ActionListener l)

fireActionEvent

public void fireActionEvent(String command)
Tells anyone interested in ActionEvents occuring on this HexIntTextField, when an event took place. More specifically, calls actionPerformed for the registered listeners.

Parameters:
command - describes the event that took place
See Also:
ActionListener.actionPerformed(ActionEvent)

getModifiedChar

private char getModifiedChar(char c)
Provide ability to re-interprete characters before inserting them into our HexIntTextField.

Parameters:
c - the character to be re-interpreted occasionally
Returns:
c in most cases, currently only changes minore letters into capitals

accept

private boolean accept(char c)
Tells whether a given character is allowed as input in the current mode of our HexIntTextField.
Notice that we don't know anything about the position to insert the given character, e.g. a minus in the middle of a number will not make any sense. But as minus signs are accepted somewhere this method will return true for them.

Parameters:
c - the character to be inserted into our field
Returns:
true iff c=='-', c in {'A',...,'F'} and we are in HEX mode, or c in {'0',...,MIN(mode, 10)-1}

insertChar

public void insertChar(char c)
                throws IllegalArgumentException
Tries to insert a given character at the current caret position. The caret steps one forward.

Parameters:
c - the character to be inserted at the caret position
Throws:
IllegalArgumentException - if
  • the modified version of the given character is not accepted
  • a minus sign is tried to be inserted elsewhere but not at the beginning
  • any sign is tried to be inserted before a leading minus sign
See Also:
getModifiedChar(char), accept(char)

stepCaret

public void stepCaret(boolean forward)
Moves the caret one step left of right.
Notice that this method is robust, ie doesn't move beyond the text field.

Parameters:
forward - moves the caret right, iff true

removeChar

public void removeChar(boolean forward)
Removes the character left or right beside the current caret position. If forward==false the caret steps one backward.
Notice that this method is robust, ie doesn't erase outside text field.

Parameters:
forward - removes the character right of the caret, iff true

moveHome

public void moveHome()

moveEnd

public void moveEnd()

keyPressed

public void keyPressed(KeyEvent e)
Location of our main work. Splits into at most one method of navigation, erasing, actionning, or insertion functionality.

Specified by:
keyPressed in interface KeyListener
See Also:
moveEnd(), moveHome(), stepCaret(boolean), removeChar(boolean), fireActionEvent(String), insertChar(char)

keyTyped

public void keyTyped(KeyEvent e)
Without effect.

Specified by:
keyTyped in interface KeyListener

keyReleased

public void keyReleased(KeyEvent e)
Without effect.

Specified by:
keyReleased in interface KeyListener

paint

public void paint(Graphics g)
Paints this Canvas to appear as a text field!

Overrides:
paint in class Canvas