/** saveDocument */
  public boolean saveDocument() throws CollabException {
    Debug.log(
        this, // NoI18n
        "CollabJavaHandler, saving document: " + getName()); // NoI18n

    try {
      EditorCookie cookie = getEditorCookie();

      if (cookie != null) {
        cookie.saveDocument();
      }
    } catch (IOException iox) {
      Debug.log(
          this, // NoI18n
          "CollabJavaHandler, Exception occured while saving the document: " // NoI18n
              + getName()); // NoI18n

      return false;
    }

    return true;
  }
Пример #2
0
  public static void overwriteFile(Node srcNode, Node destNode) throws IOException {

    DataObject srcDO = srcNode.getLookup().lookup(DataObject.class);
    FileObject srcFO = srcDO.getPrimaryFile();

    DataObject destDO = destNode.getLookup().lookup(DataObject.class);
    FileObject destFO = destDO.getPrimaryFile();

    // To avoid any confusion, save modified source first.
    if (srcDO.isModified()) {
      NotifyDescriptor d =
          new NotifyDescriptor.Confirmation(
              NbBundle.getMessage(
                  FileNodeUtil.class, "MSG_SaveModifiedSource", srcFO.getNameExt()), // NOI18N
              NbBundle.getMessage(FileNodeUtil.class, "TTL_SaveModifiedSource"), // NOI18N
              NotifyDescriptor.OK_CANCEL_OPTION);
      if (DialogDisplayer.getDefault().notify(d) == NotifyDescriptor.OK_OPTION) {
        EditorCookie srcEditorCookie = srcDO.getCookie(EditorCookie.class);
        srcEditorCookie.saveDocument();
      }
    }
    //        // Alternatively, we could use the in-memory copy of the source file
    //        EditorCookie srcEditorCookie =
    //                (EditorCookie) srcDO.getCookie(EditorCookie.class);
    //        Document srcDocument = srcEditorCookie.getDocument();
    //        if (srcDocument == null) {
    //            Task loadTask = srcEditorCookie.prepareDocument();
    //            new RequestProcessor().post(loadTask);
    //
    //            loadTask.waitFinished();
    //            srcDocument = srcEditorCookie.getDocument();
    //        }
    //        String srcText = srcDocument.getText(0, srcDocument.getLength());

    // From now on, we will only be using the on-disk copy of the source file.

    if (destDO.isModified()) {
      NotifyDescriptor d =
          new NotifyDescriptor.Confirmation(
              NbBundle.getMessage(
                  FileNodeUtil.class,
                  "MSG_OverwriteModifiedDestination",
                  destFO.getNameExt()), // NOI18N
              NbBundle.getMessage(FileNodeUtil.class, "TTL_OverwriteModifiedDestination"), // NOI18N
              NotifyDescriptor.OK_CANCEL_OPTION);
      if (DialogDisplayer.getDefault().notify(d) == NotifyDescriptor.CANCEL_OPTION) {
        return;
      }

      EditorCookie destEditorCookie = destDO.getCookie(EditorCookie.class);

      InputStream inputStream = null;
      try {
        inputStream = srcFO.getInputStream();
        String srcText = getInputStreamContents(inputStream);

        Document outputDocument = destEditorCookie.getDocument();
        try {
          outputDocument.remove(0, outputDocument.getLength());
        } catch (java.lang.Exception e) {
          // Ignore exception here on purpose.
          // One of the listener from xml module throws NPE:
          // at
          // org.netbeans.modules.xml.text.completion.GrammarManager.isGuarded(GrammarManager.java:170)
          // at
          // org.netbeans.modules.xml.text.completion.GrammarManager.removeUpdate(GrammarManager.java:140)
          // at
          // org.netbeans.lib.editor.util.swing.PriorityDocumentListenerList.removeUpdate(PriorityDocumentListenerList.java:63)
          // at javax.swing.text.AbstractDocument.fireRemoveUpdate(AbstractDocument.java:242)
          // at org.netbeans.editor.BaseDocument.fireRemoveUpdate(BaseDocument.java:1305)
          // at org.netbeans.editor.BaseDocument.remove(BaseDocument.java:737)
        }
        outputDocument.insertString(0, srcText, null);
        destEditorCookie.saveDocument();
      } catch (BadLocationException e) {
        e.printStackTrace();
        throw new IOException(e.getMessage());
      } finally {
        try {
          if (inputStream != null) {
            inputStream.close();
          }
        } catch (Exception e) {;
        }
      }
    } else {
      FileLock lock = destFO.lock();
      InputStream inputStream = null;
      OutputStream outputStream = null;

      try {
        outputStream = destFO.getOutputStream(lock);
        inputStream = srcFO.getInputStream();
        FileUtil.copy(inputStream, outputStream);
      } finally {
        try {
          if (inputStream != null) {
            inputStream.close();
          }
          if (outputStream != null) {
            outputStream.close();
          }

          lock.releaseLock();
        } catch (Exception e) {;
        }
      }
    }
  }