org.zaval.lw
Class LwTextField

java.lang.Object
  |
  +--org.zaval.util.ValidationObject
        |
        +--org.zaval.lw.LwCanvas
              |
              +--org.zaval.lw.LwLabel
                    |
                    +--org.zaval.lw.LwTextField
All Implemented Interfaces:
Cursorable, Drawable, java.util.EventListener, Layoutable, LwComponent, LwFocusListener, LwKeyListener, LwMouseListener, LwMouseMotionListener, PosInfo, PosListener, ScrollObj, TxtSelectionInfo, Validationable
Direct Known Subclasses:
LwMaskTextField

public class LwTextField
extends LwLabel
implements LwKeyListener, LwMouseListener, LwMouseMotionListener, LwFocusListener, Cursorable, PosInfo, PosListener, ScrollObj, TxtSelectionInfo

This class is a text field component. The library provides several text renders implementations that can be used as a face view of the component:

The list below describes some features of the component:

The component overrides the getOrigin method to organize scrolling of the content. The scrolling mechanism works right if the component will be inserted into a LwScrollPan component.

To listen when the cursor position has been changed use the pos controller as follow:

   public class Sample
   implements PosListener
   {
     ...
     public void init()
     {
       ...
       LwTextFiled tf = new LwTextFiled("Text");
       tf.getPosController().addPosListener(this);
       ...
     }
     public void posChanged(PosEvent e) {
       System.out.println("The old cursor location is - " + e.getPrevOffset());
     }
   }
 


Fields inherited from class org.zaval.lw.LwCanvas
bits, height, insets, parent, psHeight, psWidth, skins, width, x, y
 
Fields inherited from class org.zaval.util.ValidationObject
isValidFlag
 
Constructor Summary
LwTextField()
          Constructs a text filed component with no text.
LwTextField(java.lang.String s)
          Constructs a text field component with the specified text.
LwTextField(java.lang.String s, int maxCol)
          Constructs a single line text field component with the specified text and the maximal number of columns.
LwTextField(TextModel model)
          Constructs a text field component with the specified text model.
 
Method Summary
 boolean canHaveFocus()
          Specifies if the component can have focus.
protected  void drawCursor(java.awt.Graphics g)
          The method is used to paint the text cursor using the cursor view.
 void endDragged(LwMouseMotionEvent e)
          Invoked when the mouse ends dragged.
static java.awt.Point findNextWord(TextModel t, int line, int col, int d)
          Finds starting from the specified line and column next word in the given text model and returns the found word location.
 void focusGained(LwFocusEvent e)
          Invoked when the light weight component gained focus.
 void focusLost(LwFocusEvent e)
          Invoked when the light weight component lost focus.
 int getCursorType(LwComponent target, int x, int y)
          Gets the cursor type for the specified location of the given component.
 LwView getCursorView()
          Gets the cursor view that is used to paint the text cursor.
 java.awt.Point getEndSelection()
          Returns the last selection row and column
 int getLines()
          Gets the number of lines.
 int getLineSize(int i)
          Gets the line size for the specified line number.
 int getMaxOffset()
          Gets the maximal offset.
 java.awt.Point getOrigin()
          Returns an origin of the component.
 PosController getPosController()
          Gets the pos controller that manages the text cursor position.
 java.awt.Dimension getPreferredSize()
          Gets the preferred size of this component.
 java.awt.Color getSelectColor()
          Returns a color that is used to render selected text background.
 java.lang.String getSelectedText()
          Returns the selected text.
 java.awt.Point getSelectionOffsets()
          Gets the start and last selection offsets.
 java.awt.Point getSOLocation()
          Gets the scroll object location.
 java.awt.Dimension getSOSize()
          Gets the scroll object size.
 java.awt.Point getStartSelection()
          Returns the start selection row and column
static java.lang.String getSubString(TextModel t, java.awt.Point start, java.awt.Point end)
          Returns a substring of the specified text model, at the given start and last position.
static java.awt.Point getTextLocationAt(LwTextRender render, PosController pos)
          Calculates and gets a pixel location for the specified text render and the text cursor position.
static java.awt.Point getTextRowColAt(LwTextRender render, int x, int y)
          Calculates and gets a text position for the specified text render and the location.
protected  boolean handleKey(LwKeyEvent e)
          Handles the specified key event.
 boolean hasSelection()
          Tests if the a text part has been selected.
 boolean isEditable()
          Checks if the text field component is editable.
protected  boolean isFiltered(LwKeyEvent e)
          Returns true if the key event should be handle next, returns false if the handling process has to be terminated.
 void keyPressed(LwKeyEvent e)
          Invoked when a key has been pressed.
 void keyReleased(LwKeyEvent e)
          Invoked when a key has been released.
 void keyTyped(LwKeyEvent e)
          Invoked when a key has been typed.
protected  LwTextRender makeTextRender(TextModel t)
          The method gets default text render that will be used to render the specified text model.
 void mouseClicked(LwMouseEvent e)
          Invoked when the mouse button has been clicked on a light weight component.
 void mouseDragged(LwMouseMotionEvent e)
          Invoked when a mouse button is pressed on a component and then dragged.
 void mouseEntered(LwMouseEvent e)
          Invoked when the mouse enters a light weight component.
 void mouseExited(LwMouseEvent e)
          Invoked when the mouse exits a light weight component.
 void mouseMoved(LwMouseMotionEvent e)
          Invoked when the mouse pointer has been moved on a light weight component (with no buttons no down).
 void mousePressed(LwMouseEvent e)
          Invoked when the mouse button has been pressed on a light weight component.
 void mouseReleased(LwMouseEvent e)
          Invoked when the mouse button has been released on a light weight component.
 boolean moveContent()
          Tests if the scroll component performs scrolling by changing it location or view.
protected  int pageSize()
          Gets the page size.
 void paint(java.awt.Graphics g)
          Paints this component.
 void posChanged(PosEvent e)
          Invoked when a virtual position has been changed.
protected  void recalc()
          Invoked with validate method only if the component is not valid.
protected  void remove(int pos, int size)
          Removes a part of the text starting from the given position and with the specified size.
 void select(int startOffset, int endOffset)
          Selects the specified by the start and end offsets the text part.
 void setCursorView(LwView v)
          Sets the specified cursor view to render the text cursor.
 void setEditable(boolean b)
          Sets the specified mode for the text field component.
 void setEnabled(boolean b)
          Enables or disables this component.
 void setPosController(PosController p)
          Sets the specified pos controller to manage the text cursor position.
 void setPSByRowsCols(int r, int c)
          Sets the preferred size for the text field component that conforms to the specified number of rows and columns.
 void setScrollMan(ScrollMan m)
          Sets the specified scroll manager for the scroll object.
 void setSelectColor(java.awt.Color c)
          Sets the specified color to render selected text.
 void setSOLocation(int x, int y)
          Sets the specified scroll object location.
 void setText(java.lang.String s)
          Sets the specified string to be shown with the class.
 void startDragged(LwMouseMotionEvent e)
          Invoked when the mouse starts dragged.
protected  void stopSelection()
          Clear the current selection.
protected  void write(int pos, java.lang.String s)
          Inserts the specified text at the given position.
 
Methods inherited from class org.zaval.lw.LwLabel
getForeground, getText, getTextModel, getTextRender, setForeground
 
Methods inherited from class org.zaval.lw.LwCanvas
calcPreferredSize, getBackground, getBounds, getHeight, getInsets, getLocation, getLwComponentAt, getLwParent, getSize, getViewMan, getVisiblePart, getWidth, getX, getY, hasFocus, invalidate, isEnabled, isOpaque, isVisible, repaint, repaint, requestFocus, setBackground, setInsets, setLocation, setLwParent, setOpaque, setPSSize, setSize, setViewMan, setVisible, toString, update, validate, viewManChanged, vrp
 
Methods inherited from class org.zaval.util.ValidationObject
isValid
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.zaval.util.Validationable
isValid
 

Constructor Detail

LwTextField

public LwTextField()
Constructs a text filed component with no text.

LwTextField

public LwTextField(java.lang.String s)
Constructs a text field component with the specified text. The methods sets LwAdvTextRender as the face view and sets org.zaval.data.Text as the render target.
Parameters:
s - the specified text.

LwTextField

public LwTextField(java.lang.String s,
                   int maxCol)
Constructs a single line text field component with the specified text and the maximal number of columns.
Parameters:
s - the specified text.
maxCol - the specified maximal number of columns.

LwTextField

public LwTextField(TextModel model)
Constructs a text field component with the specified text model.
Parameters:
model - the specified text model.
Method Detail

canHaveFocus

public boolean canHaveFocus()
Description copied from interface: LwComponent
Specifies if the component can have focus.
Overrides:
canHaveFocus in class LwCanvas
Following copied from interface: org.zaval.lw.LwComponent
Returns:
true if the component can have the focus.

getPosController

public PosController getPosController()
Gets the pos controller that manages the text cursor position.
Returns:
a pos controller.

setPosController

public void setPosController(PosController p)
Sets the specified pos controller to manage the text cursor position.
Parameters:
p - the specified pos controller.

getCursorView

public LwView getCursorView()
Gets the cursor view that is used to paint the text cursor.
Returns:
a cursor view.

setCursorView

public void setCursorView(LwView v)
Sets the specified cursor view to render the text cursor.
Parameters:
v - the specified cursor view.

setPSByRowsCols

public void setPSByRowsCols(int r,
                            int c)
Sets the preferred size for the text field component that conforms to the specified number of rows and columns.
Parameters:
r - the specified number of rows.
c - the specified number of columns.

paint

public void paint(java.awt.Graphics g)
Paints this component. The method initiates painting of the cursor caret by calling drawCursor method.
Overrides:
paint in class LwCanvas
Parameters:
g - the graphics context to be used for painting.

setEditable

public void setEditable(boolean b)
Sets the specified mode for the text field component. The component is editable if it is possible to edit a text.
Parameters:
b - the specified mode. If the mode is true than the component is editable; otherwise not-editable.

isEditable

public boolean isEditable()
Checks if the text field component is editable.
Returns:
true if the text field is editable; false otherwise.

keyPressed

public void keyPressed(LwKeyEvent e)
Description copied from interface: LwKeyListener
Invoked when a key has been pressed.
Specified by:
keyPressed in interface LwKeyListener
Following copied from interface: org.zaval.lw.event.LwKeyListener
Parameters:
e - the specified key event.

keyTyped

public void keyTyped(LwKeyEvent e)
Description copied from interface: LwKeyListener
Invoked when a key has been typed.
Specified by:
keyTyped in interface LwKeyListener
Following copied from interface: org.zaval.lw.event.LwKeyListener
Parameters:
e - the specified key event.

focusGained

public void focusGained(LwFocusEvent e)
Description copied from interface: LwFocusListener
Invoked when the light weight component gained focus.
Specified by:
focusGained in interface LwFocusListener
Following copied from interface: org.zaval.lw.event.LwFocusListener
Parameters:
e - the specified focus event.

focusLost

public void focusLost(LwFocusEvent e)
Description copied from interface: LwFocusListener
Invoked when the light weight component lost focus.
Specified by:
focusLost in interface LwFocusListener
Following copied from interface: org.zaval.lw.event.LwFocusListener
Parameters:
e - the specified focus event.

keyReleased

public void keyReleased(LwKeyEvent e)
Description copied from interface: LwKeyListener
Invoked when a key has been released.
Specified by:
keyReleased in interface LwKeyListener
Following copied from interface: org.zaval.lw.event.LwKeyListener
Parameters:
e - the specified key event.

mouseClicked

public void mouseClicked(LwMouseEvent e)
Description copied from interface: LwMouseListener
Invoked when the mouse button has been clicked on a light weight component.
Specified by:
mouseClicked in interface LwMouseListener
Following copied from interface: org.zaval.lw.event.LwMouseListener
Parameters:
e - the specified mouse event.

mouseEntered

public void mouseEntered(LwMouseEvent e)
Description copied from interface: LwMouseListener
Invoked when the mouse enters a light weight component.
Specified by:
mouseEntered in interface LwMouseListener
Following copied from interface: org.zaval.lw.event.LwMouseListener
Parameters:
e - the specified mouse event.

mouseExited

public void mouseExited(LwMouseEvent e)
Description copied from interface: LwMouseListener
Invoked when the mouse exits a light weight component.
Specified by:
mouseExited in interface LwMouseListener
Following copied from interface: org.zaval.lw.event.LwMouseListener
Parameters:
e - the specified mouse event.

startDragged

public void startDragged(LwMouseMotionEvent e)
Description copied from interface: LwMouseMotionListener
Invoked when the mouse starts dragged.
Specified by:
startDragged in interface LwMouseMotionListener
Following copied from interface: org.zaval.lw.event.LwMouseMotionListener
Parameters:
e - the specified mouse motion event.

endDragged

public void endDragged(LwMouseMotionEvent e)
Description copied from interface: LwMouseMotionListener
Invoked when the mouse ends dragged.
Specified by:
endDragged in interface LwMouseMotionListener
Following copied from interface: org.zaval.lw.event.LwMouseMotionListener
Parameters:
e - the specified mouse motion event.

mouseReleased

public void mouseReleased(LwMouseEvent e)
Description copied from interface: LwMouseListener
Invoked when the mouse button has been released on a light weight component.
Specified by:
mouseReleased in interface LwMouseListener
Following copied from interface: org.zaval.lw.event.LwMouseListener
Parameters:
e - the specified mouse event.

mouseMoved

public void mouseMoved(LwMouseMotionEvent e)
Description copied from interface: LwMouseMotionListener
Invoked when the mouse pointer has been moved on a light weight component (with no buttons no down).
Specified by:
mouseMoved in interface LwMouseMotionListener
Following copied from interface: org.zaval.lw.event.LwMouseMotionListener
Parameters:
e - the specified mouse motion event.

mouseDragged

public void mouseDragged(LwMouseMotionEvent e)
Description copied from interface: LwMouseMotionListener
Invoked when a mouse button is pressed on a component and then dragged. Mouse drag events will continue to be delivered to the component where the first originated until the mouse button is released (regardless of whether the mouse position is within the bounds of the component).
Specified by:
mouseDragged in interface LwMouseMotionListener
Following copied from interface: org.zaval.lw.event.LwMouseMotionListener
Parameters:
e - the specified mouse motion event.

mousePressed

public void mousePressed(LwMouseEvent e)
Description copied from interface: LwMouseListener
Invoked when the mouse button has been pressed on a light weight component.
Specified by:
mousePressed in interface LwMouseListener
Following copied from interface: org.zaval.lw.event.LwMouseListener
Parameters:
e - the specified mouse event.

getCursorType

public int getCursorType(LwComponent target,
                         int x,
                         int y)
Gets the cursor type for the specified location of the given component. The method returns Cursor.TEXT_CURSOR cursor type.
Specified by:
getCursorType in interface Cursorable
Parameters:
target - the given component.
x - the x coordinate.
y - the y coordinate.
Returns:
a cursor type.

getLineSize

public int getLineSize(int i)
Description copied from interface: PosInfo
Gets the line size for the specified line number.
Specified by:
getLineSize in interface PosInfo
Following copied from interface: org.zaval.misc.PosInfo
Parameters:
line - the specified line number.
Returns:
a size of the line.

getLines

public int getLines()
Description copied from interface: PosInfo
Gets the number of lines.
Specified by:
getLines in interface PosInfo
Following copied from interface: org.zaval.misc.PosInfo
Returns:
a number of lines.

getMaxOffset

public int getMaxOffset()
Description copied from interface: PosInfo
Gets the maximal offset. The method can return -1 as the result, in this case the pos controller that uses the pos info will try to calculate the maximal offset itself basing on the line number and the lines sizes.
Specified by:
getMaxOffset in interface PosInfo
Following copied from interface: org.zaval.misc.PosInfo
Returns:
a maximal offset.

setText

public void setText(java.lang.String s)
Description copied from class: LwLabel
Sets the specified string to be shown with the class.
Overrides:
setText in class LwLabel
Following copied from class: org.zaval.lw.LwLabel
Parameters:
s - the specified string.

posChanged

public void posChanged(PosEvent e)
Description copied from interface: PosListener
Invoked when a virtual position has been changed.
Specified by:
posChanged in interface PosListener
Following copied from interface: org.zaval.misc.event.PosListener
Parameters:
e - the specified pos event.

getOrigin

public java.awt.Point getOrigin()
Returns an origin of the component. The method is overrided with the component to offset a content of the text field depending on the cursor position.
Overrides:
getOrigin in class LwCanvas
Returns:
an origin of the component.

getPreferredSize

public java.awt.Dimension getPreferredSize()
Description copied from class: LwCanvas
Gets the preferred size of this component. The method computes the preferred size as a sum of the component insets (returned with getInsets method) and a "pure" preferred size (returned with calcPreferredSize method). You should not override the method, use calcPreferredSize method to define the "pure" preferred size of this component.
Overrides:
getPreferredSize in class LwCanvas
Following copied from class: org.zaval.lw.LwCanvas
Returns:
a dimension object indicating this component preferred size.

getSOLocation

public java.awt.Point getSOLocation()
Description copied from interface: ScrollObj
Gets the scroll object location.
Specified by:
getSOLocation in interface ScrollObj
Following copied from interface: org.zaval.misc.ScrollObj
Returns:
a scroll object location.

setSOLocation

public void setSOLocation(int x,
                          int y)
Description copied from interface: ScrollObj
Sets the specified scroll object location. The method defines a mechanism that will be used to scrool the object.
Specified by:
setSOLocation in interface ScrollObj
Following copied from interface: org.zaval.misc.ScrollObj
Parameters:
x - the specified x coordinate.
y - the specified y coordinate.

getSOSize

public java.awt.Dimension getSOSize()
Description copied from interface: ScrollObj
Gets the scroll object size. The size is a size that the scroll object wants to have.
Specified by:
getSOSize in interface ScrollObj
Following copied from interface: org.zaval.misc.ScrollObj
Returns:
a scroll object size.

setScrollMan

public void setScrollMan(ScrollMan m)
Description copied from interface: ScrollObj
Sets the specified scroll manager for the scroll object. The manager reference should be used with the scroll object to notify when the scroll object has been moved or resized.
Specified by:
setScrollMan in interface ScrollObj
Following copied from interface: org.zaval.misc.ScrollObj
Parameters:
m - the specified scroll manager.

moveContent

public boolean moveContent()
Tests if the scroll component performs scrolling by changing it location or view. The method is overrided with the component to point move a content of the text field, the method always returns true.
Specified by:
moveContent in interface ScrollObj
Returns:
true if the scroll component organizes scrolling by moving its view; otherwise false.

getStartSelection

public java.awt.Point getStartSelection()
Description copied from interface: TxtSelectionInfo
Returns the start selection row and column
Specified by:
getStartSelection in interface TxtSelectionInfo
Following copied from interface: org.zaval.lw.TxtSelectionInfo
Returns:
a start selection row and column. The result is represented using java.awt.Point class where x field is selection row and y field is selection column.

getEndSelection

public java.awt.Point getEndSelection()
Description copied from interface: TxtSelectionInfo
Returns the last selection row and column
Specified by:
getEndSelection in interface TxtSelectionInfo
Following copied from interface: org.zaval.lw.TxtSelectionInfo
Returns:
a last selection row and column. The result is represented using java.awt.Point class where x field is selection row and y field is selection column.

hasSelection

public boolean hasSelection()
Tests if the a text part has been selected.
Returns:
true if the text part has been selected; false otherwise.

getSelectionOffsets

public java.awt.Point getSelectionOffsets()
Gets the start and last selection offsets.
Returns:
the start and last selection offsets. The result is represented with java.awt.Point class where x field corresponds to start selection offset and y field corresponds to last selection offset.

getSelectedText

public java.lang.String getSelectedText()
Description copied from interface: TxtSelectionInfo
Returns the selected text.
Specified by:
getSelectedText in interface TxtSelectionInfo
Following copied from interface: org.zaval.lw.TxtSelectionInfo
Returns:
a selected text. The method returns null if there is not selected text.

getSelectColor

public java.awt.Color getSelectColor()
Description copied from interface: TxtSelectionInfo
Returns a color that is used to render selected text background.
Specified by:
getSelectColor in interface TxtSelectionInfo
Following copied from interface: org.zaval.lw.TxtSelectionInfo
Returns:
the color to render selected text background.

setSelectColor

public void setSelectColor(java.awt.Color c)
Sets the specified color to render selected text.
Parameters:
c - the specified color.

setEnabled

public void setEnabled(boolean b)
Description copied from class: LwCanvas
Enables or disables this component. An enabled component can participate in events handling and performing processes. Component is enabled initially by default. The method performs appropriate LwComponentEvent.
Overrides:
setEnabled in class LwCanvas
Following copied from class: org.zaval.lw.LwCanvas
Parameters:
b - if true, this component is enabled; otherwise this component is disabled.

select

public void select(int startOffset,
                   int endOffset)
Selects the specified by the start and end offsets the text part.
Parameters:
startOffset - the specified start offset.
endOffset - the specified end offset.

drawCursor

protected void drawCursor(java.awt.Graphics g)
The method is used to paint the text cursor using the cursor view.
Parameters:
g - the graphics context to be used for painting.

recalc

protected void recalc()
Description copied from class: LwCanvas
Invoked with validate method only if the component is not valid. The method shoud be overrided to calculate metrical characteristics of the component to minimize computation time of the preferred size for the component. For example, you can calculate a preferred size inside the method and just return it by calcPreferredSize method.
Overrides:
recalc in class LwCanvas

handleKey

protected boolean handleKey(LwKeyEvent e)
Handles the specified key event.
Parameters:
e - the specified key event to be handle.
Returns:
true if the key event has been handled with the method; otherwise false.

isFiltered

protected boolean isFiltered(LwKeyEvent e)
Returns true if the key event should be handle next, returns false if the handling process has to be terminated. The method is called before any other event handler will be executed.
Parameters:
e - the specified key event.
Returns:
true if the key event should be handled.

makeTextRender

protected LwTextRender makeTextRender(TextModel t)
Description copied from class: LwLabel
The method gets default text render that will be used to render the specified text model. The render is set as the face view for the component during the component initialization.
Overrides:
makeTextRender in class LwLabel
Following copied from class: org.zaval.lw.LwLabel
Parameters:
t - the specified text model.
Returns:
a text render that is going to be used as the face view of the component.

remove

protected void remove(int pos,
                      int size)
Removes a part of the text starting from the given position and with the specified size.
Parameters:
pos - the given position.
size - the specified removed part size.

write

protected void write(int pos,
                     java.lang.String s)
Inserts the specified text at the given position.
Parameters:
pos - the given position.
s - the specified text to be inserted.

pageSize

protected int pageSize()
Gets the page size.
Returns:
a page size.

stopSelection

protected void stopSelection()
Clear the current selection.

getTextLocationAt

public static java.awt.Point getTextLocationAt(LwTextRender render,
                                               PosController pos)
Calculates and gets a pixel location for the specified text render and the text cursor position.
Parameters:
render - the specified text render.
pos - the pos controller that defines the text position.
Returns:
a pixel location.

getTextRowColAt

public static java.awt.Point getTextRowColAt(LwTextRender render,
                                             int x,
                                             int y)
Calculates and gets a text position for the specified text render and the location. The result is represented with java.awt.Point class where x field defines a row and y field defines a column.
Parameters:
render - the specified text render.
x - the x coordinate of the location.
y - the y coordinate of the location.
Returns:
a text position.

findNextWord

public static java.awt.Point findNextWord(TextModel t,
                                          int line,
                                          int col,
                                          int d)
Finds starting from the specified line and column next word in the given text model and returns the found word location. The result is represented with java.awt.Point object where x field defines a row text position and y field defines a column text position.
Parameters:
t - the specified text model.
line - the specified starting line.
col - the specified starting column.
d - the specified searching direction. If the value is 1 than the method looks for a next word location, if the value is -1 than the method looks for a previous word location.
Returns:
a word location.

getSubString

public static java.lang.String getSubString(TextModel t,
                                            java.awt.Point start,
                                            java.awt.Point end)
Returns a substring of the specified text model, at the given start and last position. The positions are represented with java.awt.Point object where x field defines a row and y field defines a column.
Parameters:
t - the specified text model.
start - the specified start position.
end - the specified last position.
Returns:
a substring.


Copyright © Zaval Creative Engineering Group, 2000-2005.
http://www.zaval.org