Java Code Examples for com.intellij.openapi.vfs.VirtualFile#isWritable()

The following examples show how to use com.intellij.openapi.vfs.VirtualFile#isWritable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OclProjectJdkWizardStep.java    From reasonml-idea-plugin with MIT License 6 votes vote down vote up
@Override
public boolean validate() throws ConfigurationException {
    VirtualFileSystem fileSystem = LocalFileSystem.getInstance();

    VirtualFile sdkHome = fileSystem.findFileByPath(c_sdkHome.getText().trim());
    if (sdkHome == null) {
        throw new ConfigurationException("Can't find sdk home directory, make sure it exists");
    }
    if (!sdkHome.isDirectory()) {
        throw new ConfigurationException("Sdk home is not a directory");
    }
    if (!sdkHome.isWritable()) {
        throw new ConfigurationException("sdk home is not writable");
    }

    return super.validate();
}
 
Example 2
Source File: ReadOnlyAttributeUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * Sets specified read-only status for the spcified <code>file</code>.
 * This method can be performed only for files which are in local file system.
 *
 * @param file           file which read-only attribute to be changed.
 * @param readOnlyStatus new read-only status.
 * @throws java.lang.IllegalArgumentException
 *                     if passed <code>file</code> doesn't
 *                     belong to the local file system.
 * @throws IOException if some <code>IOException</code> occurred.
 */
public static void setReadOnlyAttribute(VirtualFile file, boolean readOnlyStatus) throws IOException {
  if (file.getFileSystem().isReadOnly()) {
    throw new IllegalArgumentException("Wrong file system: " + file.getFileSystem());
  }

  if (file.isWritable() == !readOnlyStatus) {
    return;
  }

  if (file instanceof NewVirtualFile) {
    ((NewVirtualFile)file).setWritable(!readOnlyStatus);
  }
  else {
    String path = file.getPresentableUrl();
    setReadOnlyAttribute(path, readOnlyStatus);
    file.refresh(false, false);
  }
}
 
Example 3
Source File: EncodingUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
static void saveIn(@Nonnull final Document document, final Editor editor, @Nonnull final VirtualFile virtualFile, @Nonnull final Charset charset) {
  FileDocumentManager documentManager = FileDocumentManager.getInstance();
  documentManager.saveDocument(document);
  final Project project = ProjectLocator.getInstance().guessProjectForFile(virtualFile);
  boolean writable = project == null ? virtualFile.isWritable() : ReadonlyStatusHandler.ensureFilesWritable(project, virtualFile);
  if (!writable) {
    CommonRefactoringUtil.showErrorHint(project, editor, "Cannot save the file " + virtualFile.getPresentableUrl(), "Unable to Save", null);
    return;
  }

  EncodingProjectManagerImpl.suppressReloadDuring(() -> {
    EncodingManager.getInstance().setEncoding(virtualFile, charset);
    try {
      ApplicationManager.getApplication().runWriteAction((ThrowableComputable<Object, IOException>)() -> {
        virtualFile.setCharset(charset);
        LoadTextUtil.write(project, virtualFile, virtualFile, document.getText(), document.getModificationStamp());
        return null;
      });
    }
    catch (IOException io) {
      Messages.showErrorDialog(project, io.getMessage(), "Error Writing File");
    }
  });
}
 
Example 4
Source File: UndoRedo.java    From consulo with Apache License 2.0 6 votes vote down vote up
private Collection<VirtualFile> collectReadOnlyAffectedFiles() {
  Collection<VirtualFile> readOnlyFiles = new ArrayList<>();
  for (UndoableAction action : myUndoableGroup.getActions()) {
    if (action instanceof MentionOnlyUndoableAction) continue;

    DocumentReference[] refs = action.getAffectedDocuments();
    if (refs == null) continue;

    for (DocumentReference ref : refs) {
      VirtualFile file = ref.getFile();
      if ((file != null) && file.isValid() && !file.isWritable()) {
        readOnlyFiles.add(file);
      }
    }
  }
  return readOnlyFiles;
}
 
Example 5
Source File: ToggleReadOnlyAttributeAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
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");
    }
  }

}
 
Example 6
Source File: ReplaceInProjectManager.java    From consulo with Apache License 2.0 6 votes vote down vote up
private boolean ensureUsagesWritable(ReplaceContext replaceContext, Collection<? extends 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<>();
      readOnlyFiles.add(file);
    }
  }

  if (readOnlyFiles != null) {
    ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(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;
}
 
Example 7
Source File: AbstractPsiBasedNode.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
public static Image patchIcon(@Nonnull Project project, @Nullable Image original, @Nullable VirtualFile file) {
  if (file == null || original == null) return original;

  IconDescriptor iconDescriptor = new IconDescriptor(original);

  final Bookmark bookmarkAtFile = BookmarkManager.getInstance(project).findFileBookmark(file);
  if (bookmarkAtFile != null) {
    iconDescriptor.setRightIcon(bookmarkAtFile.getIcon());
  }

  if (!file.isWritable()) {
    iconDescriptor.addLayerIcon(AllIcons.Nodes.Locked);
  }

  if (file.is(VFileProperty.SYMLINK)) {
    iconDescriptor.addLayerIcon(AllIcons.Nodes.Symlink);
  }

  return iconDescriptor.toIcon();
}
 
Example 8
Source File: TwosideBinaryDiffViewer.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void update(AnActionEvent e) {
  VirtualFile baseFile = getContentFile(myBaseSide);
  VirtualFile targetFile = getContentFile(myBaseSide.other());

  boolean enabled = baseFile != null && targetFile != null && targetFile.isWritable();
  e.getPresentation().setEnabledAndVisible(enabled);
}
 
Example 9
Source File: DiffUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean canMakeWritable(@Nonnull Document document) {
  if (document.isWritable()) {
    return true;
  }
  VirtualFile file = FileDocumentManager.getInstance().getFile(document);
  if (file != null && file.isValid() && file.isInLocalFileSystem()) {
    // decompiled file can be writable, but Document with decompiled content is still read-only
    return !file.isWritable();
  }
  return false;
}
 
Example 10
Source File: ToggleReadOnlyAttributePanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public Image getIcon() {
  if (!isReadonlyApplicable()) {
    return null;
  }
  VirtualFile virtualFile = getCurrentFile();
  return virtualFile == null || virtualFile.isWritable() ? AllIcons.Ide.Readwrite : AllIcons.Ide.Readonly;
}
 
Example 11
Source File: ToggleReadOnlyAttributePanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public String getTooltipText() {
  VirtualFile virtualFile = getCurrentFile();
  int writable = virtualFile == null || virtualFile.isWritable() ? 1 : 0;
  int readonly = writable == 1 ? 0 : 1;
  return ActionsBundle.message("action.ToggleReadOnlyAttribute.files", readonly, writable, 1, 0);
}
 
Example 12
Source File: ReadonlyStatusHandlerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
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(VfsUtilCore.toVirtualFileArray(readOnlyFiles));
}
 
Example 13
Source File: ReadonlyStatusHandlerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
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()]);
}
 
Example 14
Source File: AbstractConvertLineSeparatorsAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean shouldProcess(@Nonnull VirtualFile file, @Nonnull Project project) {
  if (file.isDirectory() ||
      !file.isWritable() ||
      FileTypeRegistry.getInstance().isFileIgnored(file) ||
      file.getFileType().isBinary() ||
      file.equals(project.getProjectFile()) ||
      file.equals(project.getWorkspaceFile())) {
    return false;
  }
  return true;
}
 
Example 15
Source File: TfsFileUtil.java    From azure-devops-intellij with MIT License 4 votes vote down vote up
public static boolean isFileWritable(FilePath localPath) {
    VirtualFile file = localPath.getVirtualFile();
    return file.isWritable() && !file.isDirectory();
}