Java Code Examples for com.intellij.openapi.fileChooser.FileChooserDialog#choose()

The following examples show how to use com.intellij.openapi.fileChooser.FileChooserDialog#choose() . 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: PluginSettingTable.java    From ycy-intellij-plugin with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 添加表格行
 */
@Override
public void addRow() {
    FileChooserDescriptor descriptor = PluginSettingConfig.IMAGE_FILE_CHOOSER;
    FileChooserDialog dialog = FileChooserFactory.getInstance().createFileChooser(descriptor, null, null);
    VirtualFile[] files = dialog.choose(null);
    List<String> chosenImageUrlList = Stream.of(files)
        .map(imageFile -> {
            try {
                return VfsUtil.toUri(imageFile).toURL().toString();
            } catch (MalformedURLException e) {
                LOG.error("parse the image \"" + imageFile.getName() + "\" to URL error", e);
                return null;
            }
        })
        .filter(Objects::nonNull)
        .filter(imageUrl -> !imageUrlList.contains(imageUrl))
        .collect(Collectors.toList());
    if (chosenImageUrlList.size() != 0) {
        imageUrlList.addAll(chosenImageUrlList);
        LOG.info("add rows: " + chosenImageUrlList);
        super.fireTableRowsInserted(imageUrlList.size() - 1 - files.length, imageUrlList.size() - 1);
    } else {
        LOG.info("choose no files");
    }
}
 
Example 2
Source File: ExportRunConfigurationDialog.java    From intellij with Apache License 2.0 6 votes vote down vote up
private void chooseDirectory() {
  FileChooserDescriptor descriptor =
      FileChooserDescriptorFactory.createSingleFolderDescriptor()
          .withTitle("Export Directory Location")
          .withDescription("Choose directory to export run configurations to")
          .withHideIgnored(false);
  FileChooserDialog chooser =
      FileChooserFactory.getInstance().createFileChooser(descriptor, null, null);

  final VirtualFile[] files;
  File existingLocation = new File(getOutputDirectoryPath());
  if (existingLocation.exists()) {
    VirtualFile toSelect =
        LocalFileSystem.getInstance().refreshAndFindFileByPath(existingLocation.getPath());
    files = chooser.choose(null, toSelect);
  } else {
    files = chooser.choose(null);
  }
  if (files.length == 0) {
    return;
  }
  VirtualFile file = files[0];
  outputDirectoryPanel.setText(file.getPath());
}
 
Example 3
Source File: CopyExternalProjectViewOption.java    From intellij with Apache License 2.0 5 votes vote down vote up
private void chooseWorkspacePath() {
  FileChooserDescriptor descriptor =
      new FileChooserDescriptor(true, false, false, false, false, false)
          .withShowHiddenFiles(true) // Show root project view file
          .withHideIgnored(false)
          .withTitle("Select Project View File")
          .withDescription("Select a project view file to import.")
          .withFileFilter(
              virtualFile ->
                  ProjectViewStorageManager.isProjectViewFile(new File(virtualFile.getPath())));
  // File filters are broken for the native Mac file chooser.
  descriptor.setForcedToUseIdeaFileChooser(true);
  FileChooserDialog chooser =
      FileChooserFactory.getInstance().createFileChooser(descriptor, null, null);

  File startingLocation = null;
  String projectViewPath = getProjectViewPath();
  if (!projectViewPath.isEmpty()) {
    File fileLocation = new File(projectViewPath);
    if (fileLocation.exists()) {
      startingLocation = fileLocation;
    }
  }
  final VirtualFile[] files;
  if (startingLocation != null) {
    VirtualFile toSelect =
        LocalFileSystem.getInstance().refreshAndFindFileByPath(startingLocation.getPath());
    files = chooser.choose(null, toSelect);
  } else {
    files = chooser.choose(null);
  }
  if (files.length == 0) {
    return;
  }
  VirtualFile file = files[0];
  projectViewPathField.setText(file.getPath());
}
 
Example 4
Source File: ImportFromWorkspaceProjectViewOption.java    From intellij with Apache License 2.0 4 votes vote down vote up
private void chooseWorkspacePath() {
  FileChooserDescriptor descriptor =
      new FileChooserDescriptor(true, false, false, false, false, false)
          .withShowHiddenFiles(true) // Show root project view file
          .withHideIgnored(false)
          .withTitle("Select Project View File")
          .withDescription("Select a project view file to import.")
          .withFileFilter(
              virtualFile ->
                  ProjectViewStorageManager.isProjectViewFile(new File(virtualFile.getPath())));
  // File filters are broken for the native Mac file chooser.
  descriptor.setForcedToUseIdeaFileChooser(true);
  FileChooserDialog chooser =
      FileChooserFactory.getInstance().createFileChooser(descriptor, null, null);

  WorkspacePathResolver workspacePathResolver =
      builder.getWorkspaceData().workspacePathResolver();
  File fileBrowserRoot = builder.getWorkspaceData().fileBrowserRoot();
  File startingLocation = fileBrowserRoot;
  String projectViewPath = getProjectViewPath();
  if (!projectViewPath.isEmpty()) {
    // If the user has typed part of the path then clicked the '...', try to start from the
    // partial state
    projectViewPath = StringUtil.trimEnd(projectViewPath, '/');
    if (WorkspacePath.isValid(projectViewPath)) {
      File fileLocation = workspacePathResolver.resolveToFile(new WorkspacePath(projectViewPath));
      if (fileLocation.exists() && FileUtil.isAncestor(fileBrowserRoot, fileLocation, true)) {
        startingLocation = fileLocation;
      }
    }
  }
  VirtualFile toSelect =
      LocalFileSystem.getInstance().refreshAndFindFileByPath(startingLocation.getPath());
  VirtualFile[] files = chooser.choose(null, toSelect);
  if (files.length == 0) {
    return;
  }
  VirtualFile file = files[0];

  if (!FileUtil.startsWith(file.getPath(), fileBrowserRoot.getPath())) {
    Messages.showErrorDialog(
        String.format(
            "You must choose a project view file under %s. "
                + "To use an external project view, please use the 'Copy external' option.",
            fileBrowserRoot.getPath()),
        "Cannot Use Project View File");
    return;
  }

  String newWorkspacePath = FileUtil.getRelativePath(fileBrowserRoot, new File(file.getPath()));
  projectViewPathField.setText(newWorkspacePath);
}