de.matthiasmann.twl
Class EditField

java.lang.Object
  extended by de.matthiasmann.twl.Widget
      extended by de.matthiasmann.twl.EditField

public class EditField
extends Widget

A text edit control.

It offers single or multi line editing, auto completion, syntax highlighting and info boxes.

The single line editing (default mode) uses internal scrolling using the cursor position.

The multi line version uses internal horizontal scrolling, but for vertical scrolling it should be wrapped into a ScrollPane.

NOTE: The EditField font is controlled by the sub widget "renderer".


Nested Class Summary
static interface EditField.Callback
           
protected  class EditField.ModelChangeListener
           
protected  class EditField.TextRenderer
           
 
Nested classes/interfaces inherited from class de.matthiasmann.twl.Widget
Widget.OffscreenMouseAdjustments, Widget.RenderOffscreen
 
Field Summary
static AnimationState.StateKey STATE_CURSOR_MOVED
           
static AnimationState.StateKey STATE_ERROR
           
static AnimationState.StateKey STATE_HOVER
           
static AnimationState.StateKey STATE_READONLY
           
 
Fields inherited from class de.matthiasmann.twl.Widget
STATE_DISABLED, STATE_HAS_FOCUSED_CHILD, STATE_HAS_OPEN_POPUPS, STATE_KEYBOARD_FOCUS
 
Constructor Summary
EditField()
           
EditField(AnimationState parentAnimationState)
          Creates a new EditField with an optional parent animation state.
EditField(AnimationState parentAnimationState, EditFieldModel editFieldModel)
          Creates a new EditField with an optional parent animation state.
 
Method Summary
 void addCallback(EditField.Callback cb)
           
protected  void afterAddToGUI(GUI gui)
          Called after this widget has been added to a GUI tree.
protected  void applyTheme(ThemeInfo themeInfo)
          Apply the given theme.
protected  void applyThemeEditField(ThemeInfo themeInfo)
           
protected  void beforeRemoveFromGUI(GUI gui)
          Called when this widget is removed from the GUI tree.
protected  int computeCursorPosFromX(int x, int lineStart)
           
protected  int computeCursorPosFromX(int x, int lineStart, int lineEnd)
           
protected  int computeLineEnd(int cursorPos)
           
protected  int computeLineNumber(int cursorPos)
           
protected  int computeLineStart(int cursorPos)
           
protected  int computeRelativeCursorPositionX(int cursorPos)
           
protected  int computeRelativeCursorPositionY(int cursorPos)
           
 void copyToClipboard()
           
protected  Menu createPopupMenu()
           
 void cutToClipboard()
           
protected  void deleteNext()
           
protected  void deletePrev()
           
protected  void deleteSelection()
           
 void disableStringAttributes()
           
protected  void doCallback(int key)
           
 void duplicateLineDown()
           
 EditFieldAutoCompletionWindow getAutoCompletionWindow()
           
 int getColumns()
           
 int getCursorPos()
           
protected  int getCursorPosFromMouse(int x, int y)
           
protected  Font getFont()
           
protected  int getLineHeight()
           
 int getMaxTextLength()
           
 int getMinHeight()
          Returns the minimum height of the widget.
 int getMinWidth()
          Returns the minimum width of the widget.
 StringModel getModel()
           
 char getPasswordChar()
           
 int getPreferredInnerHeight()
          Computes the preferred inner height (the size of the widget without the border) The default implementation uses the current position of the children.
 int getPreferredInnerWidth()
          Computes the preferred inner width (the size of the widget without the border) The default implementation uses the current position of the children.
 java.lang.String getSelectedText()
           
 StringAttributes getStringAttributes()
           
 java.lang.String getText()
           
 int getTextLength()
           
 java.lang.Object getTooltipContent()
          Returns the currently set tooltip content.
 boolean handleEvent(Event evt)
          Called when an event occurred that this widget could be interested in.
protected  boolean hasFocusOrPopup()
           
 boolean hasSelection()
           
protected  void insertChar(char ch)
           
 void insertText(java.lang.String str)
           
 boolean isAutoCompletionOnSetText()
           
 boolean isForwardUnhandledKeysToCallback()
           
 boolean isMultiLine()
           
 boolean isPasswordMasking()
           
 boolean isReadOnly()
           
 boolean isScrollToCursorOnSizeChange()
           
protected  void keyboardFocusGained()
          Called when this widget has gained the keyboard focus.
protected  void keyboardFocusLost()
          Called when this widget has lost the keyboard focus.
protected  void layout()
          Called when the layoutInvalid flag is set.
protected  void modelChanged()
           
protected  void moveCursor(int dir, boolean select)
           
protected  void moveCursorY(int dir, boolean select)
           
protected  void paintOverlay(GUI gui)
          Paints the overlay image of this widget.
 void pasteFromClipboard()
           
protected  void positionChanged()
          Called when the position of this widget was changed.
 void removeCallback(EditField.Callback cb)
           
protected  void scrollToCursor(boolean force)
           
 void selectAll()
           
protected  void selectWordFromMouse(int index)
           
 void setAutoCompletion(AutoCompletionDataSource dataSource)
          Installs a new auto completion window with the given data source.
 void setAutoCompletion(AutoCompletionDataSource dataSource, java.util.concurrent.ExecutorService executorService)
          Installs a new auto completion window with the given data source.
 void setAutoCompletionOnSetText(boolean autoCompletionOnSetText)
          Controls if a call to setText() should trigger auto completion or not.
 void setAutoCompletionWindow(EditFieldAutoCompletionWindow window)
           
 void setColumns(int columns)
          This is used to determine the desired width of the EditField based on it's font and the character 'X'
 void setCursorPos(int pos)
           
protected  void setCursorPos(int pos, boolean select)
           
 void setErrorMessage(java.lang.Object errorMsg)
           
 void setForwardUnhandledKeysToCallback(boolean forwardUnhandledKeysToCallback)
          Controls if unhandled key presses are forwarded to the callback or not.
 void setMaxTextLength(int maxTextLength)
           
 void setModel(StringModel model)
           
 void setMultiLine(boolean multiLine)
          Controls multi line editing.
 void setPasswordChar(char passwordChar)
           
 void setPasswordMasking(boolean passwordMasking)
           
 void setReadOnly(boolean readOnly)
           
 void setScrollToCursorOnSizeChange(boolean scrollToCursorOnSizeChange)
           
 void setSelection(int start, int end)
           
 void setText(java.lang.String text)
          Set a new text for this EditField.
protected  void showPopupMenu(Event evt)
           
protected  void updateAutoCompletion()
           
protected  void updateSelection()
           
 
Methods inherited from class de.matthiasmann.twl.Widget
add, addActionMapping, addPropertyChangeListener, addPropertyChangeListener, adjustSize, allChildrenRemoved, applyThemeBackground, applyThemeBorder, applyThemeInputMap, applyThemeMaxSize, applyThemeMinSize, applyThemeMouseCursor, applyThemeOffscreenExtra, applyThemeOverlay, applyThemeTooltip, borderChanged, canAcceptKeyboardFocus, childAdded, childInvalidateLayout, childRemoved, childVisibilityChanged, computeSize, destroy, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, focusFirstChild, focusLastChild, focusNextChild, focusPrevChild, getActionMap, getAnimationState, getBackground, getBorderBottom, getBorderHorizontal, getBorderLeft, getBorderRight, getBorderTop, getBorderVertical, getBottom, getChild, getChildAt, getChildIndex, getGUI, getHeight, getInnerBottom, getInnerHeight, getInnerRight, getInnerWidth, getInnerX, getInnerY, getInputMap, getKeyboardFocusOrder, getMaxHeight, getMaxWidth, getMouseCursor, getMouseCursor, getNumChildren, getOffscreenExtraBottom, getOffscreenExtraLeft, getOffscreenExtraRight, getOffscreenExtraTop, getOrCreateActionMap, getOverlay, getParent, getPreferredHeight, getPreferredWidth, getRenderOffscreen, getRight, getRootWidget, getTheme, getThemePath, getThemeTooltipContent, getTintAnimator, getTooltipContentAt, getWidgetAt, getWidth, getX, getY, giveupKeyboardFocus, handleKeyStrokeAction, hasKeyboardFocus, hasOpenPopups, hasSharedAnimationState, insertChild, invalidateLayout, invalidateLayoutLocally, isAbsoluteTheme, isClip, isDepthFocusTraversal, isEnabled, isFocusKeyEnabled, isInside, isLocallyEnabled, isMouseInside, isVisible, keyboardFocusChildChanged, keyboardFocusGained, layoutChildFullInnerArea, layoutChildrenFullInnerArea, moveChild, paint, paintBackground, paintChild, paintChildren, paintDragOverlay, paintWidget, reapplyTheme, removeAllChildren, removeChild, removeChild, removePropertyChangeListener, removePropertyChangeListener, requestKeyboardFocus, requestKeyboardFocus, resetTooltip, setActionMap, setBackground, setBorderSize, setBorderSize, setBorderSize, setBorderSize, setCanAcceptKeyboardFocus, setClip, setDepthFocusTraversal, setEnabled, setFocusKeyEnabled, setInnerSize, setInputMap, setMaxSize, setMinSize, setMouseCursor, setOffscreenExtra, setOffscreenExtra, setOverlay, setPosition, setRenderOffscreen, setSize, setTheme, setTintAnimator, setTooltipContent, setVisible, sizeChanged, updateTintAnimation, updateTooltip, validateLayout, widgetDisabled
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STATE_ERROR

public static final AnimationState.StateKey STATE_ERROR

STATE_READONLY

public static final AnimationState.StateKey STATE_READONLY

STATE_HOVER

public static final AnimationState.StateKey STATE_HOVER

STATE_CURSOR_MOVED

public static final AnimationState.StateKey STATE_CURSOR_MOVED
Constructor Detail

EditField

public EditField(AnimationState parentAnimationState,
                 EditFieldModel editFieldModel)
Creates a new EditField with an optional parent animation state. Unlike other widgets which use the passed animation state directly, the EditField always creates it's animation state with the passed one as parent.

Parameters:
parentAnimationState -
editFieldModel - the edit field model to use
See Also:
AnimationState.AnimationState(de.matthiasmann.twl.AnimationState)

EditField

public EditField(AnimationState parentAnimationState)
Creates a new EditField with an optional parent animation state. Unlike other widgets which use the passed animation state directly, the EditField always creates it's animation state with the passed one as parent.

Parameters:
parentAnimationState -
See Also:
AnimationState.AnimationState(de.matthiasmann.twl.AnimationState)

EditField

public EditField()
Method Detail

addCallback

public void addCallback(EditField.Callback cb)

removeCallback

public void removeCallback(EditField.Callback cb)

isForwardUnhandledKeysToCallback

public boolean isForwardUnhandledKeysToCallback()

setForwardUnhandledKeysToCallback

public void setForwardUnhandledKeysToCallback(boolean forwardUnhandledKeysToCallback)
Controls if unhandled key presses are forwarded to the callback or not. Default is false. If set to true then the EditField will consume all key presses.

Parameters:
forwardUnhandledKeysToCallback - true if unhandled keys should be forwarded to the callbacks

isAutoCompletionOnSetText

public boolean isAutoCompletionOnSetText()

setAutoCompletionOnSetText

public void setAutoCompletionOnSetText(boolean autoCompletionOnSetText)
Controls if a call to setText() should trigger auto completion or not. Default is true.

Parameters:
autoCompletionOnSetText - true if setText() should trigger auto completion
See Also:
setText(java.lang.String)

isScrollToCursorOnSizeChange

public boolean isScrollToCursorOnSizeChange()

setScrollToCursorOnSizeChange

public void setScrollToCursorOnSizeChange(boolean scrollToCursorOnSizeChange)

doCallback

protected void doCallback(int key)

isPasswordMasking

public boolean isPasswordMasking()

setPasswordMasking

public void setPasswordMasking(boolean passwordMasking)

getPasswordChar

public char getPasswordChar()

setPasswordChar

public void setPasswordChar(char passwordChar)

getColumns

public int getColumns()

setColumns

public void setColumns(int columns)
This is used to determine the desired width of the EditField based on it's font and the character 'X'

Parameters:
columns - number of characters
Throws:
java.lang.IllegalArgumentException - if columns < 0

isMultiLine

public boolean isMultiLine()

setMultiLine

public void setMultiLine(boolean multiLine)
Controls multi line editing. Default is false (single line editing). Disabling multi line editing when multi line text is present will clear the text.

Parameters:
multiLine - true for multi line editing.

getModel

public StringModel getModel()

setModel

public void setModel(StringModel model)

setText

public void setText(java.lang.String text)
Set a new text for this EditField. If the new text is longer then getMaxTextLength() then it is truncated. The selection is cleared. The cursor is positioned at the end of the new text (single line) or at the start of the text (multi line). If a model is set, then the model is also updated.

Parameters:
text - the new text
Throws:
java.lang.NullPointerException - if text is null
See Also:
setMultiLine(boolean)

getText

public java.lang.String getText()

getStringAttributes

public StringAttributes getStringAttributes()

disableStringAttributes

public void disableStringAttributes()

getSelectedText

public java.lang.String getSelectedText()

hasSelection

public boolean hasSelection()

getCursorPos

public int getCursorPos()

getTextLength

public int getTextLength()

isReadOnly

public boolean isReadOnly()

setReadOnly

public void setReadOnly(boolean readOnly)

insertText

public void insertText(java.lang.String str)

pasteFromClipboard

public void pasteFromClipboard()

copyToClipboard

public void copyToClipboard()

cutToClipboard

public void cutToClipboard()

duplicateLineDown

public void duplicateLineDown()

getMaxTextLength

public int getMaxTextLength()

setMaxTextLength

public void setMaxTextLength(int maxTextLength)

afterAddToGUI

protected void afterAddToGUI(GUI gui)
Description copied from class: Widget
Called after this widget has been added to a GUI tree.

Overrides:
afterAddToGUI in class Widget
Parameters:
gui - the GUI object - same as getGUI()
See Also:
Widget.getGUI()

beforeRemoveFromGUI

protected void beforeRemoveFromGUI(GUI gui)
Description copied from class: Widget
Called when this widget is removed from the GUI tree. After this call getGUI() will return null.

Overrides:
beforeRemoveFromGUI in class Widget
Parameters:
gui - the GUI object - same as getGUI()
See Also:
Widget.getGUI()

applyTheme

protected void applyTheme(ThemeInfo themeInfo)
Description copied from class: Widget
Apply the given theme. This method also calls invalidateLayout()

Overrides:
applyTheme in class Widget
Parameters:
themeInfo - The theme info for this widget

applyThemeEditField

protected void applyThemeEditField(ThemeInfo themeInfo)

layout

protected void layout()
Description copied from class: Widget
Called when the layoutInvalid flag is set. The default implementation does nothing.

Overrides:
layout in class Widget

positionChanged

protected void positionChanged()
Description copied from class: Widget
Called when the position of this widget was changed. The default implementation does nothing. Child positions are already updated to retain the absolute coordinate system. This has the side effect of firing child's positionChanged before the parent's.

Overrides:
positionChanged in class Widget

getMinWidth

public int getMinWidth()
Description copied from class: Widget
Returns the minimum width of the widget. Layout manager will allocate atleast the minimum width to a widget even when the container is not big enough. The default implementation will not return values smaller then the current border width.

Overrides:
getMinWidth in class Widget
Returns:
the minimum width of the widget

getMinHeight

public int getMinHeight()
Description copied from class: Widget
Returns the minimum height of the widget. Layout manager will allocate atleast the minimum height to a widget even when the container is not big enough. The default implementation will not return values smaller then the current border width.

Overrides:
getMinHeight in class Widget
Returns:
the minimum height of the widget

getPreferredInnerWidth

public int getPreferredInnerWidth()
Description copied from class: Widget
Computes the preferred inner width (the size of the widget without the border) The default implementation uses the current position of the children. It is highly recommended to override this method as the default implementation lead to unstable layouts. The default behavior might change in the future to provide a better default behavior.

Overrides:
getPreferredInnerWidth in class Widget
Returns:
the preferred inner width

getPreferredInnerHeight

public int getPreferredInnerHeight()
Description copied from class: Widget
Computes the preferred inner height (the size of the widget without the border) The default implementation uses the current position of the children. It is highly recommended to override this method as the default implementation lead to unstable layouts. The default behavior might change in the future to provide a better default behavior.

Overrides:
getPreferredInnerHeight in class Widget
Returns:
the preferred inner height

setErrorMessage

public void setErrorMessage(java.lang.Object errorMsg)

getTooltipContent

public java.lang.Object getTooltipContent()
Description copied from class: Widget
Returns the currently set tooltip content.

Overrides:
getTooltipContent in class Widget
Returns:
the currently set tooltip content. Can be null.

setAutoCompletionWindow

public void setAutoCompletionWindow(EditFieldAutoCompletionWindow window)

getAutoCompletionWindow

public EditFieldAutoCompletionWindow getAutoCompletionWindow()

setAutoCompletion

public void setAutoCompletion(AutoCompletionDataSource dataSource)
Installs a new auto completion window with the given data source.

Parameters:
dataSource - the data source used for auto completion - can be null
See Also:
EditFieldAutoCompletionWindow.EditFieldAutoCompletionWindow(de.matthiasmann.twl.EditField, de.matthiasmann.twl.model.AutoCompletionDataSource)

setAutoCompletion

public void setAutoCompletion(AutoCompletionDataSource dataSource,
                              java.util.concurrent.ExecutorService executorService)
Installs a new auto completion window with the given data source.

Parameters:
dataSource - the data source used for auto completion - can be null
executorService - the executorService used to execute the data source queries
See Also:
EditFieldAutoCompletionWindow.EditFieldAutoCompletionWindow(de.matthiasmann.twl.EditField, de.matthiasmann.twl.model.AutoCompletionDataSource, java.util.concurrent.ExecutorService)

handleEvent

public boolean handleEvent(Event evt)
Description copied from class: Widget
Called when an event occurred that this widget could be interested in.

The default implementation handles only keyboard events and delegates them to the child widget which has keyboard focus. If focusKey handling is enabled then this widget cycles the keyboard focus through it's children. If the key was not consumed by a child or focusKey and an inputMap is specified then the event is translated by the InputMap and handleKeyStrokeAction is called when a mapping was found.

If the widget wants to receive mouse events then it must return true for all mouse events except for MOUSE_WHEEL (which is optional) event. Otherwise the following mouse event are not send. Before mouse movement or button events are send a MOUSE_ENTERED event is send first.

Overrides:
handleEvent in class Widget
Parameters:
evt - The event - do not store this object - it may be reused
Returns:
true if the widget handled this event
See Also:
Widget.setFocusKeyEnabled(boolean), Widget.handleKeyStrokeAction(java.lang.String, de.matthiasmann.twl.Event), Widget.setInputMap(de.matthiasmann.twl.InputMap)

showPopupMenu

protected void showPopupMenu(Event evt)

createPopupMenu

protected Menu createPopupMenu()

moveCursor

protected void moveCursor(int dir,
                          boolean select)

moveCursorY

protected void moveCursorY(int dir,
                           boolean select)

setCursorPos

protected void setCursorPos(int pos,
                            boolean select)

updateSelection

protected void updateSelection()

setCursorPos

public void setCursorPos(int pos)

selectAll

public void selectAll()

setSelection

public void setSelection(int start,
                         int end)

selectWordFromMouse

protected void selectWordFromMouse(int index)

scrollToCursor

protected void scrollToCursor(boolean force)

insertChar

protected void insertChar(char ch)

deletePrev

protected void deletePrev()

deleteNext

protected void deleteNext()

deleteSelection

protected void deleteSelection()

modelChanged

protected void modelChanged()

hasFocusOrPopup

protected boolean hasFocusOrPopup()

getFont

protected Font getFont()

getLineHeight

protected int getLineHeight()

computeLineNumber

protected int computeLineNumber(int cursorPos)

computeLineStart

protected int computeLineStart(int cursorPos)

computeLineEnd

protected int computeLineEnd(int cursorPos)

computeRelativeCursorPositionX

protected int computeRelativeCursorPositionX(int cursorPos)

computeRelativeCursorPositionY

protected int computeRelativeCursorPositionY(int cursorPos)

getCursorPosFromMouse

protected int getCursorPosFromMouse(int x,
                                    int y)

computeCursorPosFromX

protected int computeCursorPosFromX(int x,
                                    int lineStart)

computeCursorPosFromX

protected int computeCursorPosFromX(int x,
                                    int lineStart,
                                    int lineEnd)

paintOverlay

protected void paintOverlay(GUI gui)
Description copied from class: Widget
Paints the overlay image of this widget.

Overrides:
paintOverlay in class Widget
Parameters:
gui - the GUI object
See Also:
Widget.paint(de.matthiasmann.twl.GUI)

keyboardFocusGained

protected void keyboardFocusGained()
Description copied from class: Widget
Called when this widget has gained the keyboard focus. The default implementation does nothing.

Overrides:
keyboardFocusGained in class Widget
See Also:
Widget.keyboardFocusGained(de.matthiasmann.twl.FocusGainedCause, de.matthiasmann.twl.Widget)

keyboardFocusLost

protected void keyboardFocusLost()
Description copied from class: Widget
Called when this widget has lost the keyboard focus. The default implementation does nothing.

Overrides:
keyboardFocusLost in class Widget

updateAutoCompletion

protected void updateAutoCompletion()