Example usage for javax.swing.text Keymap addActionForKeyStroke

List of usage examples for javax.swing.text Keymap addActionForKeyStroke

Introduction

In this page you can find the example usage for javax.swing.text Keymap addActionForKeyStroke.

Prototype

public void addActionForKeyStroke(KeyStroke key, Action a);

Source Link

Document

Adds a binding to the keymap.

Usage

From source file:KeymapExample.java

public static void main(String[] args) {
    JTextArea area = new JTextArea(6, 32);
    Keymap parent = area.getKeymap();
    Keymap newmap = JTextComponent.addKeymap("KeymapExampleMap", parent);

    KeyStroke u = KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK);
    Action actionU = new UpWord();
    newmap.addActionForKeyStroke(u, actionU);

    Action actionList[] = area.getActions();
    Hashtable lookup = new Hashtable();
    for (int j = 0; j < actionList.length; j += 1)
        lookup.put(actionList[j].getValue(Action.NAME), actionList[j]);

    KeyStroke L = KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.CTRL_MASK);
    Action actionL = (Action) lookup.get(DefaultEditorKit.selectLineAction);
    newmap.addActionForKeyStroke(L, actionL);

    KeyStroke W = KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.CTRL_MASK);
    Action actionW = (Action) lookup.get(DefaultEditorKit.selectWordAction);
    newmap.addActionForKeyStroke(W, actionW);

    area.setKeymap(newmap);/*from  w w w  .  j a v a 2  s .co m*/

    JFrame f = new JFrame("KeymapExample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER);
    area.setText("www.\n java2s \n .com.");
    f.pack();
    f.setVisible(true);
}

From source file:KeymapExample.java

public static void main(String[] args) {
    JTextArea area = new JTextArea(6, 32);
    Keymap parent = area.getKeymap();
    Keymap newmap = JTextComponent.addKeymap("KeymapExampleMap", parent);

    KeyStroke u = KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK);
    Action actionU = new UpWord();
    newmap.addActionForKeyStroke(u, actionU);

    Action actionList[] = area.getActions();
    Hashtable lookup = new Hashtable();
    for (int j = 0; j < actionList.length; j += 1)
        lookup.put(actionList[j].getValue(Action.NAME), actionList[j]);

    KeyStroke L = KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.CTRL_MASK);
    Action actionL = (Action) lookup.get(DefaultEditorKit.selectLineAction);
    newmap.addActionForKeyStroke(L, actionL);

    KeyStroke W = KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.CTRL_MASK);
    Action actionW = (Action) lookup.get(DefaultEditorKit.selectWordAction);
    newmap.addActionForKeyStroke(W, actionW);

    area.setKeymap(newmap);/*  w  w w .ja  v  a 2  s . c o m*/

    JFrame f = new JFrame("KeymapExample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER);
    area.setText("This is a test.");
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JTextArea area = new JTextArea(6, 32);
    Keymap parent = area.getKeymap();
    Keymap newmap = JTextComponent.addKeymap("KeymapExampleMap", parent);

    KeyStroke u = KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK);
    Action actionU = new UppercaseAction();
    newmap.addActionForKeyStroke(u, actionU);

    Action actionList[] = area.getActions();
    Hashtable lookup = new Hashtable();
    for (int j = 0; j < actionList.length; j += 1)
        lookup.put(actionList[j].getValue(Action.NAME), actionList[j]);

    KeyStroke L = KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.CTRL_MASK);
    Action actionL = (Action) lookup.get(DefaultEditorKit.selectLineAction);
    newmap.addActionForKeyStroke(L, actionL);

    area.setKeymap(newmap);/*  w w w . jav a2  s.  c om*/

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new JScrollPane(area), BorderLayout.CENTER);
    area.setText("this is a test");
    f.setSize(300, 300);
    f.setVisible(true);
}

From source file:net.sf.jabref.gui.keyboard.EmacsKeyBindings.java

private static void createBackup() {
    Keymap oldBackup = JTextComponent.getKeymap(EmacsKeyBindings.JTCS[0].getClass().getName());
    if (oldBackup != null) {
        // if there is already a backup, do not create a new backup
        return;//from  w w w.  j  a va  2s.c  om
    }

    for (JTextComponent jtc : EmacsKeyBindings.JTCS) {
        Keymap orig = jtc.getKeymap();
        Keymap backup = JTextComponent.addKeymap(jtc.getClass().getName(), null);
        Action[] bound = orig.getBoundActions();
        for (Action aBound : bound) {
            KeyStroke[] strokes = orig.getKeyStrokesForAction(aBound);
            for (KeyStroke stroke : strokes) {
                backup.addActionForKeyStroke(stroke, aBound);
            }
        }
        backup.setDefaultAction(orig.getDefaultAction());
    }
}

From source file:net.sf.jabref.gui.keyboard.EmacsKeyBindings.java

/**
 * Restores the original keybindings for the concrete subclasses of
 * {@link JTextComponent}./*from  w  ww  . ja va2s. com*/
 *
 */
public static void unload() {
    for (int i = 0; i < EmacsKeyBindings.JTCS.length; i++) {
        Keymap backup = JTextComponent.getKeymap(EmacsKeyBindings.JTCS[i].getClass().getName());

        if (backup != null) {
            Keymap current = EmacsKeyBindings.JTCS[i].getKeymap();
            current.removeBindings();

            Action[] bound = backup.getBoundActions();
            for (Action aBound : bound) {
                KeyStroke[] strokes = backup.getKeyStrokesForAction(bound[i]);
                for (KeyStroke stroke : strokes) {
                    current.addActionForKeyStroke(stroke, aBound);
                }
            }
            current.setDefaultAction(backup.getDefaultAction());
        }
    }
}

From source file:DateTimeEditor.java

protected void setupKeymap() {
    Keymap keymap = m_textField.addKeymap("DateTimeKeymap", null);
    keymap.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), m_upAction);
    keymap.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), m_downAction);
    keymap.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0),
            new BackwardAction(DefaultEditorKit.backwardAction));
    keymap.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0),
            new ForwardAction(DefaultEditorKit.forwardAction));
    keymap.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_HOME, 0),
            new BeginAction(DefaultEditorKit.beginAction));
    keymap.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_END, 0),
            new EndAction(DefaultEditorKit.endAction));
    m_textField.setKeymap(keymap);//from  ww w.ja v  a 2s . co  m
}