Java Code Examples for com.intellij.openapi.fileChooser.FileChooser#FileChooserConsumer

The following examples show how to use com.intellij.openapi.fileChooser.FileChooser#FileChooserConsumer . 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: MacPathChooserDialog.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void choose(@Nullable VirtualFile toSelect, @Nonnull Consumer<List<VirtualFile>> callback) {
  if (toSelect != null && toSelect.getParent() != null) {

    String directoryName;
    String fileName = null;
    if (toSelect.isDirectory()) {
      directoryName = toSelect.getCanonicalPath();
    }
    else {
      directoryName = toSelect.getParent().getCanonicalPath();
      fileName = toSelect.getPath();
    }
    myFileDialog.setDirectory(directoryName);
    myFileDialog.setFile(fileName);
  }


  myFileDialog.setFilenameFilter((dir, name) -> {
    File file = new File(dir, name);
    return myFileChooserDescriptor.isFileSelectable(fileToVirtualFile(file));
  });

  myFileDialog.setMultipleMode(myFileChooserDescriptor.isChooseMultiple());

  final CommandProcessorEx commandProcessor = ApplicationManager.getApplication() != null ? (CommandProcessorEx)CommandProcessor.getInstance() : null;
  final boolean appStarted = commandProcessor != null;


  if (appStarted) {
    commandProcessor.enterModal();
    LaterInvocator.enterModal(myFileDialog);
  }

  Component parent = myParent.get();
  try {
    myFileDialog.setVisible(true);
  }
  finally {
    if (appStarted) {
      commandProcessor.leaveModal();
      LaterInvocator.leaveModal(myFileDialog);
      if (parent != null) {
        IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() -> {
          IdeFocusManager.getGlobalInstance().requestFocus(parent, true);
        });
      }
    }
  }

  File[] files = myFileDialog.getFiles();
  List<VirtualFile> virtualFileList = getChosenFiles(Stream.of(files));
  virtualFiles = virtualFileList.toArray(VirtualFile.EMPTY_ARRAY);

  if (!virtualFileList.isEmpty()) {
    try {
      if (virtualFileList.size() == 1) {
        myFileChooserDescriptor.isFileSelectable(virtualFileList.get(0));
      }
      myFileChooserDescriptor.validateSelectedFiles(virtualFiles);
    }
    catch (Exception e) {
      if (parent == null) {
        Messages.showErrorDialog(myProject, e.getMessage(), myTitle);
      }
      else {
        Messages.showErrorDialog(parent, e.getMessage(), myTitle);
      }

      return;
    }

    if (!ArrayUtil.isEmpty(files)) {
      callback.consume(virtualFileList);
      return;
    }
  }
  if (callback instanceof FileChooser.FileChooserConsumer) {
    ((FileChooser.FileChooserConsumer)callback).cancelled();
  }
}
 
Example 2
Source File: WinPathChooserDialog.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void choose(@Nullable VirtualFile toSelect, @Nonnull Consumer<List<VirtualFile>> callback) {
  if (toSelect != null && toSelect.getParent() != null) {

    String directoryName;
    String fileName = null;
    if (toSelect.isDirectory()) {
      directoryName = toSelect.getCanonicalPath();
    }
    else {
      directoryName = toSelect.getParent().getCanonicalPath();
      fileName = toSelect.getPath();
    }
    myFileDialog.setDirectory(directoryName);
    myFileDialog.setFile(fileName);
  }


  myFileDialog.setFilenameFilter((dir, name) -> {
    File file = new File(dir, name);
    return myFileChooserDescriptor.isFileSelectable(fileToVirtualFile(file));
  });

  myFileDialog.setMultipleMode(myFileChooserDescriptor.isChooseMultiple());

  final CommandProcessorEx commandProcessor = ApplicationManager.getApplication() != null ? (CommandProcessorEx)CommandProcessor.getInstance() : null;
  final boolean appStarted = commandProcessor != null;


  if (appStarted) {
    commandProcessor.enterModal();
    LaterInvocator.enterModal(myFileDialog);
  }

  Component parent = myParent.get();
  try {
    myFileDialog.setVisible(true);
  }
  finally {
    if (appStarted) {
      commandProcessor.leaveModal();
      LaterInvocator.leaveModal(myFileDialog);
      if (parent != null) parent.requestFocus();
    }
  }

  File[] files = myFileDialog.getFiles();
  List<VirtualFile> virtualFileList = getChosenFiles(Stream.of(files));
  virtualFiles = virtualFileList.toArray(VirtualFile.EMPTY_ARRAY);

  if (!virtualFileList.isEmpty()) {
    try {
      if (virtualFileList.size() == 1) {
        myFileChooserDescriptor.isFileSelectable(virtualFileList.get(0));
      }
      myFileChooserDescriptor.validateSelectedFiles(virtualFiles);
    }
    catch (Exception e) {
      if (parent == null) {
        Messages.showErrorDialog(myProject, e.getMessage(), myTitle);
      }
      else {
        Messages.showErrorDialog(parent, e.getMessage(), myTitle);
      }

      return;
    }

    if (!ArrayUtil.isEmpty(files)) {
      callback.consume(virtualFileList);
    }
    else if (callback instanceof FileChooser.FileChooserConsumer) {
      ((FileChooser.FileChooserConsumer)callback).cancelled();
    }
  }
}