Example usage for javax.swing.text JTextComponent getInputMap

List of usage examples for javax.swing.text JTextComponent getInputMap

Introduction

In this page you can find the example usage for javax.swing.text JTextComponent getInputMap.

Prototype

public final InputMap getInputMap() 

Source Link

Document

Returns the InputMap that is used when the component has focus.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextComponent textcomp = new JTextArea();
    final UndoManager undo = new UndoManager();
    Document doc = textcomp.getDocument();

    doc.addUndoableEditListener(new UndoableEditListener() {
        public void undoableEditHappened(UndoableEditEvent evt) {
            undo.addEdit(evt.getEdit());
        }/*from ww w. ja  va2 s.  co m*/
    });
    textcomp.getActionMap().put("Undo", new AbstractAction("Undo") {
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canUndo()) {
                    undo.undo();
                }
            } catch (CannotUndoException e) {
            }
        }
    });
    textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");
    textcomp.getActionMap().put("Redo", new AbstractAction("Redo") {
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canRedo()) {
                    undo.redo();
                }
            } catch (CannotRedoException e) {
            }
        }
    });
    textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo");
}

From source file:Main.java

public static void main(String[] argv) {

    JTextComponent textcomp = new JTextArea();
    final UndoManager undo = new UndoManager();
    Document doc = textcomp.getDocument();

    doc.addUndoableEditListener(new UndoableEditListener() {
        public void undoableEditHappened(UndoableEditEvent evt) {
            undo.addEdit(evt.getEdit());
        }/* w  w w  .j a  v  a2 s. com*/
    });

    textcomp.getActionMap().put("Undo", new AbstractAction("Undo") {
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canUndo()) {
                    undo.undo();
                }
            } catch (CannotUndoException e) {
            }
        }
    });

    textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(textcomp));
    frame.setSize(380, 320);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) {
    JTextComponent textcomp = new JTextArea();
    final UndoManager undo = new UndoManager();
    Document doc = textcomp.getDocument();

    doc.addUndoableEditListener(new UndoableEditListener() {
        public void undoableEditHappened(UndoableEditEvent evt) {
            undo.addEdit(evt.getEdit());
        }//w  ww.  j a v  a2s . c  om
    });

    textcomp.getActionMap().put("Undo", new AbstractAction("Undo") {
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canUndo()) {
                    undo.undo();
                }
            } catch (CannotUndoException e) {
            }
        }
    });

    textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");

    textcomp.getActionMap().put("Redo", new AbstractAction("Redo") {
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canRedo()) {
                    undo.redo();
                }
            } catch (CannotRedoException e) {
            }
        }
    });

    textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo");

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(textcomp));
    frame.setSize(380, 320);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextComponent textcomp = new JTextArea();
    final UndoManager undo = new UndoManager();
    Document doc = textcomp.getDocument();

    JFrame f = new JFrame();
    f.add(new JScrollPane(textcomp));
    f.setSize(330, 300);/*from   w w w.ja v  a2s.c  om*/
    f.setVisible(true);

    doc.addUndoableEditListener(new UndoableEditListener() {
        public void undoableEditHappened(UndoableEditEvent evt) {
            undo.addEdit(evt.getEdit());
        }
    });

    textcomp.getActionMap().put("Undo", new AbstractAction("Undo") {
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canUndo()) {
                    undo.undo();
                }
            } catch (CannotUndoException e) {
            }
        }
    });

    textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");

    textcomp.getActionMap().put("Redo", new AbstractAction("Redo") {
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canRedo()) {
                    undo.redo();
                }
            } catch (CannotRedoException e) {
            }
        }
    });

    textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo");

}

From source file:Main.java

@SuppressWarnings("serial")
public static void installUndoManager(JTextComponent textComponent, final UndoManager undoManager) {

    Document doc = textComponent.getDocument();
    doc.addUndoableEditListener(new UndoableEditListener() {
        public void undoableEditHappened(UndoableEditEvent e) {
            undoManager.addEdit(e.getEdit());
        }/*  w  ww.  ja  v a2s .com*/
    });

    ActionMap am = textComponent.getActionMap();
    InputMap im = textComponent.getInputMap();
    am.put("undo", new AbstractAction("undo") {
        @Override
        public void actionPerformed(ActionEvent e) {
            undoManager.undo();
        }

        @Override
        public boolean isEnabled() {
            return undoManager.canUndo();
        }
    });
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, getMenuShortcutKeyMask()), "undo");

    am.put("redo", new AbstractAction("redo") {
        @Override
        public void actionPerformed(ActionEvent e) {
            undoManager.redo();
        }

        @Override
        public boolean isEnabled() {
            return undoManager.canRedo();
        }
    });
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_Y, getMenuShortcutKeyMask()), "redo");
}

From source file:Main.java

public static void bindUndoManager(final JTextComponent text, final UndoManager undo) {

    text.getDocument().addUndoableEditListener(new UndoableEditListener() {
        @Override//  w w w .  j a  va2s  .  c o  m
        public void undoableEditHappened(UndoableEditEvent e) {
            undo.addEdit(e.getEdit());
        }
    });

    text.getActionMap().put("Undo", new AbstractAction("Undo") {
        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canUndo()) {
                    undo.undo();
                }
            } catch (CannotUndoException e) {
            }
        }
    });
    // Bind the undo action to ctl-Z
    text.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");

    // Create a redo action and add it to the text component
    text.getActionMap().put("Redo", new AbstractAction("Redo") {
        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canRedo()) {
                    undo.redo();
                }
            } catch (CannotRedoException e) {
            }
        }
    });
    // Bind the redo action to ctl-Y 
    text.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo");
}

From source file:net.pandoragames.far.ui.swing.component.UndoHistory.java

/**
 * Registers the specified text component for the undo history.
 * This enables the <code>ctrl + z</code> and <code>ctrl + y</code> shortcuts.
 * @param component to be registered for undos.
 */// w  w w  .  j  a  v a2 s .c  om
public void registerUndoHistory(JTextComponent component) {
    // Create an undo action and add it to the text component
    undoAction = new AbstractAction("Undo") {
        public void actionPerformed(ActionEvent evt) {
            if (UndoHistory.this.canUndo()) {
                UndoHistory.this.undo();
            }
        }
    };
    undoAction.setEnabled(false);
    component.getActionMap().put(ACTION_KEY_UNDO, undoAction);

    // Create a redo action and add it to the text component
    redoAction = new AbstractAction("Redo") {
        public void actionPerformed(ActionEvent evt) {
            if (UndoHistory.this.canRedo()) {
                UndoHistory.this.redo();
            }
        }
    };
    redoAction.setEnabled(false);
    component.getActionMap().put(ACTION_KEY_REDO, redoAction);

    // Bind the actions to ctl-Z and ctl-Y
    component.getInputMap().put(KeyStroke.getKeyStroke("control Z"), ACTION_KEY_UNDO);
    undoAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control Z"));
    component.getInputMap().put(KeyStroke.getKeyStroke("control Y"), ACTION_KEY_REDO);
    redoAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control Y"));

    // registers this UndoHistory as an UndoableEditListener
    component.getDocument().addUndoableEditListener(this);
}

From source file:net.pandoragames.far.ui.swing.component.UndoHistory.java

/**
 * Registers the specified text component for the snapshot history.
 * This enables the <code>alt + &larr;</code> and <code>ctrl + &rarr;</code> shortcuts.
 * @param component to be registered for snapshots.
 *///  ww  w  .j av  a  2s .c o  m
public void registerSnapshotHistory(JTextComponent component) {
    snapshots = new SnapshotHistory(component);

    // Create a previous action and add it to the text component
    previousAction = new AbstractAction("Previous") {
        public void actionPerformed(ActionEvent evt) {
            UndoHistory.this.previous();
        }
    };
    previousAction.setEnabled(false);
    component.getActionMap().put(ACTION_KEY_PREVIOUS, previousAction);

    // Create a next action and add it to the text component
    nextAction = new AbstractAction("Next") {
        public void actionPerformed(ActionEvent evt) {
            UndoHistory.this.next();
        }
    };
    nextAction.setEnabled(false);
    component.getActionMap().put(ACTION_KEY_NEXT, nextAction);

    // Bind the actions to alt-left-arrow and alt-right arrow
    previousAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("alt LEFT"));
    component.getInputMap().put(KeyStroke.getKeyStroke("alt LEFT"), ACTION_KEY_PREVIOUS);
    component.getInputMap().put(KeyStroke.getKeyStroke("alt KP_LEFT"), ACTION_KEY_PREVIOUS);
    nextAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("alt RIGHT"));
    component.getInputMap().put(KeyStroke.getKeyStroke("alt RIGHT"), ACTION_KEY_NEXT);
    component.getInputMap().put(KeyStroke.getKeyStroke("alt KP_RIGHT"), ACTION_KEY_NEXT);
}

From source file:net.sourceforge.pmd.util.designer.Designer.java

private static void makeTextComponentUndoable(JTextComponent textConponent) {
    final UndoManager undoManager = new UndoManager();
    textConponent.getDocument().addUndoableEditListener(new UndoableEditListener() {
        @Override/* w  w w .  j  ava 2  s  .c o m*/
        public void undoableEditHappened(UndoableEditEvent evt) {
            undoManager.addEdit(evt.getEdit());
        }
    });
    ActionMap actionMap = textConponent.getActionMap();
    InputMap inputMap = textConponent.getInputMap();
    actionMap.put("Undo", new AbstractAction("Undo") {
        @Override
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undoManager.canUndo()) {
                    undoManager.undo();
                }
            } catch (CannotUndoException e) {
                throw new RuntimeException(e);
            }
        }
    });
    inputMap.put(KeyStroke.getKeyStroke("control Z"), "Undo");

    actionMap.put("Redo", new AbstractAction("Redo") {
        @Override
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undoManager.canRedo()) {
                    undoManager.redo();
                }
            } catch (CannotRedoException e) {
                throw new RuntimeException(e);
            }
        }
    });
    inputMap.put(KeyStroke.getKeyStroke("control Y"), "Redo");
}

From source file:org.executequery.gui.editor.autocomplete.QueryEditorAutoCompletePopupProvider.java

private void addFocusActions() {

    JTextComponent textComponent = queryEditorTextComponent();

    ActionMap actionMap = textComponent.getActionMap();
    actionMap.put(LIST_FOCUS_ACTION_KEY, listFocusAction);
    actionMap.put(LIST_SCROLL_ACTION_KEY_DOWN, listScrollActionDown);
    actionMap.put(LIST_SCROLL_ACTION_KEY_UP, listScrollActionUp);
    actionMap.put(LIST_SELECTION_ACTION_KEY, listSelectionAction);
    actionMap.put(LIST_SCROLL_ACTION_KEY_PAGE_DOWN, listScrollActionPageDown);
    actionMap.put(LIST_SCROLL_ACTION_KEY_PAGE_UP, listScrollActionPageUp);

    InputMap inputMap = textComponent.getInputMap();
    saveExistingActions(inputMap);// w w  w .  j a va2  s .com

    inputMap.put(KEY_STROKE_DOWN, LIST_SCROLL_ACTION_KEY_DOWN);
    inputMap.put(KEY_STROKE_UP, LIST_SCROLL_ACTION_KEY_UP);

    inputMap.put(KEY_STROKE_PAGE_DOWN, LIST_SCROLL_ACTION_KEY_PAGE_DOWN);
    inputMap.put(KEY_STROKE_PAGE_UP, LIST_SCROLL_ACTION_KEY_PAGE_UP);

    inputMap.put(KEY_STROKE_TAB, LIST_FOCUS_ACTION_KEY);
    inputMap.put(KEY_STROKE_ENTER, LIST_SELECTION_ACTION_KEY);

    textComponent.addCaretListener(this);
}