Java Reference
In-Depth Information
Listing 21-3. Managing UndoableEditListener Objects with UndoableEditSupport
import javax.swing.undo.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
public class AnUndoableComponent {
UndoableEditSupport undoableEditSupport = new UndoableEditSupport(this);
ActionListener actionListenerList = null;
public void addActionListener(ActionListener actionListener) {
actionListenerList = AWTEventMulticaster.add(actionListener,
actionListenerList);
}
public void removeActionListener(ActionListener actionListener) {
actionListenerList = AWTEventMulticaster.remove(actionListener,
actionListenerList);
}
public void addUndoableEditListener(
UndoableEditListener undoableEditListener) {
undoableEditSupport.addUndoableEditListener(undoableEditListener);
}
public void removeUndoableEditListener(
UndoableEditListener undoableEditListener) {
undoableEditSupport.removeUndoableEditListener(undoableEditListener);
}
protected void fireActionPerformed(ActionEvent actionEvent) {
if (actionListenerList != null) {
actionListenerList.actionPerformed(actionEvent);
}
// Need to create your custom type of undoable operation
undoableEditSupport.postEdit(new AbstractUndoableEdit());
}
}
A Complete Undoable Program Example
Now that you've seen the main classes for the Swing Undo Framework, let's look at a complete
example that defines a custom undoable class. The undoable class is a drawing panel in which
each mouse click defines a point to be drawn in a polygon. Figure 21-5 shows the drawable
panel in action, before and after an undo operation.
Search WWH ::




Custom Search