Java Code Examples for com.intellij.openapi.vfs.VfsUtil#getUserHomeDir()

The following examples show how to use com.intellij.openapi.vfs.VfsUtil#getUserHomeDir() . 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 void _init() {
    for (String sdk : SDKS) {
        c_selDownload.addItem(sdk);
    }

    if (c_selExistingSdk.getItemCount() == 0) {
        c_rdSelectExisting.setEnabled(false);
        c_selExistingSdk.setEnabled(false);
        c_rdDownloadSdk.setSelected(true);
    }

    String value = PropertiesComponent.getInstance().getValue(SDK_HOME);
    if (value == null) {
        VirtualFile userHomeDir = VfsUtil.getUserHomeDir();
        value = userHomeDir == null ? "" : userHomeDir.getCanonicalPath() + "/odk";
    }
    c_sdkHome.setText(value);

    c_sdkHome.addBrowseFolderListener("Choose Sdk Home Directory: ", null, m_context.getProject(),
                                      FileChooserDescriptorFactory.createSingleFolderDescriptor());
}
 
Example 2
Source File: CSharpCopyClassHandlerDelegate.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Nullable
@RequiredReadAction
private static PsiDirectory tryNotNullizeDirectory(@Nonnull Project project, @Nullable PsiDirectory defaultTargetDirectory)
{
	if(defaultTargetDirectory == null)
	{
		VirtualFile root = ArrayUtil.getFirstElement(ProjectRootManager.getInstance(project).getContentRoots());
		if(root == null)
		{
			root = project.getBaseDir();
		}
		if(root == null)
		{
			root = VfsUtil.getUserHomeDir();
		}
		defaultTargetDirectory = root != null ? PsiManager.getInstance(project).findDirectory(root) : null;

		if(defaultTargetDirectory == null)
		{
			LOG.warn("No directory found for project: " + project.getName() + ", root: " + root);
		}
	}
	return defaultTargetDirectory;
}
 
Example 3
Source File: GotoHomeAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected void actionPerformed(final FileSystemTree fileSystemTree, final AnActionEvent e) {
  final VirtualFile userHomeDir = VfsUtil.getUserHomeDir();
  if (userHomeDir != null) {
    fileSystemTree.select(userHomeDir, new Runnable() {
      public void run() {
        fileSystemTree.expand(userHomeDir, null);
      }
    });
  }
}
 
Example 4
Source File: GotoHomeAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected void update(final FileSystemTree fileSystemTree, final AnActionEvent e) {
  final Presentation presentation = e.getPresentation();
  if (!presentation.isEnabled()) return;

  final VirtualFile userHomeDir = VfsUtil.getUserHomeDir();
  presentation.setEnabled(userHomeDir != null && fileSystemTree.isUnderRoots(userHomeDir));
}
 
Example 5
Source File: FileChooserUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static VirtualFile getFileToSelect(@Nonnull FileChooserDescriptor descriptor, @Nullable Project project,
                                          @Nullable VirtualFile toSelect, @Nullable VirtualFile lastPath) {
  boolean chooseDir = descriptor instanceof FileSaverDescriptor;
  VirtualFile result;

  if (toSelect == null && lastPath == null) {
    result = project == null? null : project.getBaseDir();
  }
  else if (toSelect != null && lastPath != null) {
    if (Boolean.TRUE.equals(descriptor.getUserData(PathChooserDialog.PREFER_LAST_OVER_EXPLICIT))) {
      result = lastPath;
    }
    else {
      result = toSelect;
    }
  }
  else if (toSelect == null) {
    result = lastPath;
  }
  else {
    result = toSelect;
  }

  if (result != null) {
    if (chooseDir && !result.isDirectory()) {
      result = result.getParent();
    }
  }
  else if (SystemInfo.isUnix) {
    result = VfsUtil.getUserHomeDir();
  }

  return result;
}
 
Example 6
Source File: CreateFileAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
public MkDirs(@Nonnull String newName, @Nonnull PsiDirectory directory) {
  if (SystemInfo.isWindows) {
    newName = newName.replace('\\', '/');
  }
  if (newName.contains("/")) {
    final List<String> subDirs = StringUtil.split(newName, "/");
    newName = subDirs.remove(subDirs.size() - 1);
    boolean firstToken = true;
    for (String dir : subDirs) {
      if (firstToken && "~".equals(dir)) {
        final VirtualFile userHomeDir = VfsUtil.getUserHomeDir();
        if (userHomeDir == null) throw new IncorrectOperationException("User home directory not found");
        final PsiDirectory directory1 = directory.getManager().findDirectory(userHomeDir);
        if (directory1 == null) throw new IncorrectOperationException("User home directory not found");
        directory = directory1;
      }
      else if ("..".equals(dir)) {
        final PsiDirectory parentDirectory = directory.getParentDirectory();
        if (parentDirectory == null) throw new IncorrectOperationException("Not a valid directory");
        directory = parentDirectory;
      }
      else if (!".".equals(dir)) {
        directory = findOrCreateSubdirectory(directory, dir);
      }
      firstToken = false;
    }
  }

  this.newName = newName;
  this.directory = directory;
}
 
Example 7
Source File: CopyFilesOrDirectoriesHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
@RequiredReadAction
private static PsiDirectory tryNotNullizeDirectory(@Nonnull Project project, @Nullable PsiDirectory defaultTargetDirectory) {
  if (defaultTargetDirectory == null) {
    VirtualFile root = ArrayUtil.getFirstElement(ProjectRootManager.getInstance(project).getContentRoots());
    if (root == null) root = project.getBaseDir();
    if (root == null) root = VfsUtil.getUserHomeDir();
    defaultTargetDirectory = root != null ? PsiManager.getInstance(project).findDirectory(root) : null;

    if (defaultTargetDirectory == null) {
      LOG.warn("No directory found for project: " + project.getName() + ", root: " + root);
    }
  }
  return defaultTargetDirectory;
}
 
Example 8
Source File: OuterIgnoreWrapper.java    From idea-gitignore with MIT License 4 votes vote down vote up
/** Constructor. */
@SuppressWarnings("unchecked")
public OuterIgnoreWrapper(@NotNull final Project project, @NotNull final IgnoreLanguage language,
                          @NotNull final List<VirtualFile> outerFiles) {
    this.outerFiles = outerFiles;
    settings = IgnoreSettings.getInstance();

    panel = new JPanel(new BorderLayout());
    panel.setBorder(BorderFactory.createEmptyBorder(0, 10, 5, 10));

    northPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 5));

    JBLabel label = new JBLabel(
            IgnoreBundle.message("outer.label"),
            UIUtil.ComponentStyle.REGULAR,
            UIUtil.FontColor.BRIGHTER
    );
    label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10));
    northPanel.add(label);

    tabbedPanel = new TabbedPaneWrapper(project);
    messageBus = project.getMessageBus().connect();
    messageBus.subscribe(UISettingsListener.TOPIC, uiSettings -> updateTabbedPanelPolicy());
    updateTabbedPanelPolicy();

    linkLabel = new LinkLabel(
            outerFiles.get(0).getPath(),
            null,
            (aSource, aLinkData) -> Utils.openFile(project, outerFiles.get(tabbedPanel.getSelectedIndex()))
    );
    final VirtualFile userHomeDir = VfsUtil.getUserHomeDir();

    for (final VirtualFile outerFile : outerFiles) {
        Document document = FileDocumentManager.getInstance().getDocument(outerFile);
        Editor outerEditor = document != null ? Utils.createPreviewEditor(document, null, true) : null;

        if (outerEditor != null) {
            final JScrollPane scrollPanel = ScrollPaneFactory.createScrollPane(outerEditor.getComponent());
            scrollPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
            scrollPanel.setPreferredSize(new Dimension(0, settings.getOuterIgnoreWrapperHeight()));

            String path = outerFile.getPath();
            if (userHomeDir != null) {
                path = path.replace(userHomeDir.getPath(), "~");
            }

            Icon icon = ((language instanceof GitLanguage) || (language instanceof GitExcludeLanguage))
                    ? AllIcons.Vcs.Ignore_file : language.getIcon();

            tabbedPanel.addTab(path, icon, scrollPanel, outerFile.getCanonicalPath());
            outerEditors.add(outerEditor);
        }
    }

    northPanel.addMouseListener(this);
    northPanel.addMouseMotionListener(this);
    tabbedPanel.addChangeListener(this);

    panel.add(northPanel, BorderLayout.NORTH);
    panel.add(tabbedPanel.getComponent(), BorderLayout.CENTER);
    panel.add(linkLabel, BorderLayout.SOUTH);
}
 
Example 9
Source File: CreateDirectoryOrPackageHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
@RequiredUIAccess
public boolean checkInput(String inputString) {
  final StringTokenizer tokenizer = new StringTokenizer(inputString, myDelimiters);
  VirtualFile vFile = myDirectory.getVirtualFile();
  boolean firstToken = true;
  while (tokenizer.hasMoreTokens()) {
    final String token = tokenizer.nextToken();
    if (!tokenizer.hasMoreTokens() && (token.equals(".") || token.equals(".."))) {
      myErrorText = "Can't create a directory with name '" + token + "'";
      return false;
    }
    if (vFile != null) {
      if (firstToken && "~".equals(token)) {
        final VirtualFile userHomeDir = VfsUtil.getUserHomeDir();
        if (userHomeDir == null) {
          myErrorText = "User home directory not found";
          return false;
        }
        vFile = userHomeDir;
      }
      else if ("..".equals(token)) {
        vFile = vFile.getParent();
        if (vFile == null) {
          myErrorText = "Not a valid directory";
          return false;
        }
      }
      else if (!".".equals(token)) {
        final VirtualFile child = vFile.findChild(token);
        if (child != null) {
          if (!child.isDirectory()) {
            myErrorText = "A file with name '" + token + "' already exists";
            return false;
          }
          else if (!tokenizer.hasMoreTokens()) {
            myErrorText = "A directory with name '" + token + "' already exists";
            return false;
          }
        }
        vFile = child;
      }
    }
    if (FileTypeManager.getInstance().isFileIgnored(token)) {
      myErrorText = "Trying to create a " + (myIsDirectory ? "directory" : "package") + " with an ignored name, the result will not be visible";
      return true;
    }
    if (!myIsDirectory && token.length() > 0 && !PsiPackageManager.getInstance(myDirectory.getProject()).isValidPackageName(myDirectory, token)) {
      myErrorText = "Not a valid package name, it will not be possible to create a class inside";
      return true;
    }
    firstToken = false;
  }
  myErrorText = null;
  return true;
}
 
Example 10
Source File: CreateFileAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
@RequiredUIAccess
@Override
public boolean checkInput(String inputString) {
  final StringTokenizer tokenizer = new StringTokenizer(inputString, "\\/");
  VirtualFile vFile = getDirectory().getVirtualFile();
  boolean firstToken = true;
  while (tokenizer.hasMoreTokens()) {
    final String token = tokenizer.nextToken();
    if ((token.equals(".") || token.equals("..")) && !tokenizer.hasMoreTokens()) {
      myErrorText = "Can't create file with name '" + token + "'";
      return false;
    }
    if (vFile != null) {
      if (firstToken && "~".equals(token)) {
        final VirtualFile userHomeDir = VfsUtil.getUserHomeDir();
        if (userHomeDir == null) {
          myErrorText = "User home directory not found";
          return false;
        }
        vFile = userHomeDir;
      }
      else if ("..".equals(token)) {
        vFile = vFile.getParent();
        if (vFile == null) {
          myErrorText = "Not a valid directory";
          return false;
        }
      }
      else if (!".".equals(token)) {
        final VirtualFile child = vFile.findChild(token);
        if (child != null) {
          if (!child.isDirectory()) {
            myErrorText = "A file with name '" + token + "' already exists";
            return false;
          }
          else if (!tokenizer.hasMoreTokens()) {
            myErrorText = "A directory with name '" + token + "' already exists";
            return false;
          }
        }
        vFile = child;
      }
    }
    if (FileTypeManager.getInstance().isFileIgnored(getFileName(token))) {
      myErrorText = "'" + token + "' is an ignored name (Settings | Editor | File Types | Ignore files and folders)";
      return true;
    }
    firstToken = false;
  }
  myErrorText = null;
  return true;
}