private File checkFileExtension(File file) { if (MyFileFilter.getExtension(file) == null || !MyFileFilter.getExtension(file).equals(JSMAA_MODELFILE_EXTENSION)) { return new File(file.getAbsolutePath() + "." + JSMAA_MODELFILE_EXTENSION); } return file; }
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; }