Пример #1
1
  /**
   * Updates the element info to a change of the file content and sends out appropriate
   * notifications.
   *
   * @param fileEditorInput the input of an text editor
   */
  protected void handleElementContentChanged(IEditorInput fileEditorInput) {
    FileInfo info = (FileInfo) getElementInfo(fileEditorInput);
    if (info == null) {
      return;
    }

    IStorage storage = EditorUtils.getStorageFromInput(fileEditorInput);
    if (storage instanceof IFile) {
      IFile file = (IFile) storage;
      IDocument document = createEmptyDocument();
      IStatus status = null;

      try {

        try {
          refreshFile(file);
        } catch (CoreException x) {
          log.error("handleElementContentChanged", x);
        }

        setDocumentContent(document, file);
      } catch (CoreException x) {
        status = x.getStatus();
      }

      String newContent = document.get();

      if (!newContent.equals(info.fDocument.get())) {

        // set the new content and fire content related events
        fireElementContentAboutToBeReplaced(fileEditorInput);

        removeUnchangedElementListeners(fileEditorInput, info);

        info.fDocument.removeDocumentListener(info);
        info.fDocument.set(newContent);
        info.fCanBeSaved = false;
        info.modificationStamp = computeModificationStamp(file);
        info.fStatus = status;

        addUnchangedElementListeners(fileEditorInput, info);

        fireElementContentReplaced(fileEditorInput);

      } else {

        removeUnchangedElementListeners(fileEditorInput, info);

        // fires only the dirty state related event
        info.fCanBeSaved = false;
        info.modificationStamp = computeModificationStamp(file);
        info.fStatus = status;

        addUnchangedElementListeners(fileEditorInput, info);

        fireElementDirtyStateChanged(fileEditorInput, false);
      }
    }
  }
Пример #2
0
  @Override
  protected ElementInfo createElementInfo(Object element) throws CoreException {
    if (element instanceof IEditorInput) {

      IEditorInput input = (IEditorInput) element;
      IStorage storage = EditorUtils.getStorageFromInput(input);
      if (storage instanceof IFile) {
        IFile file = (IFile) storage;
        try {
          refreshFile(file);
        } catch (CoreException x) {
          log.warn("Can't refresh file", x);
        }

        IDocument d;
        IStatus s = null;

        try {
          d = createDocument(element);
        } catch (CoreException x) {
          log.warn("Can't create document", x);
          s = x.getStatus();
          d = createEmptyDocument();
        }

        // Set the initial line delimiter
        String initialLineDelimiter = GeneralUtils.getDefaultLineSeparator();
        if (initialLineDelimiter != null) {
          ((IDocumentExtension4) d).setInitialLineDelimiter(initialLineDelimiter);
        }

        IAnnotationModel m = createAnnotationModel(element);
        FileSynchronizer f = new FileSynchronizer(input);
        f.install();

        FileInfo info = new FileInfo(d, m, f);
        info.modificationStamp = computeModificationStamp(file);
        info.fStatus = s;

        return info;
      }
    }

    return super.createElementInfo(element);
  }