Lesson: Enhancing an Editor Application With Undo/Redo Support

Class Graph2DUndoManager provides our application with convenient undo/redo support covering graph structure changes as well as changes in the visual representation of graph elements.

The code in Example 1.1, “Setting up undo/redo support” presents the general setup that we will need in an application to make use of the services of Graph2DUndoManager. You can observe this code also in the UndoRedo1 application.

Example 1.1. Setting up undo/redo support

public Graph2DUndoManager getUndoSupport() {
  if (undo == null) {
    // Create undo/redo support that listens to graph structure changes and also 
    // handles requests for backing up realizers.
    undo = new Graph2DUndoManager(getCustomGraph());
    
    // Set the application's view as the view container for the undo/redo 
    // support so the view gets updated after undo/redo actions have been 
    // performed.
    undo.setViewContainer(view);
  }
  return undo;
}

Example 1.2, “Adding undo/redo actions to a tool bar” shows how we can add the default undo and redo actions provided by class Graph2DUndoManager to the toolbar of an application.

Example 1.2. Adding undo/redo actions to a tool bar

// Adding an undo action to the tool bar.
toolbar.add(getUndoSupport().getUndoAction());
// Adding a redo action to the tool bar.
toolbar.add(getUndoSupport().getRedoAction());

Some Observations

UndoRedo1 builds upon the AdditionalDataEditor application, which allows to associate additional data, a custom text, with the nodes of the graph. However, if we change the custom text for a node using the text field, we will not be able to reverse our changes via the undo/redo support, since it does not cover such events!

We will see in the next lesson how we can add undo/redo support for changes to the additional data. Read on...

Related Resources

You will find related information in the yFiles for Java Developer's Guide:

In the yFiles for Java API:

In the yFiles for Java source code demos: