Undo and redo functions with the Java Implementation Editor

Source: Internet
Author: User
Tags i18n

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.