@Override
 protected void notifyClosed() {
   // Stop listening to the undoable edit sources when we are closed.
   QuietUndoManager undo = getUndoManager();
   StyledDocument doc = getDocument();
   synchronized (undo) {
     // May be null when closing the editor.
     if (doc != null) {
       doc.removeUndoableEditListener(undo);
       undo.endCompound();
       undo.setDocument(null);
     }
     try {
       WadlModel model = getModel();
       if (model != null) {
         model.removeUndoableEditListener(undo);
       }
       // Must unset the model when no longer listening to it.
       undo.setModel(null);
     } catch (IOException ioe) {
       // Model is gone, but just removing the listener is not
       // going to matter anyway.
     }
   }
   super.notifyClosed();
 }
  /**
   * Adds the undo/redo manager to the document as an undoable edit listener, so it receives the
   * edits onto the queue. The manager will be removed from the model as an undoable edit listener.
   *
   * <p>This method may be called repeatedly.
   */
  public void addUndoManagerToDocument() {
    // This method may be called repeatedly.
    // Stop the undo manager from listening to the model, as it will
    // be listening to the document now.
    QuietUndoManager undo = getUndoManager();
    StyledDocument doc = getDocument();

    try {
      model = getModel();
    } catch (IOException ioe) {
      // Model is gone, but just removing the listener is not
      // going to matter anyway.
    }
    synchronized (undo) {
      if (model != null) {
        model.removeUndoableEditListener(undo);
      }
      // Must unset the model when no longer listening to it.
      undo.setModel(null);
      // Document may be null if the cloned views are not behaving correctly.
      if (doc != null) {
        // Ensure the listener is not added twice.
        doc.removeUndoableEditListener(undo);
        doc.addUndoableEditListener(undo);
        // Start the compound mode of the undo manager, such that when
        // we are hidden, we will treat all of the edits as a single
        // compound edit. This avoids having the user invoke undo
        // numerous times when in the model view.
        undo.beginCompound();
      }
    }
  }
 /** Have the Wadl model sync with the document. */
 public void syncModel() {
   // Only sync the document if the change relates to loss of focus,
   // which indicates that we are switching from the source view.
   // Update the tree with the modified text.
   try {
     WadlModel model = getModel();
     if (model != null) {
       model.sync();
     }
   } catch (Throwable ioe) {
     // The document cannot be parsed
     NotifyDescriptor nd =
         new NotifyDescriptor.Message(
             NbBundle.getMessage(MultiViewSupport.class, "MSG_NotWellformedWadl"),
             NotifyDescriptor.ERROR_MESSAGE);
     DialogDisplayer.getDefault().notify(nd);
   }
 }
 /**
  * Add the undo/redo manager undoable edit listener to the model.
  *
  * <p>Caller should synchronize on the undo manager prior to calling this method, to avoid thread
  * concurrency issues.
  *
  * @param undo the undo manager.
  */
 private void addUndoManagerToModel(QuietUndoManager undo) {
   // This method may be called repeatedly.
   try {
     model = getModel();
     if (model != null) {
       // Ensure the listener is not added twice.
       model.removeUndoableEditListener(undo);
       model.addUndoableEditListener(undo);
       // Ensure the model is sync'd when undo/redo is invoked,
       // otherwise the edits are added to the queue and eventually
       // cause exceptions.
       undo.setModel(model);
     }
   } catch (IOException ioe) {
     // Model is gone, but just removing the listener is not
     // going to matter anyway.
   }
 }