public boolean isContainerDirty(TypeNameMatch match) {
   ICompilationUnit cu = match.getType().getCompilationUnit();
   if (cu == null) {
     return false;
   }
   IResource resource = cu.getResource();
   ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
   ITextFileBuffer textFileBuffer =
       manager.getTextFileBuffer(resource.getFullPath(), LocationKind.IFILE);
   if (textFileBuffer != null) {
     return textFileBuffer.isDirty();
   }
   return false;
 }
 protected static void saveFileIfNeeded(IFile file, IProgressMonitor pm) throws CoreException {
   ITextFileBuffer buffer =
       FileBuffers.getTextFileBufferManager()
           .getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
   if (buffer != null
       && buffer.isDirty()
       && buffer.isStateValidated()
       && buffer.isSynchronized()) {
     pm.beginTask("", 2); // $NON-NLS-1$
     buffer.commit(new SubProgressMonitor(pm, 1), false);
     file.refreshLocal(IResource.DEPTH_ONE, new SubProgressMonitor(pm, 1));
   }
   pm.done();
 }
 private void checkDirtyFile(RefactoringStatus result, IFile file) {
   if (!file.exists()) return;
   ITextFileBuffer buffer =
       FileBuffers.getTextFileBufferManager()
           .getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
   if (buffer != null && buffer.isDirty()) {
     String message = RefactoringCoreMessages.DeleteResourcesProcessor_warning_unsaved_file;
     if (buffer.isStateValidated() && buffer.isSynchronized()) {
       result.addWarning(
           Messages.format(message, BasicElementLabels.getPathLabel(file.getFullPath(), false)));
     } else {
       result.addFatalError(
           Messages.format(message, BasicElementLabels.getPathLabel(file.getFullPath(), false)));
     }
   }
 }
Esempio n. 4
0
 private void initializeFile(IFile file) {
   fTextFileBuffer = getBuffer(file);
   if (fTextFileBuffer == null) {
     initializeResource(file);
   } else {
     IDocument document = fTextFileBuffer.getDocument();
     fDirty = fTextFileBuffer.isDirty();
     fReadOnly = isReadOnly(file);
     if (document instanceof IDocumentExtension4) {
       fKind = DOCUMENT;
       fModificationStamp = ((IDocumentExtension4) document).getModificationStamp();
     } else {
       fKind = RESOURCE;
       fModificationStamp = file.getModificationStamp();
     }
   }
 }
  private void doRunOnMultiple(ICompilationUnit[] cus, MultiStatus status, IProgressMonitor monitor)
      throws OperationCanceledException {
    if (monitor == null) {
      monitor = new NullProgressMonitor();
    }
    monitor.setTaskName("Organizing imports...");

    monitor.beginTask("", cus.length); // $NON-NLS-1$
    try {
      IChooseImportQuery query =
          new IChooseImportQuery() {
            public TypeNameMatch[] chooseImports(
                TypeNameMatch[][] openChoices, ISourceRange[] ranges) {
              throw new OrganizeImportError();
            }
          };
      IJavaProject lastProject = null;

      for (int i = 0; i < cus.length; i++) {
        ICompilationUnit cu = cus[i];
        if (testOnBuildPath(cu, status)) {
          if (lastProject == null || !lastProject.equals(cu.getJavaProject())) {
            lastProject = cu.getJavaProject();
          }
          CodeGenerationSettings settings =
              JavaPreferencesSettings.getCodeGenerationSettings(lastProject);

          String cuLocation = cu.getPath().makeRelative().toString();

          monitor.subTask(cuLocation);

          try {
            boolean save = !cu.isWorkingCopy();
            if (!save) {
              ITextFileBuffer textFileBuffer =
                  FileBuffers.getTextFileBufferManager().getTextFileBuffer(cu.getPath());
              save = textFileBuffer != null && !textFileBuffer.isDirty(); // save when not dirty
            }

            AJOrganizeImportsOperation op =
                new AJOrganizeImportsOperation(
                    cu, null, settings.importIgnoreLowercase, save, true, query);
            runInSync(op, cuLocation, status, monitor);

            IProblem parseError = op.getParseError();
            if (parseError != null) {
              String message =
                  Messages.format(
                      ActionMessages.OrganizeImportsAction_multi_error_parse, cuLocation);
              status.add(new Status(IStatus.INFO, JavaUI.ID_PLUGIN, IStatus.ERROR, message, null));
            }
          } catch (CoreException e) {
            JavaPlugin.log(e);
            String message =
                Messages.format(
                    "{0}: Unexpected error. See log for details.", e.getStatus().getMessage());
            status.add(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.ERROR, message, null));
          }

          if (monitor.isCanceled()) {
            throw new OperationCanceledException();
          }
        }
      }
    } finally {
      monitor.done();
    }
  }