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:

Focus and mouse event behavior are directly implemented. To change these types of event this class must be subclassed.

See Also:
JTextAction, TextController

Class Index

 o DefaultTextController.KeyBinding
Binding record for creating key bindings

Variable Index

 o 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.

Constructor Index

 o DefaultTextController(JTextComponent)
Creates a new controller based on an editor.

Method Index

 o addKeymap(String, Keymap)
Adds a new keymap into the keymap hierarchy.
 o caretOff()
Turns the caret off.
 o caretOn()
Turns the caret on.
 o focusGained(FocusEvent)
Stashes the current focused JTextComponent reference for JTextAction instances to use, and sets the caret to be visible.
 o focusLost(FocusEvent)
Removes reference to focused text component that instances of JTextAction use and turns off the caret visibility.
 o getEditor()
Gets the editor associated with the controller.
 o getKeymap(String)
Fetches a named keymap previously added to the document.
 o keyPressed(KeyEvent)
Tries to map the event to an action using the current keymap.
 o keyReleased(KeyEvent)
Called when a key is released.
 o keyTyped(KeyEvent)
Tries to map the event to an action using the current keymap.
 o loadKeymap(Keymap, DefaultTextController.KeyBinding[], Action[])

Loads a keymap with a bunch of bindings.

 o mouseClicked(MouseEvent)
Called when the mouse is clicked.
 o mouseDragged(MouseEvent)
Moves the caret position according to the mouse pointers current location.
 o mouseEntered(MouseEvent)
Called when the mouse enters a region.
 o mouseExited(MouseEvent)
Called when the mouse exits a region.
 o mouseMoved(MouseEvent)
Called when the mouse is moved.
 o mousePressed(MouseEvent)
Requests focus on the associated text component, and try to set the cursor position.
 o mouseReleased(MouseEvent)
Called when the mouse is released.
 o moveCursor(MouseEvent)
Tries to move the position of the caret from the coordinates of the mouse event.
 o positionCursor(MouseEvent)
Tries to set the position of the caret from the coordinates of the mouse event.
 o removeKeymap(String)
Removes a named keymap previously added to the document.

Variables

 o 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.

Constructors

 o DefaultTextController
public DefaultTextController(JTextComponent editor)
Creates a new controller based on an editor.

Parameters:
editor - the text editor

Methods

 o 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
 o 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
 o 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
 o 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
 o 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
 o 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
 o caretOn
protected void caretOn()
Turns the caret on.

 o caretOff
protected void caretOff()
Turns the caret off.

 o getEditor
protected final JTextComponent getEditor()
Gets the editor associated with the controller.

Returns:
the editor
 o mouseClicked
public void mouseClicked(MouseEvent e)
Called when the mouse is clicked.

Parameters:
e - the mouse event
See Also:
mouseClicked
 o 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
 o mouseReleased
public void mouseReleased(MouseEvent e)
Called when the mouse is released.

Parameters:
e - the mouse event
See Also:
mouseReleased
 o mouseEntered
public void mouseEntered(MouseEvent e)
Called when the mouse enters a region.

Parameters:
e - the mouse event
See Also:
mouseEntered
 o mouseExited
public void mouseExited(MouseEvent e)
Called when the mouse exits a region.

Parameters:
e - the mouse event
See Also:
mouseExited
 o 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
 o mouseMoved
public void mouseMoved(MouseEvent e)
Called when the mouse is moved.

Parameters:
e - the mouse event
See Also:
mouseMoved
 o 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
 o 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
 o 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
 o 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
 o 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