下面列出了javax.swing.text.Document#removeUndoableEditListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void startNewDocument() {
Document oldDoc = textPane.getDocument();
if (oldDoc != null)
oldDoc.removeUndoableEditListener(undoHandler);
HTMLEditorKit editorKit = new HTMLEditorKit();
document = (HTMLDocument) editorKit.createDefaultDocument();
textPane.setDocument(document);
currentFile = null;
setTitle("HTMLDocumentEditor");
textPane.getDocument().addUndoableEditListener(undoHandler);
resetUndoManager();
}
public void openDocument() {
try {
File current = new File(".");
JFileChooser chooser = new JFileChooser(current);
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setFileFilter(new HTMLFileFilter());
int approval = chooser.showSaveDialog(this);
if (approval == JFileChooser.APPROVE_OPTION) {
currentFile = chooser.getSelectedFile();
setTitle(currentFile.getName());
FileReader fr = new FileReader(currentFile);
Document oldDoc = textPane.getDocument();
if (oldDoc != null)
oldDoc.removeUndoableEditListener(undoHandler);
HTMLEditorKit editorKit = new HTMLEditorKit();
document = (HTMLDocument) editorKit.createDefaultDocument();
editorKit.read(fr, document, 0);
document.addUndoableEditListener(undoHandler);
textPane.setDocument(document);
resetUndoManager();
}
} catch (BadLocationException ble) {
System.err.println("BadLocationException: " + ble.getMessage());
} catch (FileNotFoundException fnfe) {
System.err.println("FileNotFoundException: " + fnfe.getMessage());
} catch (IOException ioe) {
System.err.println("IOException: " + ioe.getMessage());
}
}
public void testSourceEditSyncUndo() throws Exception {
defaultSetup();
UndoManager urListener = new UndoManager();
Document doc = mModel.getBaseDocument();
mModel.addUndoableEditListener(urListener);
mModel.startTransaction();
B b2 = new B(mModel, 2);
mModel.getRootComponent().addAfter(b2.getName(), b2, TestComponent3._A);
mModel.endTransaction();
assertEquals("first edit setup", 2, mModel.getRootComponent().getChildren(B.class).size());
// see fix for issue 83963, with this fix we need coordinate edits from
// on XDM model and on document buffer. This reduce XDM undo/redo efficiency,
// but is the best we can have to satisfy fine-grained text edit undo requirements.
mModel.removeUndoableEditListener(urListener);
doc.addUndoableEditListener(urListener);
Util.setDocumentContentTo(doc, "resources/test2.xml");
assertEquals("undo sync", 1, mModel.getRootComponent().getChildren(C.class).size());
mModel.sync();
doc.removeUndoableEditListener(urListener);
assertEquals("sync setup", 1, mModel.getRootComponent().getChildren(B.class).size());
assertEquals("sync setup", 0, mModel.getRootComponent().getChildren(C.class).size());
// setDocumentContentTo did delete all, then insert, hence 2 undo's'
urListener.undo(); urListener.undo();
mModel.sync(); // the above undo's are just on document buffer, needs sync (inefficient).
assertEquals("undo sync", 1, mModel.getRootComponent().getChildren(C.class).size());
assertEquals("undo sync", 2, mModel.getRootComponent().getChildren(B.class).size());
urListener.undo();
assertEquals("undo first edit before sync", 1, mModel.getRootComponent().getChildren(B.class).size());
urListener.redo();
assertEquals("redo first edit", 1, mModel.getRootComponent().getChildren(C.class).size());
assertEquals("redo first edit", 2, mModel.getRootComponent().getChildren(B.class).size());
// needs to back track the undo's, still needs sync'
urListener.redo(); urListener.redo();
mModel.sync();
assertEquals("redo to sync", 1, mModel.getRootComponent().getChildren(B.class).size());
assertEquals("redo to sync", 0, mModel.getRootComponent().getChildren(C.class).size());
}