be.ugent.caagt.swirl.undoredo

Class UndoManager


public class UndoManager
extends java.lang.Object

Manages a list of changes of type UndoableChange. Allows changes of this type to be undone and redone. Listeners can be registered with this manager and will be notified whenever a change has been undone or redone.

Changes come in logical groups. A single call to undoLast() or redoLast() always undoes or redoes one group at the time. Groups are constructed by merging subsequent changes with the leader of the group. The leader also determines the caption for the undo and redo buttons of the entire group. If this is not the desired behaviour an extra dummy change (with trivial undo and redo operations and the desired captions) can be used as a leader.

Additionally the manager can be used to keep track of whether the state of the managed data is dirty, i.e., whether important information would be lost if the application would be aborted without saving. This is done by marking the manager state (i.e., the index of the current change in the list) when a save operation has just completed. If at a later time the current index position is different from the marked index position, the state of the data should be considered dirty. Registered listeners will also be notified whenever the mark changes.

Note: For this simple mark strategy to be valid, you must make sure that every action which changes the managed data is registered with the undo manager and that actions which do not change the data in a significant way, are not.

Constructor Summary

UndoManager()
Default constructor.

Method Summary

void
add(UndoableChange change, boolean leader)
Register an undoable change with the manager.
void
addUndoListener(UndoListener l)
Register a listener with this object.
boolean
canRedo()
Are there currently any changes that can be redone?
boolean
canUndo()
Are there currently any changes that can be undone?
void
clear()
Clear the list of undoable commands.
protected void
fireUndoStateChanged()
Notify all listeners of a change to the undo state.
String
getRedoCaption()
Return the caption for a redo button.
String
getUndoCaption()
Return the caption for an undo button.
boolean
isDirty()
Check whether the current index position is different from the mark.
void
redoLast()
Redo the last group of commands which was previously undone.
void
removeUndoListener(UndoListener l)
Unregister a listener with this object.
void
setMark()
Put the mark at the current index position.
void
undoLast()
Undo te last group of commands.

Constructor Details

UndoManager

public UndoManager()
Default constructor.

Method Details

add

public void add(UndoableChange change,
                boolean leader)
Register an undoable change with the manager.
Parameters:
leader - If true, the call will start a new group of which the given element becomes the leader. If false the given undoable change will be appended to the current group.

addUndoListener

public void addUndoListener(UndoListener l)
Register a listener with this object.

canRedo

public boolean canRedo()
Are there currently any changes that can be redone?

canUndo

public boolean canUndo()
Are there currently any changes that can be undone?

clear

public void clear()
Clear the list of undoable commands.

fireUndoStateChanged

protected void fireUndoStateChanged()
Notify all listeners of a change to the undo state.

getRedoCaption

public String getRedoCaption()
Return the caption for a redo button. This caption is obtained from the leader of the next group to be redone.

getUndoCaption

public String getUndoCaption()
Return the caption for an undo button. This caption is obtained from the leader of the next group to be undone.

isDirty

public boolean isDirty()
Check whether the current index position is different from the mark. If true, the managed data should probably be considered in a dirty state.

redoLast

public void redoLast()
Redo the last group of commands which was previously undone.

removeUndoListener

public void removeUndoListener(UndoListener l)
Unregister a listener with this object.

setMark

public void setMark()
Put the mark at the current index position. Typically this is done just after the managed data has been saved.

undoLast

public void undoLast()
Undo te last group of commands. Commands are undone in reverse order of registration: the leader of the group will be undone last.