Java JTextComponent addUndoRedo(JTextComponent comp)

Here you can find the source of addUndoRedo(JTextComponent comp)

Description

add Undo Redo

License

Open Source License

Declaration

public static void addUndoRedo(JTextComponent comp) 

Method Source Code

//package com.java2s;
/**/*  w  ww .  j a va2s  .  co  m*/
 * PagaVCS is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.<br>
 * <br>
 * PagaVCS is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.<br>
 * <br>
 * You should have received a copy of the GNU General Public License along with
 * PagaVCS; If not, see http://www.gnu.org/licenses/.
 */

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;

import javax.swing.InputMap;

import javax.swing.KeyStroke;

import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;

public class Main {
    public static void addUndoRedo(JTextComponent comp) {
        final UndoManager undo = new UndoManager();
        Document doc = comp.getDocument();
        doc.addUndoableEditListener(new UndoableEditListener() {

            public void undoableEditHappened(UndoableEditEvent evt) {
                undo.addEdit(evt.getEdit());
            }
        });
        ActionMap actionMap = comp.getActionMap();
        InputMap inputMap = comp.getInputMap();
        actionMap.put("Undo", new AbstractAction("Undo") {

            public void actionPerformed(ActionEvent evt) {
                try {
                    if (undo.canUndo()) {
                        undo.undo();
                    }
                } catch (CannotUndoException e) {
                }
            }
        });
        inputMap.put(KeyStroke.getKeyStroke("control Z"), "Undo");
        actionMap.put("Redo", new AbstractAction("Redo") {

            public void actionPerformed(ActionEvent evt) {
                try {
                    if (undo.canRedo()) {
                        undo.redo();
                    }
                } catch (CannotRedoException e) {
                }
            }
        });
        inputMap.put(KeyStroke.getKeyStroke("control Y"), "Redo");
    }
}

Related

  1. addChangeListener(JTextComponent text, ChangeListener changeListener)
  2. addJTextComponentListeners(JTextComponent c, Object... objs)
  3. addTextListener( JTextComponent c, Runnable listener )
  4. addTextUpdateListener(JTextComponent textComponent, Consumer listener)
  5. adjustForLineComment(JTextComponent editor, int iStart)
  6. appendToText(JTextComponent textComp, String s)
  7. applyDefaultProperties(final JTextComponent comp)
  8. biggestFont(final javax.swing.text.JTextComponent c)