This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes Undo and Redo in JText viewer? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Undo and Redo in JText viewer?" Watch "Undo and Redo in JText viewer?" New topic
Author

Undo and Redo in JText viewer?

Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Hi,
can anyone give me any idea how to do Undo and Redo in JText viewer? I am completely lost..!


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38437
    
  23
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

Joined: Oct 27, 2005
Posts: 19672
    
  18

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".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38437
    
  23
Brilliant, Rob. I never realised that.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Undo and Redo in JText viewer?