Пример #1
0
  /**
   * Shows a file-open selection dialog for the given working directory.
   *
   * @param aOwner the owning window to show the dialog in;
   * @param aCurrentDirectory the working directory to start the dialog in, can be <code>null</code>
   *     .
   * @return the selected file, or <code>null</code> if the user aborted the dialog.
   */
  public static final File showFileOpenDialog(
      final Window aOwner,
      final String aCurrentDirectory,
      final javax.swing.filechooser.FileFilter... aFileFilters) {
    if (HostUtils.isMacOS()) {
      final FileDialog dialog;
      if (aOwner instanceof Dialog) {
        dialog = new FileDialog((Dialog) aOwner, "Open file", FileDialog.LOAD);
      } else {
        dialog = new FileDialog((Frame) aOwner, "Open file", FileDialog.LOAD);
      }
      dialog.setDirectory(aCurrentDirectory);

      if ((aFileFilters != null) && (aFileFilters.length > 0)) {
        dialog.setFilenameFilter(new FilenameFilterAdapter(aFileFilters));
      }

      try {
        dialog.setVisible(true);
        final String selectedFile = dialog.getFile();
        return selectedFile == null ? null : new File(dialog.getDirectory(), selectedFile);
      } finally {
        dialog.dispose();
      }
    } else {
      final JFileChooser dialog = new JFileChooser();
      dialog.setCurrentDirectory((aCurrentDirectory == null) ? null : new File(aCurrentDirectory));

      for (javax.swing.filechooser.FileFilter filter : aFileFilters) {
        dialog.addChoosableFileFilter(filter);
      }

      File result = null;
      if (dialog.showOpenDialog(aOwner) == JFileChooser.APPROVE_OPTION) {
        result = dialog.getSelectedFile();
      }

      return result;
    }
  }