Java Reference
In-Depth Information
frame.add(toolbar, BorderLayout.NORTH);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
If you plan to use the Undo Framework only with the Swing text components, you can skip
reading the rest of the chapter. On the other hand, if you want to use the framework with other
components, or even in a noncomponent setting, you'll want to read the remaining sections,
which describe the inner workings of the framework in more detail.
The Command Design Pattern
The undo facility of the
javax.swing.undo
package utilizes the Command design pattern,
which has the following participants:
Command:
The
UndoableEdit
interface defines the interface for executing the
undo/redo operations.
•
Concrete Command:
Instances of the
AbstractUndoableEdit
class, or more specifically
its subclasses, implement the necessary Command interface. They bind the commands
to the receiver (
Document
) to modify its contents.
•
•
Client:
In the case of the Swing text components, the
Document
does the creation of the
actual
AbstractUndoableEdit
subclass, an
AbstractDocument.DefaultDocumentEvent
by default.
Invoker:
The
UndoManager
serves as the Invoker of the
UndoableEdit
command. Normally,
someone else tells the Invoker when to do the invoking. However, it's the Invoker who
notifies the specific
UndoableEdit
instance when to undo/redo the command.
•
Receiver:
The
Document
is the receiver of the command from the actual
AbstractUndoableEdit
subclass. It knows how to process the request.
•