public DefaultSaveDialog( JFrame parent, Path lastDirectory, Path initialFile, FileFilter filter, String defaultExtension) { this.parent = parent; this.defaultExtension = defaultExtension; fileChooser = new OverwritePromptFileChooser(); for (javax.swing.filechooser.FileFilter fileFilter : fileChooser.getChoosableFileFilters()) { fileChooser.removeChoosableFileFilter(fileFilter); } if (fileChooser instanceof OverwritePromptFileChooser) { ((OverwritePromptFileChooser) fileChooser).setFileFilter(filter, defaultExtension); } else { fileChooser.setFileFilter(filter); } if (lastDirectory != null) { fileChooser.setCurrentDirectory(lastDirectory.toFile()); } fileChooser.setSelectedFile(initialFile != null ? initialFile.toFile() : null); }
public File[] getFiles( boolean open, boolean multiple, String message, String file, final File directory, String... filter) { if (directory == null) { fileChooser = new JFileChooser(fileChooser.getCurrentDirectory()); } else { fileChooser = new JFileChooser(directory); } if (file != null) { fileChooser.setSelectedFile(new File(directory, file)); } fileChooser.setMultiSelectionEnabled(multiple); if (filter.length > 0) { for (int i = 0; i < filter.length; i += 2) { final String description = filter[i]; final String suffix = filter[i + 1].toLowerCase(); MyFileFilter fileFilter = new MyFileFilter() { @Override public boolean accept(File f) { if (f.isDirectory()) { return true; } String name = f.getName().toLowerCase(); return name.endsWith(suffix); } @Override public String getDescription() { return description; } }; fileFilter.suffix = suffix; fileChooser.addChoosableFileFilter(fileFilter); } if (file != null) { int dot = file.lastIndexOf('.'); if (dot >= 0) { String type = file.substring(dot + 1); for (FileFilter _filter : fileChooser.getChoosableFileFilters()) { if (_filter instanceof MyFileFilter) { if (((MyFileFilter) _filter).suffix.equals(type)) { fileChooser.setFileFilter(_filter); break; } } } } } } fileChooser.setDialogTitle(message); int ret; if (open) { ret = fileChooser.showOpenDialog(this); } else { ret = fileChooser.showSaveDialog(this); } if (ret == JFileChooser.APPROVE_OPTION) { if (multiple) { if (fileChooser.getSelectedFiles() == null || fileChooser.getSelectedFiles().length == 0) { return null; } return fileChooser.getSelectedFiles(); } File selectedFile = fileChooser.getSelectedFile(); if (!open) { FileFilter selectedFilter = fileChooser.getFileFilter(); if (selectedFilter instanceof MyFileFilter) { String type = ((MyFileFilter) selectedFilter).suffix; selectedFile = UIUtilities.affixType(selectedFile, type); } } return new File[] {selectedFile}; } return null; }