How undo and redo work in SimplyHTML

Currently, only the Document class offers undo and redo support to text components. For any edit action that can be cancelled and reinstated again in a particular Document , the Document sends undoable edit events to all UndoableEditListeners registered with that Document.

Creating an UndoManager

To store undoable edit actions for later use in a possible undo or redo action, class FrmMain creates an instance of class UndoManager. The object is stored in private field undo of class FrmMain for later use in the undo/redo implementation.

Defining and registering an UndoableEditListener

Class FrmMain also defines an inner class UndoHandler that implements the UndoableEditListener interface. In its method undoableEditHappened it stores undoable edit actions in the previously created UndoManager. As well, UndoHandler updates the GUI whenever an undoable edit event is received.

The UndoHandler of FrmMain is registered with any Document created or opened by SimplyHTML in the respective actions SHTMLFileOpenAction and SHTMLFileNewAction. Consequently UndoHandler is properly removed from any open Document before it is closed to ensure proper cleanup of the disposed Document in the garbage collection.

Defining actions for undo and redo

To finally being able to perform an undo or redo command, either through the menu or from other places such as a tool bar, class FrmMain defines two actions UndoAction and RedoAction.

In their actionPerformed methods UndoAction and RedoAction call methods undo and redo of FrmMain's UndoManager accordingly. In addition they both have a method update to bring their GUI representation in line with the current undo state (being enabled or not depending on whether or not undoable edits are present in the UndoManager, that is).