All Packages Class Hierarchy This Package Previous Next Index
Class java.awt.swing.text.DefaultTextController
java.lang.Object
|
+----java.awt.swing.text.DefaultTextController
- public class DefaultTextController
- extends Object
- implements TextController
A class for binding events to manipulations on
JTextComponent implementations. To facilitate
flexible use of the keyboard, support for
creating keymaps and binding various keystrokes
to some kind of action is provided. In order
to allow keymaps to be shared across multiple
text components, they can use actions that
extend JTextAction. JTextAction can determine
which JTextComponent (using a JTextController)
has focus and therefore is the subject of the
action. JTextController creates a minimal
keymap (named DEFAULT_KEYMAP) that is
shared by all JTextController instances as the
default keymap. Typically a look-and-feel
implementation will install a different keymap
that resolves to the default keymap for those
bindings not found in the different keymap.
The minimal bindings include:
- inserting content into the editor for the
printable keys.
- removing content with the backspace and del
keys.
- caret movement forward and backward
- jumping to the beginning and end of the
text stream using the home and end keys.
Focus and mouse event behavior are directly
implemented. To change these types of event
this class must be subclassed.
- See Also:
- JTextAction, TextController
DefaultTextController.KeyBinding- Binding record for creating key bindings
DEFAULT_KEYMAP- This is the name of the default keymap that will be shared by all
DefaultTextController instances unless they have had a different
keymap set.
DefaultTextController(JTextComponent)
- Creates a new controller based on an editor.
addKeymap(String, Keymap)
- Adds a new keymap into the keymap hierarchy.
caretOff()
- Turns the caret off.
caretOn()
- Turns the caret on.
focusGained(FocusEvent)
- Stashes the current focused JTextComponent reference
for JTextAction instances to use, and sets the caret
to be visible.
focusLost(FocusEvent)
- Removes reference to focused text component that
instances of JTextAction use and turns off the
caret visibility.
getEditor()
- Gets the editor associated with the controller.
getKeymap(String)
- Fetches a named keymap previously added to the document.
keyPressed(KeyEvent)
- Tries to map the event to an action using the
current keymap.
keyReleased(KeyEvent)
- Called when a key is released.
keyTyped(KeyEvent)
- Tries to map the event to an action using the
current keymap.
loadKeymap(Keymap, DefaultTextController.KeyBinding[], Action[])
Loads a keymap with a bunch of
bindings.
mouseClicked(MouseEvent)
- Called when the mouse is clicked.
mouseDragged(MouseEvent)
- Moves the caret position
according to the mouse pointers current
location.
mouseEntered(MouseEvent)
- Called when the mouse enters a region.
mouseExited(MouseEvent)
- Called when the mouse exits a region.
mouseMoved(MouseEvent)
- Called when the mouse is moved.
mousePressed(MouseEvent)
- Requests focus on the associated
text component, and try to set the cursor position.
mouseReleased(MouseEvent)
- Called when the mouse is released.
moveCursor(MouseEvent)
- Tries to move the position of the caret from
the coordinates of the mouse event.
positionCursor(MouseEvent)
- Tries to set the position of the caret from
the coordinates of the mouse event.
removeKeymap(String)
- Removes a named keymap previously added to the document.
DEFAULT_KEYMAP
public static final String DEFAULT_KEYMAP
- This is the name of the default keymap that will be shared by all
DefaultTextController instances unless they have had a different
keymap set.
DefaultTextController
public DefaultTextController(JTextComponent editor)
- Creates a new controller based on an editor.
- Parameters:
- editor - the text editor
addKeymap
public static Keymap addKeymap(String nm,
Keymap parent)
- Adds a new keymap into the keymap hierarchy. Keymap bindings
resolve from bottom up so an attribute specified in a child
will override an attribute specified in the parent.
- Parameters:
- nm - the name of the keymap (must be unique within the
collection of named keymaps in the document). The name may
be null if the keymap is unnamed, but the caller is responsible
for managing the reference returned as an unnamed keymap can't
be fetched by name.
- parent - the parent keymap. This may be null if unspecified
bindings need not be resolved in some other keymap.
- Returns:
- the keymap
removeKeymap
public static Keymap removeKeymap(String nm)
- Removes a named keymap previously added to the document.
- Parameters:
- nm - the name of the keymap to remove
- Returns:
- the keymap
getKeymap
public static Keymap getKeymap(String nm)
- Fetches a named keymap previously added to the document.
- Parameters:
- nm - the name of the keymap
- Returns:
- the keymap
loadKeymap
public static void loadKeymap(Keymap map,
DefaultTextController.KeyBinding[] bindings,
Action[] actions)
Loads a keymap with a bunch of
bindings. This can be used to take a static table of
definitions and load them into some keymap. The following
example illustrates an example of binding some keys to
the cut, copy, and paste actions associated with a
JTextComponent. It also binds tab to insert the tab
into the text component. A code fragment to accomplish
this might look as follows:
JTextComponent c = new JTextPane();
Keymap k = c.getKeymap();
DefaultTextController.loadKeymap(k, c.getActions(), defaultBindings);
static final DefaultTextController.KeyBinding[] defaultBindings = {
new DefaultTextController.KeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK),
JTextComponent.copyAction),
new DefaultTextController.KeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK),
JTextComponent.pasteAction),
new DefaultTextController.KeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK),
JTextComponent.cutAction),
new DefaultTextController.KeyBinding(
KeyStroke.getKeyStroke('\t'),
JTextComponent.insertContentAction)
};
- Parameters:
- map - the keymap
- bindings - the bindings
- actions - the set of actions
positionCursor
protected void positionCursor(MouseEvent e)
- Tries to set the position of the caret from
the coordinates of the mouse event.
- Parameters:
- e - the mouse event
moveCursor
protected void moveCursor(MouseEvent e)
- Tries to move the position of the caret from
the coordinates of the mouse event. This
will cause a selection if the dot and mark
are different.
- Parameters:
- e - the mouse event
caretOn
protected void caretOn()
- Turns the caret on.
caretOff
protected void caretOff()
- Turns the caret off.
getEditor
protected final JTextComponent getEditor()
- Gets the editor associated with the controller.
- Returns:
- the editor
mouseClicked
public void mouseClicked(MouseEvent e)
- Called when the mouse is clicked.
- Parameters:
- e - the mouse event
- See Also:
- mouseClicked
mousePressed
public void mousePressed(MouseEvent e)
- Requests focus on the associated
text component, and try to set the cursor position.
- Parameters:
- e - the mouse event
- See Also:
- mousePressed
mouseReleased
public void mouseReleased(MouseEvent e)
- Called when the mouse is released.
- Parameters:
- e - the mouse event
- See Also:
- mouseReleased
mouseEntered
public void mouseEntered(MouseEvent e)
- Called when the mouse enters a region.
- Parameters:
- e - the mouse event
- See Also:
- mouseEntered
mouseExited
public void mouseExited(MouseEvent e)
- Called when the mouse exits a region.
- Parameters:
- e - the mouse event
- See Also:
- mouseExited
mouseDragged
public void mouseDragged(MouseEvent e)
- Moves the caret position
according to the mouse pointers current
location. This effectively extends the
selection.
- Parameters:
- e - the mouse event
- See Also:
- mouseDragged
mouseMoved
public void mouseMoved(MouseEvent e)
- Called when the mouse is moved.
- Parameters:
- e - the mouse event
- See Also:
- mouseMoved
focusGained
public void focusGained(FocusEvent e)
- Stashes the current focused JTextComponent reference
for JTextAction instances to use, and sets the caret
to be visible.
- Parameters:
- e - the focus event
- See Also:
- JTextAction, focusGained
focusLost
public void focusLost(FocusEvent e)
- Removes reference to focused text component that
instances of JTextAction use and turns off the
caret visibility.
- Parameters:
- e - the focus event
- See Also:
- JTextAction, focusLost
keyTyped
public void keyTyped(KeyEvent e)
- Tries to map the event to an action using the
current keymap. If an action is found it will
be executed and the event consumed. If nothing
is found and a default action has been mapped
to
KeyStroke.getKeyStroke(KeyEvent.CHAR_UNDEFINED)
that action will be fired and the event consumed.
Failing all of these, the event will be ignored.
- Parameters:
- e - the key event
- See Also:
- keyTyped, getKeyStroke
keyPressed
public void keyPressed(KeyEvent e)
- Tries to map the event to an action using the
current keymap. If an action is found it will
be executed and the event consumed.
- Parameters:
- e - the key event
- See Also:
- keyPressed, getKeyStroke
keyReleased
public void keyReleased(KeyEvent e)
- Called when a key is released.
- Parameters:
- e - the key event
- See Also:
- keyReleased
All Packages Class Hierarchy This Package Previous Next Index
Submit a bug or feature