コード例 #1
0
 @Override
 public void approveSelection() {
   File f = getSelectedFile();
   // If the file exists, ask user if they want to overwrite
   if (f.exists() && getDialogType() == SAVE_DIALOG) {
     int result =
         JOptionPane.showConfirmDialog(
             this,
             "The file exists, overwrite?",
             "Existing file",
             JOptionPane.YES_NO_CANCEL_OPTION);
     switch (result) {
       case JOptionPane.YES_OPTION:
         super.approveSelection();
         return;
       case JOptionPane.CANCEL_OPTION:
         cancelSelection();
         return;
       default:
         return;
     }
   }
   super.approveSelection();
 }
コード例 #2
0
ファイル: Plotter.java プロジェクト: susotajuraj/jdk8u-jdk
    @Override
    public void approveSelection() {
      File file = getSelectedFile();
      if (file != null) {
        FileFilter filter = getFileFilter();
        if (filter != null && filter instanceof FileNameExtensionFilter) {
          String[] extensions = ((FileNameExtensionFilter) filter).getExtensions();

          boolean goodExt = false;
          for (String ext : extensions) {
            if (file.getName().toLowerCase().endsWith("." + ext.toLowerCase())) {
              goodExt = true;
              break;
            }
          }
          if (!goodExt) {
            file = new File(file.getParent(), file.getName() + "." + extensions[0]);
          }
        }

        if (file.exists()) {
          String okStr = Messages.FILE_CHOOSER_FILE_EXISTS_OK_OPTION;
          String cancelStr = Messages.FILE_CHOOSER_FILE_EXISTS_CANCEL_OPTION;
          int ret =
              JOptionPane.showOptionDialog(
                  this,
                  Resources.format(Messages.FILE_CHOOSER_FILE_EXISTS_MESSAGE, file.getName()),
                  Messages.FILE_CHOOSER_FILE_EXISTS_TITLE,
                  JOptionPane.OK_CANCEL_OPTION,
                  JOptionPane.WARNING_MESSAGE,
                  null,
                  new Object[] {okStr, cancelStr},
                  okStr);
          if (ret != JOptionPane.OK_OPTION) {
            return;
          }
        }
        setSelectedFile(file);
      }
      super.approveSelection();
    }
コード例 #3
0
    public void actionPerformed(ActionEvent e) {
      if (isDirectorySelected()) {
        File dir = getDirectory();
        if (dir != null) {
          try {
            // Strip trailing ".."
            dir = ShellFolder.getNormalizedFile(dir);
          } catch (IOException ex) {
            // Ok, use f as is
          }
          changeDirectory(dir);
          return;
        }
      }

      JFileChooser chooser = getFileChooser();

      String filename = getFileName();
      FileSystemView fs = chooser.getFileSystemView();
      File dir = chooser.getCurrentDirectory();

      if (filename != null) {
        // Remove whitespaces from end of filename
        int i = filename.length() - 1;

        while (i >= 0 && filename.charAt(i) <= ' ') {
          i--;
        }

        filename = filename.substring(0, i + 1);
      }

      if (filename == null || filename.length() == 0) {
        // no file selected, multiple selection off, therefore cancel the approve action
        resetGlobFilter();
        return;
      }

      File selectedFile = null;
      File[] selectedFiles = null;

      // Unix: Resolve '~' to user's home directory
      if (File.separatorChar == '/') {
        if (filename.startsWith("~/")) {
          filename = System.getProperty("user.home") + filename.substring(1);
        } else if (filename.equals("~")) {
          filename = System.getProperty("user.home");
        }
      }

      if (chooser.isMultiSelectionEnabled()
          && filename.length() > 1
          && filename.charAt(0) == '"'
          && filename.charAt(filename.length() - 1) == '"') {
        List<File> fList = new ArrayList<File>();

        String[] files = filename.substring(1, filename.length() - 1).split("\" \"");
        // Optimize searching files by names in "children" array
        Arrays.sort(files);

        File[] children = null;
        int childIndex = 0;

        for (String str : files) {
          File file = fs.createFileObject(str);
          if (!file.isAbsolute()) {
            if (children == null) {
              children = fs.getFiles(dir, false);
              Arrays.sort(children);
            }
            for (int k = 0; k < children.length; k++) {
              int l = (childIndex + k) % children.length;
              if (children[l].getName().equals(str)) {
                file = children[l];
                childIndex = l + 1;
                break;
              }
            }
          }
          fList.add(file);
        }

        if (!fList.isEmpty()) {
          selectedFiles = fList.toArray(new File[fList.size()]);
        }
        resetGlobFilter();
      } else {
        selectedFile = fs.createFileObject(filename);
        if (!selectedFile.isAbsolute()) {
          selectedFile = fs.getChild(dir, filename);
        }
        // check for wildcard pattern
        FileFilter currentFilter = chooser.getFileFilter();
        if (!selectedFile.exists() && isGlobPattern(filename)) {
          changeDirectory(selectedFile.getParentFile());
          if (globFilter == null) {
            globFilter = new GlobFilter();
          }
          try {
            globFilter.setPattern(selectedFile.getName());
            if (!(currentFilter instanceof GlobFilter)) {
              actualFileFilter = currentFilter;
            }
            chooser.setFileFilter(null);
            chooser.setFileFilter(globFilter);
            return;
          } catch (PatternSyntaxException pse) {
            // Not a valid glob pattern. Abandon filter.
          }
        }

        resetGlobFilter();

        // Check for directory change action
        boolean isDir = (selectedFile != null && selectedFile.isDirectory());
        boolean isTrav = (selectedFile != null && chooser.isTraversable(selectedFile));
        boolean isDirSelEnabled = chooser.isDirectorySelectionEnabled();
        boolean isFileSelEnabled = chooser.isFileSelectionEnabled();
        boolean isCtrl =
            (e != null
                && (e.getModifiers() & Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) != 0);

        if (isDir && isTrav && (isCtrl || !isDirSelEnabled)) {
          changeDirectory(selectedFile);
          return;
        } else if ((isDir || !isFileSelEnabled)
            && (!isDir || !isDirSelEnabled)
            && (!isDirSelEnabled || selectedFile.exists())) {
          selectedFile = null;
        }
      }

      if (selectedFiles != null || selectedFile != null) {
        if (selectedFiles != null || chooser.isMultiSelectionEnabled()) {
          if (selectedFiles == null) {
            selectedFiles = new File[] {selectedFile};
          }
          chooser.setSelectedFiles(selectedFiles);
          // Do it again. This is a fix for bug 4949273 to force the
          // selected value in case the ListSelectionModel clears it
          // for non-existing file names.
          chooser.setSelectedFiles(selectedFiles);
        } else {
          chooser.setSelectedFile(selectedFile);
        }
        chooser.approveSelection();
      } else {
        if (chooser.isMultiSelectionEnabled()) {
          chooser.setSelectedFiles(null);
        } else {
          chooser.setSelectedFile(null);
        }
        chooser.cancelSelection();
      }
    }
コード例 #4
0
ファイル: UIFileChooser.java プロジェクト: maiklos/Compendium
  /** Approve the file selection against the required extension. */
  public void approveSelection() {

    File file = getSelectedFile();
    if (file == null) {
      // no file selected ... -> cancelSelection()
    } else {
      if (getDialogType() == JFileChooser.SAVE_DIALOG) {

        String fileName = file.getAbsolutePath();

        File newfile = file;
        if (fileName != null && !sRequiredExtension.equals("")) { // $NON-NLS-1$
          if (!fileName.toLowerCase().endsWith(sRequiredExtension)) {
            fileName = fileName + sRequiredExtension;
            newfile = new File(fileName);
            setSelectedFile(newfile);
          }
        }

        if (newfile.exists()) {
          int answer =
              JOptionPane.showConfirmDialog(
                  this,
                  LanguageProperties.getString(
                      LanguageProperties.UI_GENERAL_BUNDLE, "UIFileChooser.fileExists"),
                  LanguageProperties.getString(
                      LanguageProperties.UI_GENERAL_BUNDLE,
                      "UIFileChooser.warning"), //$NON-NLS-1$ //$NON-NLS-2$
                  JOptionPane.OK_CANCEL_OPTION,
                  JOptionPane.WARNING_MESSAGE);

          if (answer == JOptionPane.OK_OPTION) {
            super.approveSelection();
          } else {
            // a user cancelled over write.
          }
        } else {
          super.approveSelection();
        }
      } else if (getDialogType() == JFileChooser.OPEN_DIALOG) {

        String fileName = file.getAbsolutePath();
        if (fileName != null) {
          if (!sRequiredExtension.equals("")) { // $NON-NLS-1$

            if (!fileName.toLowerCase().endsWith(sRequiredExtension)) {
              JOptionPane.showMessageDialog(
                  this,
                  LanguageProperties.getString(
                          LanguageProperties.UI_GENERAL_BUNDLE, "UIFileChooser.selectFile")
                      + " '"
                      + sRequiredExtension
                      + "'"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            } else {
              super.approveSelection();
            }
          } else {
            super.approveSelection();
          }
        }
      } else {
        super.approveSelection();
      }
    }
  }