@Override public Task prepareDocument() { Task task = super.prepareDocument(); // Avoid listening to the same task more than once. if (task != prepareTask2) { prepareTask2 = task; task.addTaskListener( new TaskListener() { public void taskFinished(Task task) { QuietUndoManager undo = getUndoManager(); StyledDocument doc = getDocument(); synchronized (undo) { // Now that the document is ready, pass it to the manager. undo.setDocument((AbstractDocument) doc); if (!undo.isCompound()) { // The superclass prepareDocument() adds the undo/redo // manager as a listener -- we need to remove it since // we will initially listen to the model instead. doc.removeUndoableEditListener(undo); // If not listening to document, then listen to model. addUndoManagerToModel(undo); } } prepareTask2 = null; } }); } return task; }
@Override public Task reloadDocument() { Task task = super.reloadDocument(); task.addTaskListener( new TaskListener() { public void taskFinished(Task task) { EventQueue.invokeLater( new Runnable() { public void run() { QuietUndoManager undo = getUndoManager(); StyledDocument doc = getDocument(); // The superclass reloadDocument() adds the undo // manager as an undoable edit listener. synchronized (undo) { if (!undo.isCompound()) { doc.removeUndoableEditListener(undo); } } } }); } }); return task; }