Java Reference
In-Depth Information
Note Some developers might find it useful to extend the UndoManager to expose the UndoableEdit list
that it is managing. Then they can display the presentation names of the various edit commands. Fortunately
(or unfortunately), by default, these aren't exposed.
UndoableEditListener Interface and UndoableEditEvent Class
The UndoManager implements the UndoableEditListener interface so that it can be notified
when undoable operations happen. The listener has the following definition:
public interface UndoableEditListener extends EventListener {
public void undoableEditHappened(UndoableEditEvent undoableEditEvent);
}
The UndoableEditListener uses an UndoableEditEvent to tell interested objects when a
command that can be undone has happened. The UndoableEditEvent class includes one property,
edit , which returns the UndoableEdit object for the event: public UndoableEdit getEdit() .
Of all the classes in the Swing-related packages, only the AbstractDocument class (as defined
in the Document interface) comes with built-in support to add these listeners. When creating
your own classes that support undoable operations, you'll need to maintain your own list of
listeners with the help of the UndoableEditSupport class, described next.
UndoableEditSupport Class
The UndoableEditSupport class is similar to the JavaBeans-related classes of
PropertyChangeSupport and VetoableChangeSupport . All three of these classes manage
a list of a specific type of listener. In the case of the UndoableEditSupport class, that
type of listener is the UndoableEditListener . You add listeners with public void
addUndoableListener(UndoableEditListener) and remove them with public void
removeUndoableListener(UndoableEditListener) .
When you want to notify listeners that an UndoableEdit operation has happened, you call
the public void postEdit(UndoableEdit) method, which creates an UndoableEditEvent and
calls the undoableEditHappened() method of each listener.
Note The UndoableEditSupport class also includes support for combining multiple undoable edit
commands into a CompoundEdit with the public void beginUpdate() and public void endUpdate()
methods.
The basic framework for the class usage follows in Listing 21-3. Normally, you tie the undo-
able event to some other operation. In this example, it's tied to the moment an ActionEvent
happens, and therefore any registered ActionListener objects need to be notified.
 
Search WWH ::




Custom Search