Search
Undo Manager

The undo manager acts as a mediator between commands, the diagram and the command history. It is represented by the UndoManager class, whose only instance can be accessed by calling the Diagram's getUndoManager method.

By default, undo functionality is disabled - commands are executed, but are not recorded in history for later undo or redo. To enable undo, invoke setUndoEnabled and pass true as argument. You might need to disable undo before executing actions not intended to be undone by the end-users, for example creating special start/end nodes. Another situation in which you might want to disable undoing is when creating programmatically a lot of new diagram items - say hundreds or thousands. JDiagram creates an undo record for each item creation; if the capacity of the command history (getCapacity, setCapacity) is smaller than the number of items created, most of the records will drop from the history queue while adding new ones. That would waste processing power and memory for creating undo records that won't be used.

As an alternative, you can create a composite record by calling startComposite. All subsequent create-item actions are saved in the composite. The composite is saved in the undo history as a single record that allows undo or redo of all constituent actions as a single operation. startComposite creates and returns a CompositeCmd instance, which is marked as active composite in the undo manager. While there is an active composite, all actions that are carried out are added to it. Call the execute method of the active composite to stop recording sub-commands in it and save it in the history as atomic undoable operation.

Use the undo and redo methods of the undo manager to undo or redo actions. Do not call the respective methods of the Command class - they are called internally by the undo manager, and in addition the current command history position is updated and the diagram view is repainted.