File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Undo and Redo in JText viewer?

 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
can anyone give me any idea how to do Undo and Redo in JText viewer? I am completely lost..!
 
Campbell Ritchie
Sheriff
Pie
Posts: 47244
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Store your states on a stack, linked list or similar.
Undo means go back one place on the stack.
Redo means go one place forwards.
Back from state 0 means throw CantUndoException.
Forward from state n means throw CantRedoException.
Any changes of state mean lost whatever is ahead of the present point on the stack, probably easiest done by resetting a maxIndex variable.

The alternative is to store each state change as an operation and put the operations into a list. You can the insert an operation in the middle of the list if you so wish.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JTextComponent, the base class for most text controls in Swing, is your friend here.

Each JTextComponent has a Document that can be retrieved using the "getDocument" method. Document has a method "addUndoableEditListener" which allows you to attach UndoableEditListeners. These trigger "undoableEditHappened" events; the event has an UndoableEdit.

Create an instance of UndoManager, and each time such an UndoableEdit occurs add it with the "addEdit" method.

Now this will register and store your undo actions, but there's still no means of executing them. You can do this through a menu, popup menu, key presses or a combination.

The following code shows part of a key listener that will undo or redo, using CTRL+Z for undo and CTRL+Y for redo:

Now you can investigate UndoManager for some more cool methods, which include "getUndoPresentationName", "getRedoPresentationName" and "getUndoOrRedoPresentationName".
 
Campbell Ritchie
Sheriff
Pie
Posts: 47244
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brilliant, Rob. I never realised that.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've also been using Java's own undo mechanism for a few months now, and writing support for text components was of course the first to try it on

That one's easy as the document can listen for undoable edits on its own. For other controls (such as JSpinner and JComboBox, I've got those working too) you have to write custom undoable edit classes.
Sure, you can use the text document used in JSpinner, but there's one drawback: setText first removes the entire text (one edit), then adds the new text (two edits). So I found out that undoing to the previous value would require two undo actions each time. In the end, I listen to the stateChanged event of the JSpinner, and create a custom undoable edit there based on the old and new values. That class' undo() method sets the old value, the redo() methods sets the new value. Quite easy in fact.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic