public OperationStatus ensureFilesWritable(@NotNull VirtualFile... files) {
    if (files.length == 0) {
      return new OperationStatusImpl(VirtualFile.EMPTY_ARRAY);
    }
    ApplicationManager.getApplication().assertIsDispatchThread();

    Set<VirtualFile> realFiles = new THashSet<VirtualFile>(files.length);
    for (VirtualFile file : files) {
      if (file instanceof VirtualFileWindow) file = ((VirtualFileWindow) file).getDelegate();
      if (file != null) {
        realFiles.add(file);
      }
    }
    files = VfsUtil.toVirtualFileArray(realFiles);

    for (WritingAccessProvider accessProvider : myAccessProviders) {
      Collection<VirtualFile> denied = accessProvider.requestWriting(files);
      if (!denied.isEmpty()) {
        return new OperationStatusImpl(VfsUtil.toVirtualFileArray(denied));
      }
    }

    final FileInfo[] fileInfos = createFileInfos(files);
    if (fileInfos.length == 0) { // if all files are already writable
      return createResultStatus(files);
    }

    if (ApplicationManager.getApplication().isUnitTestMode()) {
      return createResultStatus(files);
    }

    // This event count hack is necessary to allow actions that called this stuff could still get
    // data from their data contexts.
    // Otherwise data manager stuff will fire up an assertion saying that event count has been
    // changed (due to modal dialog show-up)
    // The hack itself is safe since we guarantee that focus will return to the same component had
    // it before modal dialog have been shown.
    final int savedEventCount = IdeEventQueue.getInstance().getEventCount();
    if (myState.SHOW_DIALOG) {
      new ReadOnlyStatusDialog(myProject, fileInfos).show();
    } else {
      processFiles(
          new ArrayList<FileInfo>(Arrays.asList(fileInfos)),
          null); // the collection passed is modified
    }
    IdeEventQueue.getInstance().setEventCount(savedEventCount);
    return createResultStatus(files);
  }
Ejemplo n.º 2
0
  @Override
  public void recalculateOutputDirs() {
    final Module[] allModules = ModuleManager.getInstance(myProject).getModules();

    final Set<VirtualFile> allDirs = new OrderedSet<VirtualFile>();
    final Set<VirtualFile> testOutputDirs = new java.util.HashSet<VirtualFile>();
    final Set<VirtualFile> productionOutputDirs = new java.util.HashSet<VirtualFile>();

    CompilerPathsManager pathsManager = CompilerPathsManager.getInstance(getProject());
    for (Module module : allModules) {
      final VirtualFile output =
          pathsManager.getCompilerOutput(module, ProductionContentFolderTypeProvider.getInstance());
      if (output != null && output.isValid()) {
        allDirs.add(output);
        productionOutputDirs.add(output);
      }

      final VirtualFile testsOutput =
          pathsManager.getCompilerOutput(module, TestContentFolderTypeProvider.getInstance());
      if (testsOutput != null && testsOutput.isValid()) {
        allDirs.add(testsOutput);
        testOutputDirs.add(testsOutput);
      }
    }
    myOutputDirectories = VfsUtil.toVirtualFileArray(allDirs);
    // need this to ensure that the sent contains only _dedicated_ test output dirs
    // Directories that are configured for both test and production classes must not be added in the
    // resulting set
    testOutputDirs.removeAll(productionOutputDirs);
    myTestOutputDirectories = Collections.unmodifiableSet(testOutputDirs);
  }
Ejemplo n.º 3
0
  public boolean execute(boolean drop, boolean isInsideStartFinishGroup) {
    if (!myUndoableGroup.isUndoable()) {
      reportCannotUndo(
          CommonBundle.message("cannot.undo.error.contains.nonundoable.changes.message"),
          myUndoableGroup.getAffectedDocuments());
      return false;
    }

    Set<DocumentReference> clashing = getStackHolder().collectClashingActions(myUndoableGroup);
    if (!clashing.isEmpty()) {
      reportCannotUndo(
          CommonBundle.message("cannot.undo.error.other.affected.files.changed.message"), clashing);
      return false;
    }

    if (!isInsideStartFinishGroup && myUndoableGroup.shouldAskConfirmation(isRedo())) {
      if (!askUser()) return false;
    } else {
      if (restore(getBeforeState())) {
        setBeforeState(new EditorAndState(myEditor, myEditor.getState(FileEditorStateLevel.UNDO)));
        return true;
      }
    }

    Collection<VirtualFile> readOnlyFiles = collectReadOnlyAffectedFiles();
    if (!readOnlyFiles.isEmpty()) {
      final Project project = myManager.getProject();
      final VirtualFile[] files = VfsUtil.toVirtualFileArray(readOnlyFiles);

      if (project == null) {
        return false;
      }

      final ReadonlyStatusHandler.OperationStatus operationStatus =
          ReadonlyStatusHandler.getInstance(project).ensureFilesWritable(files);
      if (operationStatus.hasReadonlyFiles()) {
        return false;
      }
    }

    Collection<Document> readOnlyDocuments = collectReadOnlyDocuments();
    if (!readOnlyDocuments.isEmpty()) {
      for (Document document : readOnlyDocuments) {
        document.fireReadOnlyModificationAttempt();
      }
      return false;
    }

    getStackHolder().removeFromStacks(myUndoableGroup);
    if (!drop) {
      getReverseStackHolder().addToStacks(myUndoableGroup);
    }

    performAction();

    restore(getAfterState());

    return true;
  }
  private boolean checkReadonlyUsages() {
    final Set<VirtualFile> readOnlyUsages = getReadOnlyUsagesFiles();

    return readOnlyUsages.isEmpty()
        || !ReadonlyStatusHandler.getInstance(myProject)
            .ensureFilesWritable(VfsUtil.toVirtualFileArray(readOnlyUsages))
            .hasReadonlyFiles();
  }
 @NotNull
 private static VirtualFile[] filterByReadOnliness(@NotNull VirtualFile[] files) {
   List<VirtualFile> result = new ArrayList<VirtualFile>();
   for (VirtualFile file : files) {
     if (file.isInLocalFileSystem()) {
       result.add(file);
     }
   }
   return VfsUtil.toVirtualFileArray(result);
 }
 static VirtualFile[] getFiles(DataContext dataContext) {
   ArrayList<VirtualFile> filesList = new ArrayList<VirtualFile>();
   VirtualFile[] files = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext);
   for (int i = 0; files != null && i < files.length; i++) {
     VirtualFile file = files[i];
     if (file.isInLocalFileSystem()) {
       filesList.add(file);
     }
   }
   return VfsUtil.toVirtualFileArray(filesList);
 }
 private VirtualFile[] getVirtualFileArray() {
   ArrayList<VirtualFile> result = new ArrayList<VirtualFile>();
   TreePath[] selectionPaths = myTree.getSelectionPaths();
   if (selectionPaths != null) {
     for (TreePath selectionPath : selectionPaths) {
       AbstractTreeNode treeNode = (AbstractTreeNode) selectionPath.getLastPathComponent();
       result.addAll(treeNode.getVirtualFiles());
     }
   }
   return VfsUtil.toVirtualFileArray(result);
 }
  private static OperationStatus createResultStatus(final VirtualFile[] files) {
    List<VirtualFile> readOnlyFiles = new ArrayList<VirtualFile>();
    for (VirtualFile file : files) {
      if (file.exists()) {
        if (!file.isWritable()) {
          readOnlyFiles.add(file);
        }
      }
    }

    return new OperationStatusImpl(VfsUtil.toVirtualFileArray(readOnlyFiles));
  }
 @NotNull
 public VirtualFile[] getOpenFiles() {
   final ArrayListSet<VirtualFile> files = new ArrayListSet<VirtualFile>();
   for (final EditorWindow myWindow : myWindows) {
     final EditorWithProviderComposite[] editors = myWindow.getEditors();
     for (final EditorWithProviderComposite editor : editors) {
       final VirtualFile file = editor.getFile();
       LOG.assertTrue(file.isValid(), Arrays.toString(editor.getProviders()));
       files.add(file);
     }
   }
   return VfsUtil.toVirtualFileArray(files);
 }
 @Override
 protected VirtualFile[] computeFiles(PsiFile context, boolean compileTimeOnly) {
   final ArrayList<VirtualFile> files = new ArrayList<VirtualFile>();
   processIncludingFiles(
       context,
       new Processor<Pair<VirtualFile, FileIncludeInfo>>() {
         @Override
         public boolean process(
             Pair<VirtualFile, FileIncludeInfo> virtualFileFileIncludeInfoPair) {
           files.add(virtualFileFileIncludeInfoPair.first);
           return true;
         }
       });
   return VfsUtil.toVirtualFileArray(files);
 }
 @NotNull
 public VirtualFile[] getOpenFiles() {
   final ArrayListSet<VirtualFile> files = new ArrayListSet<VirtualFile>();
   for (final EditorWindow myWindow : myWindows) {
     final EditorWithProviderComposite[] editors = myWindow.getEditors();
     for (final EditorWithProviderComposite editor : editors) {
       VirtualFile file = editor.getFile();
       // background thread may call this method when invalid file is being removed
       // do not return it here as it will quietly drop out soon
       if (file.isValid()) {
         files.add(file);
       }
     }
   }
   return VfsUtil.toVirtualFileArray(files);
 }
Ejemplo n.º 12
0
 private VirtualFile[] doAdd() {
   VirtualFile baseDir = myAddBaseDir;
   Project project =
       PlatformDataKeys.PROJECT.getData(DataManager.getInstance().getDataContext(myPanel));
   if (baseDir == null && project != null) {
     baseDir = project.getBaseDir();
   }
   VirtualFile[] files = FileChooser.chooseFiles(myDescriptor, myPanel, project, baseDir);
   files = adjustAddedFileSet(myPanel, files);
   List<VirtualFile> added = new ArrayList<VirtualFile>(files.length);
   for (VirtualFile vFile : files) {
     if (addElement(vFile)) {
       added.add(vFile);
     }
   }
   return VfsUtil.toVirtualFileArray(added);
 }
 @Override
 protected VirtualFile[] computeFiles(final PsiFile file, final boolean compileTimeOnly) {
   final ArrayList<VirtualFile> files = new ArrayList<VirtualFile>();
   processIncludes(
       file,
       new Processor<FileIncludeInfo>() {
         @Override
         public boolean process(FileIncludeInfo info) {
           if (compileTimeOnly != info.runtimeOnly) {
             PsiFileSystemItem item = resolveFileInclude(info, file);
             if (item != null) {
               ContainerUtil.addIfNotNull(files, item.getVirtualFile());
             }
           }
           return true;
         }
       });
   return VfsUtil.toVirtualFileArray(files);
 }
 private static VirtualFile[] getFilesToCheckReadonlyStatus(
     GeneratingCompiler.GenerationItem[] items) {
   List<VirtualFile> filesToCheck = new ArrayList<VirtualFile>();
   for (GeneratingCompiler.GenerationItem item : items) {
     if (item instanceof AndroidAptCompiler.AptGenerationItem) {
       final Set<File> generatedFiles =
           ((AndroidAptCompiler.AptGenerationItem) item).getGeneratedFiles().keySet();
       for (File generatedFile : generatedFiles) {
         if (generatedFile.exists()) {
           VirtualFile generatedVFile =
               LocalFileSystem.getInstance().findFileByIoFile(generatedFile);
           if (generatedVFile != null) {
             filesToCheck.add(generatedVFile);
           }
         }
       }
     }
   }
   return VfsUtil.toVirtualFileArray(filesToCheck);
 }
  @Override
  public VirtualFile[] getFiles(OrderRootType rootType) {
    List<VirtualFile> result = new ArrayList<VirtualFile>();
    for (LightFilePointer pointer : myRoots.get(rootType)) {
      final VirtualFile file = pointer.getFile();
      if (file == null) {
        continue;
      }

      if (file.isDirectory()) {
        final String url = file.getUrl();
        if (myJarDirectories.contains(rootType, url)) {
          LibraryImpl.collectJarFiles(file, result, myJarDirectories.isRecursive(rootType, url));
          continue;
        }
      }
      result.add(file);
    }
    return VfsUtil.toVirtualFileArray(result);
  }
 @NotNull
 public VirtualFile[] getSelectedFiles() {
   final ArrayListSet<VirtualFile> files = new ArrayListSet<VirtualFile>();
   for (final EditorWindow window : myWindows) {
     final VirtualFile file = window.getSelectedFile();
     if (file != null) {
       files.add(file);
     }
   }
   final VirtualFile[] virtualFiles = VfsUtil.toVirtualFileArray(files);
   final VirtualFile currentFile = getCurrentFile();
   if (currentFile != null) {
     for (int i = 0; i != virtualFiles.length; ++i) {
       if (Comparing.equal(virtualFiles[i], currentFile)) {
         virtualFiles[i] = virtualFiles[0];
         virtualFiles[0] = currentFile;
         break;
       }
     }
   }
   return virtualFiles;
 }
  @Override
  public VirtualFile[] occursInPackagePrefixes(PsiPackage psiPackage) {
    List<VirtualFile> result = new ArrayList<VirtualFile>();
    final Module[] modules = ModuleManager.getInstance(psiPackage.getProject()).getModules();

    for (final Module module : modules) {
      for (final ContentEntry contentEntry :
          ModuleRootManager.getInstance(module).getContentEntries()) {
        final List<SourceFolder> sourceFolders =
            contentEntry.getSourceFolders(JavaModuleSourceRootTypes.SOURCES);
        for (final SourceFolder sourceFolder : sourceFolders) {
          final String packagePrefix = sourceFolder.getPackagePrefix();
          if (packagePrefix.startsWith(psiPackage.getQualifiedName())) {
            final VirtualFile file = sourceFolder.getFile();
            if (file != null) {
              result.add(file);
            }
          }
        }
      }
    }

    return VfsUtil.toVirtualFileArray(result);
  }
 public VirtualFile[] getAllFiles(VirtualFile file, boolean compileTimeOnly) {
   Set<VirtualFile> result = new HashSet<VirtualFile>();
   getFilesRecursively(file, compileTimeOnly, result);
   return VfsUtil.toVirtualFileArray(result);
 }