With the Java implementation of the editor's Undo, redo function, very convenient, the following is a realization of this function of the class,
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import Javax.swing.JEditorPane;
import Javax.swing.KeyStroke;
import javax.swing.event.UndoableEditEvent;
import Javax.swing.event.UndoableEditListener;
import javax.swing.text.JTextComponent;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import Javax.swing.undo.UndoManager;
/**
* Undowrapper is responsible for adding undo and redo support to text components.
* @author Antonio Vieiro (antonio@antonioshome.net), $Author: $
* @version $Revision: $
*/
public class Undowrapper
implements Undoableeditlistener
{
private UndoManager UndoManager;
private UndoAction undoaction;
private redoaction redoaction;
private JEditorPane textcomponent;
/**
* Creates a new instance of Undowrapper
*/
public Undowrapper (JEditorPane acomponent)
{
textcomponent = acomponent;
UndoManager = new UndoManager ();
undoaction = new UndoAction ();
redoaction = new RedoAction ();
textcomponent.getdocument (). Addundoableeditlistener (this);
Textcomponent.getinputmap (). put (keystroke) undoaction.getvalue (
Action.accelerator_key), "undo");
Textcomponent.getinputmap (). put (keystroke) redoaction.getvalue (
action.accelerator_key), "Redo");
Textcomponent.getactionmap (). Put ("undo", UndoAction);
Textcomponent.getactionmap (). Put ("Redo", redoaction);
}
public void undoableedithappened (Undoableeditevent e)
{
Undomanager.addedit (E.getedit ());
undoaction.updateundostate ();
redoaction.updateredostate ();
}
/**
* UndoAction is the Action responsible for handling the undo operation.
*/
class UndoAction
extends AbstractAction
{
public undoaction ()
{
Super ("Cannot undo"); todo:i18n
setenabled (FALSE);
Putvalue (Action.accelerator_key, Keystroke.getkeystroke ("Ctrl Z"));
}
public void actionperformed (ActionEvent e)
{
Try
{
Undomanager.undo ();
}
catch (cannotundoexception cue)
{
//todo:use logging?
Cue.printstacktrace (System.err);
}
updateundostate ();
redoaction.updateredostate ();
}
void Updateundostate ()
{
if (Undomanager.canundo ())
{
setenabled (TRUE);
Putvalue (Action.name, "Undo"); TODO i18n
}
Else
{
setenabled (FALSE);
Putvalue (Action.name, "cannot undo"); TODO i18n
}
}
}
/**
* RedoAction is the Action responsible for handling the redo operation.
*/
class RedoAction
extends AbstractAction
{
public redoaction ()
{
super ("Cannot redo"); TODO i18n
setenabled (FALSE);
Putvalue (Action.accelerator_key, Keystroke.getkeystroke ("Ctrl Y"));
}
public void actionperformed (ActionEvent e)
{
Try
{
Undomanager.redo ();
}
catch (cannotredoexception CRE)
{
//todo:use logging?
Cre.printstacktrace (System.err);
}
updateredostate ();
undoaction.updateundostate ();
}
void Updateredostate ()
{
if (Undomanager.canredo ())
{
setenabled (TRUE);
Putvalue (Action.name, "Redo"); TODO i18n
}
Else
{
setenabled (FALSE);
Putvalue (Action.name, "cannot redo"); TODO i18n
}
}
}
undoaction getundoaction ()
{
return undoaction;
}
redoaction getredoaction ()
{
return redoaction;
}
}
When you use it, you just need to pass the jeditorpane you created as an object into the Undowrapper. Use the following method as new Undowrapper (Editorpane);
Ok so your editor has the undo Redo function, and is not limited by the number of times.