private boolean ensureUsagesWritable(
      ReplaceContext replaceContext, Collection<Usage> selectedUsages) {
    Set<VirtualFile> readOnlyFiles = null;
    for (final Usage usage : selectedUsages) {
      final VirtualFile file = ((UsageInFile) usage).getFile();

      if (file != null && !file.isWritable()) {
        if (readOnlyFiles == null) readOnlyFiles = new HashSet<VirtualFile>();
        readOnlyFiles.add(file);
      }
    }

    if (readOnlyFiles != null) {
      ReadonlyStatusHandler.getInstance(myProject)
          .ensureFilesWritable(VfsUtilCore.toVirtualFileArray(readOnlyFiles));
    }

    if (hasReadOnlyUsages(selectedUsages)) {
      int result =
          Messages.showOkCancelDialog(
              replaceContext.getUsageView().getComponent(),
              FindBundle.message("find.replace.occurrences.in.read.only.files.prompt"),
              FindBundle.message("find.replace.occurrences.in.read.only.files.title"),
              Messages.getWarningIcon());
      if (result != Messages.OK) {
        return false;
      }
    }
    return true;
  }
 @NotNull
 public Icon getIcon() {
   VirtualFile virtualFile = getCurrentFile();
   return virtualFile == null || virtualFile.isWritable()
       ? AllIcons.Ide.Readwrite
       : AllIcons.Ide.Readonly;
 }
 private FileInfo[] createFileInfos(VirtualFile[] files) {
   List<FileInfo> fileInfos = new ArrayList<FileInfo>();
   for (final VirtualFile file : files) {
     if (file != null && !file.isWritable() && file.isInLocalFileSystem()) {
       fileInfos.add(new FileInfo(file, myProject));
     }
   }
   return fileInfos.toArray(new FileInfo[fileInfos.size()]);
 }
  @Override
  @Nullable
  public Document getDocument(@NotNull final VirtualFile file) {
    DocumentEx document = (DocumentEx) getCachedDocument(file);
    if (document == null) {
      if (file.isDirectory()
          || isBinaryWithoutDecompiler(file)
          || SingleRootFileViewProvider.isTooLargeForContentLoading(file)) {
        return null;
      }
      final CharSequence text = LoadTextUtil.loadText(file);

      synchronized (lock) {
        document = (DocumentEx) getCachedDocument(file);
        if (document != null) return document; // Double checking

        document = (DocumentEx) createDocument(text);
        document.setModificationStamp(file.getModificationStamp());
        final FileType fileType = file.getFileType();
        document.setReadOnly(!file.isWritable() || fileType.isBinary());
        file.putUserData(DOCUMENT_KEY, new WeakReference<Document>(document));
        document.putUserData(FILE_KEY, file);

        if (!(file instanceof LightVirtualFile
            || file.getFileSystem() instanceof DummyFileSystem)) {
          document.addDocumentListener(
              new DocumentAdapter() {
                @Override
                public void documentChanged(DocumentEvent e) {
                  final Document document = e.getDocument();
                  myUnsavedDocuments.add(document);
                  final Runnable currentCommand =
                      CommandProcessor.getInstance().getCurrentCommand();
                  Project project =
                      currentCommand == null
                          ? null
                          : CommandProcessor.getInstance().getCurrentCommandProject();
                  String lineSeparator = CodeStyleFacade.getInstance(project).getLineSeparator();
                  document.putUserData(LINE_SEPARATOR_KEY, lineSeparator);

                  // avoid documents piling up during batch processing
                  if (areTooManyDocumentsInTheQueue(myUnsavedDocuments)) {
                    saveAllDocumentsLater();
                  }
                }
              });
        }
      }

      myMultiCaster.fileContentLoaded(file, document);
    }

    return document;
  }
 private Collection<VirtualFile> collectReadOnlyAffectedFiles() {
   Collection<DocumentReference> affectedDocument = myUndoableGroup.getAffectedDocuments();
   Collection<VirtualFile> readOnlyFiles = new ArrayList<VirtualFile>();
   for (DocumentReference documentReference : affectedDocument) {
     VirtualFile file = documentReference.getFile();
     if ((file != null) && file.isValid() && !file.isWritable()) {
       readOnlyFiles.add(file);
     }
   }
   return readOnlyFiles;
 }
  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 static ManifestFileConfiguration createManifestFileConfiguration(
     @NotNull VirtualFile manifestFile) {
   final String path = manifestFile.getPath();
   Manifest manifest = readManifest(manifestFile);
   String mainClass = manifest.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
   final String classpathText = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
   final List<String> classpath = new ArrayList<String>();
   if (classpathText != null) {
     classpath.addAll(StringUtil.split(classpathText, " "));
   }
   return new ManifestFileConfiguration(path, classpath, mainClass, manifestFile.isWritable());
 }
 /** Fills readOnlyFiles with VirtualFiles */
 private static void getReadOnlyVirtualFiles(
     VirtualFile file, ArrayList<VirtualFile> readOnlyFiles, final FileTypeManager ftManager) {
   if (ftManager.isFileIgnored(file)) return;
   if (!file.isWritable()) {
     readOnlyFiles.add(file);
   }
   if (file.isDirectory()) {
     VirtualFile[] children = file.getChildren();
     for (VirtualFile child : children) {
       getReadOnlyVirtualFiles(child, readOnlyFiles, ftManager);
     }
   }
 }
 @Override
 public boolean isWritableStepLikeFile(@NotNull PsiElement child, @NotNull PsiElement parent) {
   if (child instanceof PsiJavaFile) {
     final PsiFile file = child.getContainingFile();
     if (file != null) {
       final VirtualFile virtualFile = file.getVirtualFile();
       if (virtualFile != null) {
         return virtualFile.isWritable();
       }
     }
   }
   return false;
 }
  private void doReplaceSelected(final ReplaceContext replaceContext) {
    final Set<Usage> selectedUsages = replaceContext.getUsageView().getSelectedUsages();
    if (selectedUsages == null) {
      return;
    }

    Set<VirtualFile> readOnlyFiles = null;
    for (final Usage usage : selectedUsages) {
      final VirtualFile file = ((UsageInFile) usage).getFile();

      if (file != null && !file.isWritable()) {
        if (readOnlyFiles == null) readOnlyFiles = new HashSet<VirtualFile>();
        readOnlyFiles.add(file);
      }
    }

    if (readOnlyFiles != null) {
      ReadonlyStatusHandler.getInstance(myProject)
          .ensureFilesWritable(VfsUtilCore.toVirtualFileArray(readOnlyFiles));
    }

    if (hasReadOnlyUsages(selectedUsages)) {
      int result =
          Messages.showOkCancelDialog(
              replaceContext.getUsageView().getComponent(),
              FindBundle.message("find.replace.occurrences.in.read.only.files.prompt"),
              FindBundle.message("find.replace.occurrences.in.read.only.files.title"),
              Messages.getWarningIcon());
      if (result != 0) {
        return;
      }
    }

    CommandProcessor.getInstance()
        .executeCommand(
            myProject,
            new Runnable() {
              @Override
              public void run() {
                final boolean success = doReplace(replaceContext, selectedUsages);
                final UsageView usageView = replaceContext.getUsageView();

                if (closeUsageViewIfEmpty(usageView, success)) return;
                usageView.getComponent().requestFocus();
              }
            },
            FindBundle.message("find.replace.command"),
            null);
  }
  private void restoreCopy(VirtualFile file) {
    try {
      if (file == null) return; // Externally deleted actually.
      if (!file.isWritable()) return; // IDEA was unable to save it as well. So no need to restore.

      final byte[] bytes = mySavedCopies.get(file);
      if (bytes != null) {
        try {
          file.setBinaryContent(bytes, -1, mySavedTimestamps.get(file));
        } catch (IOException e) {
          Messages.showWarningDialog(
              ProjectBundle.message("project.reload.write.failed", file.getPresentableUrl()),
              ProjectBundle.message("project.reload.write.failed.title"));
        }
      }
    } finally {
      mySavedCopies.remove(file);
      mySavedTimestamps.remove(file);
    }
  }
 public void update(AnActionEvent e) {
   VirtualFile[] files = getFiles(e.getDataContext());
   e.getPresentation().setEnabled(files.length > 0);
   if (files.length > 0) {
     boolean allReadOnly = true;
     boolean allWritable = true;
     for (VirtualFile file : files) {
       if (file.isWritable()) {
         allReadOnly = false;
       } else {
         allWritable = false;
       }
     }
     if (allReadOnly) {
       e.getPresentation()
           .setText(files.length > 1 ? "Make Files Writable" : "Make File Writable");
     } else if (allWritable) {
       e.getPresentation()
           .setText(files.length > 1 ? "Make Files Read-only" : "Make File Read-only");
     } else {
       e.getPresentation().setText("Toggle Read-only Attribute");
     }
   }
 }
        @Override
        public Icon fun(final FileIconKey key) {
          final VirtualFile file = key.getFile();
          final int flags = key.getFlags();
          final Project project = key.getProject();

          if (!file.isValid()
              || project != null && (project.isDisposed() || !wasEverInitialized(project)))
            return null;

          final Icon providersIcon = getProvidersIcon(file, flags, project);
          Icon icon =
              providersIcon == null ? VirtualFilePresentation.getIconImpl(file) : providersIcon;

          final boolean dumb = project != null && DumbService.getInstance(project).isDumb();
          for (FileIconPatcher patcher : getPatchers()) {
            if (dumb && !DumbService.isDumbAware(patcher)) {
              continue;
            }

            icon = patcher.patchIcon(icon, file, flags, project);
          }

          if ((flags & Iconable.ICON_FLAG_READ_STATUS) != 0
              && (!file.isWritable()
                  || !WritingAccessProvider.isPotentiallyWritable(file, project))) {
            icon = new LayeredIcon(icon, PlatformIcons.LOCKED_ICON);
          }
          if (file.is(VFileProperty.SYMLINK)) {
            icon = new LayeredIcon(icon, PlatformIcons.SYMLINK_ICON);
          }

          Iconable.LastComputedIcon.put(file, icon, flags);

          return icon;
        }