I have an editorpane which displays editable information which changes based on what the user has selected in a list. So, for implementing my undo/redo feature I must first re-select the item that the user had selected when making their edits prior to undoing/redoing them. However, I am finding it difficult to add functionality to default document events because when I attempt to extend that class I get the "No enclosing instance" error.
below is my undomanager code. I know there is no setSelection and getCurrentSelection for JEditorPane but just pretend I am using an extended version with those features to determine what item the user has selected.
public class MyUndoManager extends UndoManager {
private JEditorPane editor;
public MyUndoManager() {
super();
}
public void setEditor(JEditorPane editor) {
this.editor = editor开发者_开发百科;
}
@Override
public synchronized boolean addEdit(UndoableEdit anEdit) {
if (anEdit instanceof javax.swing.text.AbstractDocument.DefaultDocumentEvent) {
try {
MyDocumentEvent evt = ((MyDocumentEvent) anEdit);
evt.setCallbackObj(editor.getCurrentSelection());
return super.addEdit(anEdit);
} catch (Exception e) {
e.printStackTrace();
return false;
}
} else {
return super.addEdit(anEdit);
}
}
private class MyDocumentEvent extends javax.swing.text.AbstractDocument.DefaultDocumentEvent {
private Object callbackObj;
public void setCallbackObj(Object o) {
this.callbackObj = o;
}
@Override
public void undo() throws CannotUndoException {
editor.setSelection(o);
super.undo();
}
@Override
public void redo() throws CannotRedoException {
editor.setSelection(o);
super.redo();
}
}
}
It could be accessible by your code if DefaultDocumentEvent
were static field of AbstractDocument
.
精彩评论