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: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());
        }/*ww  w. ja  v a2  s  . c o m*/
    });

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

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

    doc.addUndoableEditListener(new UndoableEditListener() {
        public void undoableEditHappened(UndoableEditEvent evt) {
            undo.addEdit(evt.getEdit());
        }/*from  www .ja va  2s  . c  o 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());
        }/*  www  .  j  av  a  2 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");

    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  w w.  j av  a 2s  .  c o 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");

    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  ww  .jav a2 s . co m
    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

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

    text.getDocument().addUndoableEditListener(new UndoableEditListener() {
        @Override/*w w w  .j av a  2  s.com*/
        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: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());
        }//from  w w w  .jav a 2 s  . c o  m
    });

    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 NavigationFilterPrefixWithBackspace(int prefixLength, JTextComponent component) {
    this.prefixLength = prefixLength;
    deletePrevious = component.getActionMap().get("delete-previous");
    component.getActionMap().put("delete-previous", new BackspaceAction());
    component.setCaretPosition(prefixLength);
}

From source file:gg.pistol.sweeper.gui.component.DecoratedPanel.java

/**
 * Helper method to add a contextual menu on a text component that will allow for Copy & Select All text actions.
 *///w w  w . j  a  v a 2  s .c  o  m
protected void addCopyMenu(final JTextComponent component) {
    Preconditions.checkNotNull(component);

    if (!component.isEditable()) {
        component.setCursor(new Cursor(Cursor.TEXT_CURSOR));
    }

    final JPopupMenu contextMenu = new JPopupMenu();
    JMenuItem copy = new JMenuItem(component.getActionMap().get(DefaultEditorKit.copyAction));
    copy.setText(i18n.getString(I18n.TEXT_COPY_ID));
    contextMenu.add(copy);
    contextMenu.addSeparator();

    JMenuItem selectAll = new JMenuItem(component.getActionMap().get(DefaultEditorKit.selectAllAction));
    selectAll.setText(i18n.getString(I18n.TEXT_SELECT_ALL_ID));
    contextMenu.add(selectAll);

    component.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON3) {
                contextMenu.show(component, e.getX(), e.getY());
            }
        }
    });
}

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.
 *///w  w  w.  j  a v a2  s  .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);
}