Class LWTextComponent

java.lang.Object
  |
  +--java.awt.Component
        |
        +--LWTextComponent
Direct Known Subclasses:
ActiveClient

public class LWTextComponent
extends Component
implements KeyListener, FocusListener

Implements a very simple lightweight text editing component. It lets the user edit a single line of text using the keyboard. The only special character that it knows about is backspace; all other characters are added to the text. Selections are not supported, so there's only a simple caret indicating the insertion point. The component also displays a component name above the editable text line, and draws a black frame whose thickness indicates whether the component has the focus.

The component can be initialized to enable or disable input through input methods. Other than that, it doesn't do anything to support input methods, so input method interaction (if any) will occur in a separate composition window. However, the component is designed to be easily extended with full input method support. It distinguishes between "displayed text" and "committed text" - here, they're the same, but in a subclass that supports on-the-spot input, the displayed text would be the combination of committed text and composed text. The component also uses TextLayout to draw the text, so it can be easily extended to handle input method highlights.

See Also:
Serialized Form

Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Constructor Summary
LWTextComponent(String name, boolean enableInputMethods)
          Constructs a LWTextComponent.
 
Method Summary
 void focusGained(FocusEvent event)
          Turns on drawing of the component's thicker frame and the caret.
 void focusLost(FocusEvent event)
          Turns off drawing of the component's thicker frame and the caret.
 TextHitInfo getCaret()
          Returns a text hit info indicating the current caret (insertion point).
 Rectangle getCaretRectangle()
          Returns a 0-width caret rectangle.
 Rectangle getCaretRectangle(TextHitInfo caret)
          Returns a 0-width caret rectangle for the given text index.
 AttributedCharacterIterator getCommittedText()
          Returns the text that the user has entered and committed.
 AttributedCharacterIterator getCommittedText(int beginIndex, int endIndex)
          Returns a subrange of the text that the user has entered and committed.
 int getCommittedTextLength()
          Returns the length of the text that the user has entered and committed.
 AttributedCharacterIterator getDisplayText()
          Returns the text that the user has entered.
 TextLayout getTextLayout()
          Returns a text layout for the text that the user has entered.
 Point getTextOrigin()
          Returns the origin of the text.
 void insertCharacter(char c)
          Inserts the given character at the end of the text.
 void invalidateTextLayout()
          Invalidates the cached text layout.
 void keyPressed(KeyEvent event)
          Ignores key pressed events.
 void keyReleased(KeyEvent event)
          Ignores key released events.
 void keyTyped(KeyEvent event)
          Handles the key typed event.
 void paint(Graphics g)
          Draws the component.
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getGraphics, getHeight, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getName, getParent, getPeer, getPreferredSize, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isDisplayable, isDoubleBuffered, isEnabled, isFocusTraversable, 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, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, remove, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFont, setForeground, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus, update, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LWTextComponent

public LWTextComponent(String name,
                       boolean enableInputMethods)
Constructs a LWTextComponent.
Parameters:
name - the component name to be displayed above the text
enableInputMethods - whether to enable input methods for this component
Method Detail

paint

public void paint(Graphics g)
Draws the component. The following items are drawn:
Overrides:
paint in class Component

getCommittedText

public AttributedCharacterIterator getCommittedText()
Returns the text that the user has entered and committed. Since this component does not support on-the-spot input, there's no composed text, so all text that has been entered is committed.
Returns:
an AttributedCharacterIterator for the text that the user has entered and committed

getCommittedText

public AttributedCharacterIterator getCommittedText(int beginIndex,
                                                    int endIndex)
Returns a subrange of the text that the user has entered and committed. Since this component does not support on-the-spot input, there's no composed text, so all text that has been entered is committed.
Parameters:
beginIndex - the index of the first character of the subrange
endIndex - the index of the character following the subrange
Returns:
an AttributedCharacterIterator for a subrange of the text that the user has entered and committed

getCommittedTextLength

public int getCommittedTextLength()
Returns the length of the text that the user has entered and committed. Since this component does not support on-the-spot input, there's no composed text, so all text that has been entered is committed.
Returns:
the length of the text that the user has entered and committed

getDisplayText

public AttributedCharacterIterator getDisplayText()
Returns the text that the user has entered. As TextLayout requires a font to be defined for each character, the default font is applied to the entire text. A subclass that supports on-the-spot input must override this method to include composed text.
Returns:
the text that the user has entered

getTextLayout

public TextLayout getTextLayout()
Returns a text layout for the text that the user has entered. This text layout is created from the text returned by getDisplayText. The text layout is cached until invalidateTextLayout is called.
Returns:
a text layout for the text that the user has entered, or null
See Also:
invalidateTextLayout(), getDisplayText()

invalidateTextLayout

public void invalidateTextLayout()
Invalidates the cached text layout. This must be called whenever the component's text is modified.
See Also:
getTextLayout()

getTextOrigin

public Point getTextOrigin()
Returns the origin of the text. This is the leftmost point on the baseline of the text.
Returns:
the origin of the text

getCaretRectangle

public Rectangle getCaretRectangle()
Returns a 0-width caret rectangle. This rectangle is derived from the caret returned by getCaret. getCaretRectangle returns null iff getCaret does.
Returns:
the caret rectangle, or null
See Also:
getCaret()

getCaretRectangle

public Rectangle getCaretRectangle(TextHitInfo caret)
Returns a 0-width caret rectangle for the given text index. It is calculated based on the text layout returned by getTextLayout, so this method can be used for the entire displayed text.
Parameters:
caret - the text index for which to calculate a caret rectangle
Returns:
the caret rectangle

getCaret

public TextHitInfo getCaret()
Returns a text hit info indicating the current caret (insertion point). This class always returns a caret at the end of the text that the user has entered. Subclasses may return a different caret or null.
Returns:
the caret, or null

insertCharacter

public void insertCharacter(char c)
Inserts the given character at the end of the text.
Parameters:
c - the character to be inserted

keyTyped

public void keyTyped(KeyEvent event)
Handles the key typed event. If the character is backspace, the last character is removed from the text that the user has entered. Otherwise, the character is appended to the text. Then, the text is redrawn.
Specified by:
keyTyped in interface KeyListener

keyPressed

public void keyPressed(KeyEvent event)
Ignores key pressed events.
Specified by:
keyPressed in interface KeyListener

keyReleased

public void keyReleased(KeyEvent event)
Ignores key released events.
Specified by:
keyReleased in interface KeyListener

focusGained

public void focusGained(FocusEvent event)
Turns on drawing of the component's thicker frame and the caret.
Specified by:
focusGained in interface FocusListener

focusLost

public void focusLost(FocusEvent event)
Turns off drawing of the component's thicker frame and the caret.
Specified by:
focusLost in interface FocusListener


Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1998 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.