/**
* Changes the specified text component to support undo and redo using Ctrl-Z and Ctrl-Y.
* @param textComponent the text component to be modified
*/
public static void makeTextComponentUndoable(JTextComponent textComponent) {
final UndoManager undoManager = new UndoManager();
final Action undoAction = new AbstractAction(UIMessages.instance.getString("GenericUndoName")) { //$NON-NLS-1$
private static final long serialVersionUID = -2135822508998640523L;
public void actionPerformed(ActionEvent e) {
try {
if (undoManager.canUndo()) {
undoManager.undo();
}
}
catch (CannotUndoException ex) {
}
}
};
final Action redoAction = new AbstractAction(UIMessages.instance.getString("GenericRedoName")) { //$NON-NLS-1$
private static final long serialVersionUID = 2928127985356997903L;
public void actionPerformed(ActionEvent e) {
try {
if (undoManager.canRedo()) {
undoManager.redo();
}
}
catch (CannotUndoException ex) {
}
}
};
UndoableEditListener undoListener = new UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent e) {
undoManager.addEdit(e.getEdit());
}
};
textComponent.getDocument().addUndoableEditListener(undoListener);