Example usage for javax.swing.text JTextComponent getActionMap

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

Introduction

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

Prototype

public final ActionMap getActionMap() 

Source Link

Document

Returns the ActionMap used to determine what Action to fire for particular KeyStroke binding.

Usage

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.
 *//*from   ww  w .  j av  a 2  s  .com*/
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.sf.jabref.gui.entryeditor.EntryEditor.java

/**
 * NOTE: This method is only used for the source panel, not for the
 * other tabs. Look at EntryEditorTab for the setup of text components
 * in the other tabs./*from w  ww  . ja  va2  s  . com*/
 */
private void setupJTextComponent(JTextComponent textComponent) {
    // Set up key bindings and focus listener for the FieldEditor.
    InputMap inputMap = textComponent.getInputMap(JComponent.WHEN_FOCUSED);
    ActionMap actionMap = textComponent.getActionMap();

    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.ENTRY_EDITOR_STORE_FIELD), "store");
    actionMap.put("store", getStoreFieldAction());

    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.ENTRY_EDITOR_NEXT_PANEL), "right");
    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.ENTRY_EDITOR_NEXT_PANEL_2), "right");
    actionMap.put("right", getSwitchRightAction());

    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.ENTRY_EDITOR_PREVIOUS_PANEL), "left");
    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.ENTRY_EDITOR_PREVIOUS_PANEL_2), "left");
    actionMap.put("left", getSwitchLeftAction());

    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.HELP), "help");
    actionMap.put("help", getHelpAction());
    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.SAVE_DATABASE), "save");
    actionMap.put("save", getSaveDatabaseAction());

    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.NEXT_TAB), "nexttab");
    actionMap.put("nexttab", frame.nextTab);
    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.PREVIOUS_TAB), "prevtab");
    actionMap.put("prevtab", frame.prevTab);

    Set<AWTKeyStroke> keys = new HashSet<>(
            textComponent.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
    keys.clear();
    keys.add(AWTKeyStroke.getAWTKeyStroke("pressed TAB"));
    textComponent.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, keys);
    keys = new HashSet<>(textComponent.getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS));
    keys.clear();
    keys.add(KeyStroke.getKeyStroke("shift pressed TAB"));
    textComponent.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, keys);

    textComponent.addFocusListener(new FieldListener());
}

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 a  v a  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:op.tools.SYSTools.java

public static BigDecimal checkBigDecimal(javax.swing.event.CaretEvent evt, boolean nees2BePositive) {
    BigDecimal bd = null;//from   w ww . j  a  va 2  s  .c  o m
    JTextComponent txt = (JTextComponent) evt.getSource();
    Action toolTipAction = txt.getActionMap().get("hideTip");
    if (toolTipAction != null) {
        ActionEvent hideTip = new ActionEvent(txt, ActionEvent.ACTION_PERFORMED, "");
        toolTipAction.actionPerformed(hideTip);
    }
    try {
        OPDE.debug(txt.getText());
        OPDE.debug(assimilateDecimalSeparators(txt.getText()));

        bd = parseDecimal(txt.getText());

        //            bd = BigDecimal.valueOf(Double.parseDouble(assimilateDecimalSeparators(txt.getText())));
        OPDE.debug(bd);
        if (nees2BePositive && bd.compareTo(BigDecimal.ZERO) <= 0) {
            txt.setToolTipText("<html><font color=\"red\"><b>" + SYSTools.xx("misc.msg.invalidnumber")
                    + "</b></font></html>");
            toolTipAction = txt.getActionMap().get("postTip");
            bd = BigDecimal.ONE;
        } else {
            txt.setToolTipText("");
        }

    } catch (NumberFormatException ex) {
        if (nees2BePositive) {
            bd = BigDecimal.ONE;
        } else {
            bd = BigDecimal.ZERO;
        }
        txt.setToolTipText(
                "<html><font color=\"red\"><b>" + SYSTools.xx("misc.msg.invalidnumber") + "</b></font></html>");
        toolTipAction = txt.getActionMap().get("postTip");
        if (toolTipAction != null) {
            ActionEvent postTip = new ActionEvent(txt, ActionEvent.ACTION_PERFORMED, "");
            toolTipAction.actionPerformed(postTip);
        }
    }
    return bd;
}

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);// www  .  ja  v  a2s . c o  m

    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);
}

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

private void resetEditorActions() {

    JTextComponent textComponent = queryEditorTextComponent();

    ActionMap actionMap = textComponent.getActionMap();
    actionMap.remove(LIST_FOCUS_ACTION_KEY);
    actionMap.remove(LIST_SELECTION_ACTION_KEY);
    actionMap.remove(LIST_SCROLL_ACTION_KEY_DOWN);
    actionMap.remove(LIST_SCROLL_ACTION_KEY_UP);

    InputMap inputMap = textComponent.getInputMap();
    inputMap.remove(KEY_STROKE_DOWN);//from ww  w  .j  a v a 2s  .c  o m
    inputMap.remove(KEY_STROKE_UP);
    inputMap.remove(KEY_STROKE_PAGE_DOWN);
    inputMap.remove(KEY_STROKE_PAGE_UP);
    inputMap.remove(KEY_STROKE_ENTER);
    inputMap.remove(KEY_STROKE_TAB);

    inputMap.put(KEY_STROKE_DOWN, existingKeyStrokeDownAction);
    inputMap.put(KEY_STROKE_UP, existingKeyStrokeUpAction);
    inputMap.put(KEY_STROKE_PAGE_DOWN, existingKeyStrokePageDownAction);
    inputMap.put(KEY_STROKE_PAGE_UP, existingKeyStrokePageUpAction);
    inputMap.put(KEY_STROKE_TAB, existingKeyStrokeTabAction);
    inputMap.put(KEY_STROKE_ENTER, existingKeyStrokeEnterAction);

    textComponent.removeCaretListener(this);
}