June 24, 2010, 4:29 p.m.
posted by pythonrocks
How to Write an Undoable Edit ListenerUndoable edit events occur when an operation that can be undone occurs on a component. Currently only text components fire undoable edit events, and then only indirectly. The text component's document fires the events. For text components, undoable operations include inserting characters, deleting characters, and modifying the style of text. Programs typically listen to undoable edit events to assist in the implementation of undo and redo commands. Here's the undoable edit event handling code from an application called TextComponentDemo.[36]
...
//where initialization occurs
document.addUndoableEditListener(new MyUndoableEditListener());
...
protected class MyUndoableEditListener implements UndoableEditListener {
public void undoableEditHappened(UndoableEditEvent e) {
//Remember the edit and update the menus
undo.addEdit(e.getEdit());
undoAction.updateUndoState();
redoAction.updateRedoState();
}
}
For a discussion about the undoable edit listener aspect of the program see Implementing Undo and Redo (page 68) in Chapter 3. The Undoable Edit Listener APIFigure lists the only method in the UndoableEditListener interface and Figure describes the methods in the UndoableEditEvent class. Note that UndoableEditListener has no corresponding adapter class. Also refer to the UndoableEditListener API documentation at: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/event/UndoableEditListener.html. The UndoableEditEvent API documentation is online at: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/event/UndoableEditEvent.html.
Examples That Use Undoable Edit ListenersThe following table lists the example that uses undoable edit listeners.
|
- Comment