Java Reference
In-Depth Information
CHAPTER 21
■ ■ ■
The Undo Framework
I
n Chapter 20, you discovered how to customize your Swing-based applications by examining
the pluggable look and feel architecture support. In this chapter, you'll examine the Undo
Framework provided with the JFC as part of the Swing packages.
The Swing packages from Sun include a facility for supporting undo operations within
your applications. It allows you to support undoable and redoable operations that change the
state of your data. Although the framework is part of the Swing package hierarchy, it's usable
within any application, not just component-based applications.
Working with the Undo Framework
Found in the
javax.swing.undo
package, the Undo Framework includes five classes, two inter-
faces, and two exceptions. To support the Undo Framework, a related interface and event are
included in the
javax.swing.event
package. At the root of it all is the
UndoableEdit
interface.
The interface forms the basis for encapsulating operations that can be undone or redone using
the Command design pattern.
■
Note
Although technically part of the JFC/Project Swing release as an element in the
javax.swing.undo
package, the undo facility actually belongs in the
java.util
package. Unfortunately,
when Swing first came out, it needed to be usable within JDK 1.1. Because Swing text packages use this undo
facility, that facility needed to be in the Swing package hierarchy. Because Sun couldn't alter the Core API set
for JDK 1.1, you'll find the undo support as part of the JFC/Swing packages, and it is therefore usable under
the old JDK 1.1, as well as the 1.2 and later platforms, where Swing is standard.
The root implementation class of the undoable command is the
AbstractUndoableEdit
class. Don't let the class name fool you, though—it isn't abstract. The children of the root
command are the
CompoundEdit
and
StateEdit
command classes.
The
CompoundEdit
class allows you to combine multiple undoable operations, in which
some of the undoable operations could be
StateEdit
objects that store state changes. The
Swing text components create
DefaultDocumentEvent
commands when their contents change.
The command is a subclass of
CompoundEdit
as well as an inner class of
AbstractDocument
. An
additional encapsulated command is the
UndoManager
, which is a subclass of
CompoundEdit
.
783