Java JTextArea attachSimpleUndoManager(JTextArea jta)

Here you can find the source of attachSimpleUndoManager(JTextArea jta)

Description

attach Simple Undo Manager

License

Mozilla Public License

Declaration

@SuppressWarnings("serial")
    public static void attachSimpleUndoManager(JTextArea jta) 

Method Source Code

//package com.java2s;
/**/*from   w  ww  .j a  va2s  .com*/
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This Source Code Form is "Incompatible With Secondary Licenses", as
 * defined by the Mozilla Public License, v. 2.0.
 */

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.undo.UndoManager;

public class Main {
    @SuppressWarnings("serial")
    public static void attachSimpleUndoManager(JTextArea jta) {
        UndoManager um = new UndoManager();
        jta.getDocument().addUndoableEditListener(um);

        jta.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("control Z"),
                "doUndo");
        jta.getActionMap().put("doUndo", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (um.canUndo())
                    um.undo();
            }
        });

        jta.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("control Y"),
                "doRedo");
        jta.getActionMap().put("doRedo", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (um.canRedo())
                    um.redo();
            }
        });
    }
}

Related

  1. addMessageLogger(JTextArea t)
  2. addStyle(JTextArea textArea, String labelName, boolean isBorder)
  3. adjustToText(JTextArea testString)
  4. appendNewLine(final JTextArea textArea, final String line)
  5. applyDefaultProperties(final JTextArea comp)
  6. blockUncomment(JTextArea scriptPanel)
  7. clearTextArea(JTextArea textArea)
  8. columns(JTextArea testString)
  9. commentSQL(JTextArea scriptPanel, String commentCharacter)