From faaf2a4443ab575fbe070a657eb6b4a4de89aa91 Mon Sep 17 00:00:00 2001 From: Michael Hoffer Date: Sun, 31 Jan 2016 17:08:37 +0100 Subject: [PATCH] Use native File dialogs if possible (issue #63): - https://github.com/VRL-Studio/VRL/issues/63 - native dialogs are not always possible since they are not as flexible as the Swing dialogs. In these cases, the Swing dialog will be used instead --- .../vrl/dialogs/FileDialogManager.java | 119 ++--- .../vrl/dialogs/VNativeFileChooser.java | 418 ++++++++++++++++++ VRL/src/eu/mihosoft/vrl/io/ImageFilter.java | 12 +- .../eu/mihosoft/vrl/io/ProjectFileFilter.java | 9 +- .../mihosoft/vrl/io/VExtensionFileFilter.java | 25 ++ VRL/src/eu/mihosoft/vrl/io/VFileFilter.java | 21 +- 6 files changed, 526 insertions(+), 78 deletions(-) create mode 100644 VRL/src/eu/mihosoft/vrl/dialogs/VNativeFileChooser.java create mode 100644 VRL/src/eu/mihosoft/vrl/io/VExtensionFileFilter.java diff --git a/VRL/src/eu/mihosoft/vrl/dialogs/FileDialogManager.java b/VRL/src/eu/mihosoft/vrl/dialogs/FileDialogManager.java index 006250a1..541f4954 100644 --- a/VRL/src/eu/mihosoft/vrl/dialogs/FileDialogManager.java +++ b/VRL/src/eu/mihosoft/vrl/dialogs/FileDialogManager.java @@ -54,6 +54,7 @@ import eu.mihosoft.vrl.io.FileSaver; import eu.mihosoft.vrl.io.FileLoader; import eu.mihosoft.vrl.io.RestrictedFileSystemView; +import eu.mihosoft.vrl.io.VExtensionFileFilter; import eu.mihosoft.vrl.system.VSysUtil; import eu.mihosoft.vrl.visual.Canvas; import eu.mihosoft.vrl.visual.VSwingUtil; @@ -69,6 +70,7 @@ import java.util.logging.Logger; import javax.swing.*; import javax.swing.filechooser.FileFilter; +import javax.swing.filechooser.FileNameExtensionFilter; /** * A file dialog manager is responsible for showing a dialog for loading/saving @@ -88,7 +90,6 @@ public static File getDefaultDir() { return CACHED_DIR; } - // public static final String FILE_OR_FOLDER_LOADED_ACTION = "file-or-folder-selected"; // // // protected File latestFileOrFolder; @@ -150,14 +151,10 @@ public Object loadFile(Component parent, FileLoader loader, File directory, Object result = null; - if (VSysUtil.isMacOSX()) { - VSwingUtil.forceAppleLAF(null); - } - - final JFileChooser fc = new JFileChooser(); - directory = chooseDefaultDir(directory); + final JFileChooser fc = createFileChooser(restrict, filter); + if (restrict) { fc.setFileSystemView(new RestrictedFileSystemView(directory)); } @@ -173,7 +170,6 @@ public Object loadFile(Component parent, FileLoader loader, File directory, } } - fc.setFileFilter(filter); int returnVal = fc.showOpenDialog(parent); if (returnVal == JFileChooser.APPROVE_OPTION) { @@ -203,7 +199,6 @@ public Object loadFile(Component parent, FileLoader loader, File directory, FileNotFoundDialog.show(parent); } - } else { System.out.println( ">> Operation \"load file\" cancelled by user." + "\n"); @@ -216,6 +211,27 @@ public Object loadFile(Component parent, FileLoader loader, File directory, return result; } + private JFileChooser createFileChooser(boolean restrict, FileFilter filter) { + final JFileChooser fc; + boolean nativeChooser = !restrict + && (filter instanceof FileNameExtensionFilter + || filter instanceof VExtensionFileFilter || filter == null); + if (nativeChooser) { + fc = new VNativeFileChooser(); + } else { + if (VSysUtil.isMacOSX()) { + VSwingUtil.forceAppleLAF(null); + } + fc = new JFileChooser(); + } + + if (filter != null) { + fc.setFileFilter(filter); + } + + return fc; + } + /** * Opens a load file dialog and returns the selected file. * @@ -230,6 +246,7 @@ public File getLoadFile(Component parent, FileFilter filter) { /** * Chooses the finally used default dir for all file dialogs in this class. + * * @param directory the directory (may be null) * @return the finally used default dir or null */ @@ -242,10 +259,11 @@ private static File chooseDefaultDir(File directory) { } /** - * Defines the default directory that shall be used if no directory has - * been specified. - * @param fileOrDir the default directory to set - * (if it is a file, the parent dir wil be used) + * Defines the default directory that shall be used if no directory has been + * specified. + * + * @param fileOrDir the default directory to set (if it is a file, the + * parent dir wil be used) */ public static void setDefaultDir(File fileOrDir) { @@ -278,10 +296,7 @@ public File getLoadFile(Component parent, File result = null; - if (VSysUtil.isMacOSX()) { - VSwingUtil.forceAppleLAF(null); - } - final JFileChooser fc = new JFileChooser(); + final JFileChooser fc = createFileChooser(restrict, filter); if (restrict) { fc.setFileSystemView(new RestrictedFileSystemView(directory)); @@ -290,8 +305,6 @@ public File getLoadFile(Component parent, fc.setCurrentDirectory(directory); - - fc.setFileFilter(filter); int returnVal = fc.showOpenDialog(parent); if (returnVal == JFileChooser.APPROVE_OPTION) { @@ -328,20 +341,15 @@ public File[] getLoadFiles(Component parent, File directory, FileFilter filter, boolean restrict) { File[] result = null; - if (VSysUtil.isMacOSX()) { - VSwingUtil.forceAppleLAF(null); - } - - final JFileChooser fc = new JFileChooser(); - directory = chooseDefaultDir(directory); + final JFileChooser fc = createFileChooser(restrict, filter); + if (restrict) { fc.setFileSystemView(new RestrictedFileSystemView(directory)); } fc.setCurrentDirectory(directory); - fc.setFileFilter(filter); fc.setMultiSelectionEnabled(true); int returnVal = fc.showOpenDialog(parent); @@ -397,15 +405,9 @@ public File getLoadFileOrFolder(Component parent, File directory, boolean restrict, int loadType, FileFilter filter) { File result = null; - if (VSysUtil.isMacOSX()) { - VSwingUtil.forceAppleLAF(null); - } - - final JFileChooser fc = new JFileChooser(); - directory = chooseDefaultDir(directory); - fc.setFileFilter(filter); + final JFileChooser fc = createFileChooser(restrict, filter); if (restrict) { fc.setFileSystemView(new RestrictedFileSystemView(directory)); @@ -439,7 +441,6 @@ public File getLoadFileOrFolder(Component parent, // // fireAction(new ActionEvent(this, 0, FILE_OR_FOLDER_LOADED_ACTION)); - return result; } @@ -459,15 +460,9 @@ public File getSaveFileOrFolder(Component parent, File directory, boolean restrict, int saveType, FileFilter filter) { File result = null; - if (VSysUtil.isMacOSX()) { - VSwingUtil.forceAppleLAF(null); - } - - final JFileChooser fc = new JFileChooser(); - directory = chooseDefaultDir(directory); - fc.setFileFilter(filter); + final JFileChooser fc = createFileChooser(restrict, filter); if (restrict) { fc.setFileSystemView(new RestrictedFileSystemView(directory)); @@ -501,7 +496,6 @@ public File getSaveFileOrFolder(Component parent, // // fireAction(new ActionEvent(this, 0, FILE_OR_FOLDER_LOADED_ACTION)); - return result; } @@ -538,14 +532,10 @@ public void saveFile(Component parent, Object o, FileSaver fileSaver, File directory, FileFilter filter, boolean restrict) { - if (VSysUtil.isMacOSX()) { - VSwingUtil.forceAppleLAF(null); - } - - final JFileChooser fc = new JFileChooser(); - directory = chooseDefaultDir(directory); + final JFileChooser fc = createFileChooser(restrict, filter); + if (restrict) { fc.setFileSystemView(new RestrictedFileSystemView(directory)); } @@ -561,8 +551,6 @@ public void saveFile(Component parent, Object o, } } - fc.setFileFilter(filter); - int returnVal = fc.showSaveDialog(parent); if (returnVal == JFileChooser.APPROVE_OPTION) { @@ -631,21 +619,17 @@ public File getSaveFile(Component parent, File directory, FileFilter filter, boolean restrict) { File result = null; - if (VSysUtil.isMacOSX()) { - VSwingUtil.forceAppleLAF(null); - } - - final JFileChooser fc = new JFileChooser(); - directory = chooseDefaultDir(directory); + final JFileChooser fc = createFileChooser(restrict, filter); + if (restrict) { fc.setFileSystemView(new RestrictedFileSystemView(directory)); } fc.setFileHidingEnabled(true); fc.setCurrentDirectory(directory); - fc.setFileFilter(filter); + int returnVal = fc.showSaveDialog(parent); if (returnVal == JFileChooser.APPROVE_OPTION) { @@ -687,19 +671,15 @@ public File[] getSaveFiles(Component parent, File directory, FileFilter filter, boolean restrict) { File[] result = null; - if (VSysUtil.isMacOSX()) { - VSwingUtil.forceAppleLAF(null); - } - - final JFileChooser fc = new JFileChooser(); - directory = chooseDefaultDir(directory); + final JFileChooser fc = createFileChooser(restrict, filter); + if (restrict) { fc.setFileSystemView(new RestrictedFileSystemView(directory)); } - // allow mut liple selections + // allow multiple selections fc.setMultiSelectionEnabled(true); fc.setFileHidingEnabled(true); fc.setCurrentDirectory(directory); @@ -746,14 +726,10 @@ public File getSaveFolder(Component parent, File directory, boolean restrict) { File result = null; - if (VSysUtil.isMacOSX()) { - VSwingUtil.forceAppleLAF(null); - } - - final JFileChooser fc = new JFileChooser(); - directory = chooseDefaultDir(directory); + final JFileChooser fc = createFileChooser(restrict, null); + if (restrict) { fc.setFileSystemView(new RestrictedFileSystemView(directory)); } @@ -765,7 +741,7 @@ public File getSaveFolder(Component parent, if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); - + setDefaultDir(file); // if (file.exists()) { @@ -913,7 +889,6 @@ public static void restrictNavigation(JFileChooser fc, File dir) { for (Component c : VSwingUtil.getAllChildren(fc, JComboBox.class)) { JComboBox box = (JComboBox) c; - if (box.getItemCount() == 0) { break; } diff --git a/VRL/src/eu/mihosoft/vrl/dialogs/VNativeFileChooser.java b/VRL/src/eu/mihosoft/vrl/dialogs/VNativeFileChooser.java new file mode 100644 index 00000000..d7349065 --- /dev/null +++ b/VRL/src/eu/mihosoft/vrl/dialogs/VNativeFileChooser.java @@ -0,0 +1,418 @@ +/* + * VNativeFilechooser.java + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2016 Steinbeis Forschungszentrum (STZ Ölbronn), + * Copyright (c) 2016 by Michael Hoffer + * + * This file is part of Visual Reflection Library (VRL). + * + * VRL is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * see: http://opensource.org/licenses/LGPL-3.0 + * file://path/to/VRL/src/eu/mihosoft/vrl/resources/license/lgplv3.txt + * + * VRL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * This version of VRL includes copyright notice and attribution requirements. + * According to the LGPL this information must be displayed even if you modify + * the source code of VRL. Neither the VRL Canvas attribution icon nor any + * copyright statement/attribution may be removed. + * + * Attribution Requirements: + * + * If you create derived work you must do three things regarding copyright + * notice and author attribution. + * + * First, the following text must be displayed on the Canvas: + * "based on VRL source code". In this case the VRL canvas icon must be removed. + * + * Second, the copyright notice must remain. It must be reproduced in any + * program that uses VRL. + * + * Third, add an additional notice, stating that you modified VRL. In addition + * you must cite the publications listed below. A suitable notice might read + * "VRL source code modified by YourName 2012". + * + * Note, that these requirements are in full accordance with the LGPL v3 + * (see 7. Additional Terms, b). + * + * Publications: + * + * M. Hoffer, C.Poliwoda, G.Wittum. Visual Reflection Library - + * A Framework for Declarative GUI Programming on the Java Platform. + * Computing and Visualization in Science, 2011, in press. + */ +package eu.mihosoft.vrl.dialogs; + +/* + * Original Header: + * + * Copyright (c) 2015, Veluria + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +import eu.mihosoft.vrl.io.VExtensionFileFilter; +import java.awt.Component; +import java.awt.HeadlessException; +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.FutureTask; +import java.util.logging.Level; +import java.util.logging.Logger; +import javafx.application.Platform; +import javafx.embed.swing.JFXPanel; +import javafx.stage.DirectoryChooser; +import javafx.stage.FileChooser; +import javax.swing.JFileChooser; +import static javax.swing.JFileChooser.DIRECTORIES_ONLY; +import javax.swing.filechooser.FileFilter; +import javax.swing.filechooser.FileNameExtensionFilter; +import javax.swing.filechooser.FileSystemView; + +/** + * This is a drop-in replacement for Swing's file chooser. Instead of displaying + * the Swing file chooser, this method makes use of the JavaFX file chooser. + * JavaFX uses the OS's native file chooser, which is much better than the Java + * one. Technically, this class is a waste of memory, but its use is convenient. + * Furthermore, if JavaFX is not available, the default file chooser will be + * displayed instead. Of course, this class will not compile if you don't have + * an JDK 8 or higher that has JavaFX support. Since this class will have to + * call the constructor of JFileChooser, it won't increase the performance of + * the file chooser, though; if anything, it might further decrease it. Please + * note that some methods have not been overwritten and may not have any impact + * on the file chooser. Sometimes, the new JavaFX file chooser does not provide + * a certain functionality, or the native file chooser provides them itself, but + * there are no methods to access them. For example, one feature that is not + * supported is the selection of files AND directories. If trying to set this + * using setFileSelectionMode(), still only files will be selectable. + * + * @author Veluria + * @author @author Michael Hoffer <info@michaelhoffer.de> + * @version 1.6.2-HEAD + */ +public class VNativeFileChooser extends JFileChooser { + + public static final boolean FX_AVAILABLE; + private List currentFiles; + private FileChooser fileChooser; + private File currentFile; + private DirectoryChooser directoryChooser; + + static { + boolean isFx; + try { + Class.forName("javafx.stage.FileChooser"); + isFx = true; + JFXPanel jfxPanel = new JFXPanel(); // initializes JavaFX environment + } catch (ClassNotFoundException e) { + isFx = false; + } + + FX_AVAILABLE = isFx; + } + + public VNativeFileChooser() { + initFxFileChooser(null); + } + + public VNativeFileChooser(String currentDirectoryPath) { + super(currentDirectoryPath); + initFxFileChooser(new File(currentDirectoryPath)); + } + + public VNativeFileChooser(File currentDirectory) { + super(currentDirectory); + initFxFileChooser(currentDirectory); + } + + public VNativeFileChooser(FileSystemView fsv) { + super(fsv); + initFxFileChooser(fsv.getDefaultDirectory()); + } + + public VNativeFileChooser(File currentDirectory, FileSystemView fsv) { + super(currentDirectory, fsv); + initFxFileChooser(currentDirectory); + } + + public VNativeFileChooser(String currentDirectoryPath, FileSystemView fsv) { + super(currentDirectoryPath, fsv); + initFxFileChooser(new File(currentDirectoryPath)); + } + + @Override + public int showOpenDialog(Component parent) throws HeadlessException { + if (!FX_AVAILABLE) { + return super.showOpenDialog(parent); + } + runAndWait(() -> { + // parent.setEnabled(false); + if (isDirectorySelectionEnabled()) { + directoryChooser.setTitle("Open Directory"); + currentFile = directoryChooser.showDialog(null); + } else if (isMultiSelectionEnabled()) { + fileChooser.setTitle("Open Files"); + currentFiles = fileChooser.showOpenMultipleDialog(null); + } else { + fileChooser.setTitle("Open File"); + currentFile = fileChooser.showOpenDialog(null); + } + }); + + if (isMultiSelectionEnabled()) { + if (currentFiles != null) { + return JFileChooser.APPROVE_OPTION; + } else { + return JFileChooser.CANCEL_OPTION; + } + } else if (currentFile != null) { + return JFileChooser.APPROVE_OPTION; + } else { + return JFileChooser.CANCEL_OPTION; + } + + } + + // taken from here: http://www.guigarage.com/2013/01/invokeandwait-for-javafx/ + static void runAndWait(Runnable runnable) { + FutureTask future = new FutureTask(runnable, null); + Platform.runLater(future); + try { + future.get(); + } catch (InterruptedException | ExecutionException ex) { + Logger.getLogger(VNativeFileChooser.class.getName()).log(Level.SEVERE, null, ex); + } + } + + @Override + public int showSaveDialog(Component parent) throws HeadlessException { + if (!FX_AVAILABLE) { + return super.showSaveDialog(parent); + } + + runAndWait(() -> { + // parent.setEnabled(false); + if (isDirectorySelectionEnabled()) { + directoryChooser.setTitle("Save Directory"); + currentFile = directoryChooser.showDialog(null); + } else { + fileChooser.setTitle("Save File"); + currentFile = fileChooser.showSaveDialog(null); + } + + }); + + if (currentFile != null) { + return JFileChooser.APPROVE_OPTION; + } else { + return JFileChooser.CANCEL_OPTION; + } + } + + @Override + public int showDialog(Component parent, String approveButtonText) throws HeadlessException { + if (!FX_AVAILABLE) { + return super.showDialog(parent, approveButtonText); + } + return showOpenDialog(parent); + } + + @Override + public File[] getSelectedFiles() { + if (!FX_AVAILABLE) { + return super.getSelectedFiles(); + } + if (currentFiles == null) { + return null; + } + return currentFiles.toArray(new File[currentFiles.size()]); + } + + @Override + public File getSelectedFile() { + if (!FX_AVAILABLE) { + return super.getSelectedFile(); + } + return currentFile; + } + + @Override + public void setSelectedFiles(File[] selectedFiles) { + if (!FX_AVAILABLE) { + super.setSelectedFiles(selectedFiles); + return; + } + if (selectedFiles == null || selectedFiles.length == 0) { + currentFiles = null; + } else { + setSelectedFile(selectedFiles[0]); + currentFiles = new ArrayList<>(Arrays.asList(selectedFiles)); + } + } + + @Override + public void setSelectedFile(File file) { + if (!FX_AVAILABLE) { + super.setSelectedFile(file); + return; + } + currentFile = file; + if (file != null) { + if (file.isDirectory()) { + fileChooser.setInitialDirectory(file.getAbsoluteFile()); + + if (directoryChooser != null) { + directoryChooser.setInitialDirectory(file.getAbsoluteFile()); + } + } else if (file.isFile()) { + fileChooser.setInitialDirectory(file.getParentFile()); + fileChooser.setInitialFileName(file.getName()); + + if (directoryChooser != null) { + directoryChooser.setInitialDirectory(file.getParentFile()); + } + } + + } + } + + @Override + public void setFileSelectionMode(int mode) { + super.setFileSelectionMode(mode); + if (!FX_AVAILABLE) { + return; + } + if (mode == DIRECTORIES_ONLY) { + if (directoryChooser == null) { + directoryChooser = new DirectoryChooser(); + } + setSelectedFile(currentFile); // Set file again, so directory chooser will be affected by it + setDialogTitle(getDialogTitle()); + } + } + + @Override + public void setDialogTitle(String dialogTitle) { + if (!FX_AVAILABLE) { + super.setDialogTitle(dialogTitle); + return; + } + fileChooser.setTitle(dialogTitle); + if (directoryChooser != null) { + directoryChooser.setTitle(dialogTitle); + } + } + + @Override + public String getDialogTitle() { + if (!FX_AVAILABLE) { + return super.getDialogTitle(); + } + return fileChooser.getTitle(); + } + + @Override + public void changeToParentDirectory() { + if (!FX_AVAILABLE) { + super.changeToParentDirectory(); + return; + } + File parentDir = fileChooser.getInitialDirectory().getParentFile(); + if (parentDir.isDirectory()) { + fileChooser.setInitialDirectory(parentDir); + if (directoryChooser != null) { + directoryChooser.setInitialDirectory(parentDir); + } + } + } + + @Override + public void addChoosableFileFilter(FileFilter filter) { + super.addChoosableFileFilter(filter); + if (!FX_AVAILABLE || filter == null) { + return; + } + if (filter instanceof FileNameExtensionFilter) { + FileNameExtensionFilter f = (FileNameExtensionFilter) filter; + + List ext = new ArrayList<>(); + for (String extension : f.getExtensions()) { + ext.add(extension.replaceAll("^\\*?\\.?(.*)$", "*.$1")); + } + if (!ext.isEmpty()) { + fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter(f.getDescription(), ext)); + } + } else if (filter instanceof VExtensionFileFilter) { + VExtensionFileFilter f = (VExtensionFileFilter) filter; + + List ext = new ArrayList<>(); + for (String extension : f.getExtensions()) { + ext.add(extension.replaceAll("^\\*?\\.?(.*)$", "*.$1")); + } + if (!ext.isEmpty()) { + fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter(f.getDescription(), ext)); + } + } + } + + @Override + public void setAcceptAllFileFilterUsed(boolean bool) { + boolean differs = isAcceptAllFileFilterUsed() ^ bool; + super.setAcceptAllFileFilterUsed(bool); + if (!FX_AVAILABLE) { + return; + } + if (differs) { + if (bool) { + fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("All files", "*.*")); + } else { + for (Iterator it = fileChooser.getExtensionFilters().iterator(); it.hasNext();) { + FileChooser.ExtensionFilter filter = it.next(); + if (filter.getExtensions().contains("*.*")) { + it.remove(); + } + } + } + } + } + + private void initFxFileChooser(File currentFile) { + if (FX_AVAILABLE) { + fileChooser = new FileChooser(); + this.currentFile = currentFile; + setSelectedFile(currentFile); + } + } + +} diff --git a/VRL/src/eu/mihosoft/vrl/io/ImageFilter.java b/VRL/src/eu/mihosoft/vrl/io/ImageFilter.java index 5653b729..d554e82a 100644 --- a/VRL/src/eu/mihosoft/vrl/io/ImageFilter.java +++ b/VRL/src/eu/mihosoft/vrl/io/ImageFilter.java @@ -53,13 +53,15 @@ package eu.mihosoft.vrl.io; import java.io.File; +import java.util.Arrays; +import java.util.List; import javax.swing.filechooser.*; /** * File extension filter for image files. * @author Michael Hoffer <info@michaelhoffer.de> */ -public class ImageFilter extends FileFilter { +public class ImageFilter extends FileFilter implements VExtensionFileFilter{ @Override public boolean accept(File f) { @@ -75,5 +77,13 @@ public boolean accept(File f) { public String getDescription() { return "Image Files (jpg, png, gif, tif)"; } + + + @Override + public List getExtensions() { + + return Arrays.asList( + new String[]{".jpeg",".jpg",".png",".gif", ".tiff", ".tif"}); + } } diff --git a/VRL/src/eu/mihosoft/vrl/io/ProjectFileFilter.java b/VRL/src/eu/mihosoft/vrl/io/ProjectFileFilter.java index 3dd3b971..9c857227 100644 --- a/VRL/src/eu/mihosoft/vrl/io/ProjectFileFilter.java +++ b/VRL/src/eu/mihosoft/vrl/io/ProjectFileFilter.java @@ -53,13 +53,15 @@ package eu.mihosoft.vrl.io; import java.io.File; +import java.util.Arrays; +import java.util.List; import javax.swing.filechooser.FileFilter; /** * File extension filter for VRL project files. * @author Michael Hoffer <info@michaelhoffer.de> */ -public class ProjectFileFilter extends FileFilter { +public class ProjectFileFilter extends FileFilter implements VExtensionFileFilter{ @Override public boolean accept(File f) { @@ -71,4 +73,9 @@ public boolean accept(File f) { public String getDescription() { return "VRL Project Files (.vrlp)"; } + + @Override + public List getExtensions() { + return Arrays.asList(".vrlp"); + } } diff --git a/VRL/src/eu/mihosoft/vrl/io/VExtensionFileFilter.java b/VRL/src/eu/mihosoft/vrl/io/VExtensionFileFilter.java new file mode 100644 index 00000000..ffc7b685 --- /dev/null +++ b/VRL/src/eu/mihosoft/vrl/io/VExtensionFileFilter.java @@ -0,0 +1,25 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package eu.mihosoft.vrl.io; + +import java.util.List; + +/** + * + * @author Michael Hoffer <info@michaelhoffer.de> + */ +public interface VExtensionFileFilter { + + String getDescription(); + + /** + * Returns the endings accepted by this file filter. + * + * @return the accepted endings + */ + List getExtensions(); + +} diff --git a/VRL/src/eu/mihosoft/vrl/io/VFileFilter.java b/VRL/src/eu/mihosoft/vrl/io/VFileFilter.java index ffaecd21..1976ccd1 100644 --- a/VRL/src/eu/mihosoft/vrl/io/VFileFilter.java +++ b/VRL/src/eu/mihosoft/vrl/io/VFileFilter.java @@ -49,7 +49,6 @@ * A Framework for Declarative GUI Programming on the Java Platform. * Computing and Visualization in Science, 2011, in press. */ - package eu.mihosoft.vrl.io; import java.io.File; @@ -59,12 +58,13 @@ /** * File extension filter for files. + * * @author Michael Hoffer <info@michaelhoffer.de> */ -public class VFileFilter extends FileFilter implements Serializable { +public class VFileFilter extends FileFilter implements Serializable, VExtensionFileFilter { private static final long serialVersionUID = 1L; - private ArrayList acceptedEndings = new ArrayList(); + private ArrayList acceptedEndings = new ArrayList<>(); private String description = "Files *.*"; @Override @@ -92,6 +92,7 @@ public String getDescription() { /** * Defines the description for this file filter. + * * @param description the description to set */ public void setDescription(String description) { @@ -100,14 +101,26 @@ public void setDescription(String description) { /** * Returns the endings accepted by this file filter. + * * @return the accepted endings */ public ArrayList getAcceptedEndings() { return acceptedEndings; } + /** + * Returns the endings accepted by this file filter. + * + * @return the accepted endings + */ + @Override + public ArrayList getExtensions() { + return acceptedEndings; + } + /** * Defines the endings accepted by this file filter. + * * @param acceptedEndings the endings to set */ public void setAcceptedEndings(ArrayList acceptedEndings) { @@ -116,10 +129,10 @@ public void setAcceptedEndings(ArrayList acceptedEndings) { /** * Adds an accepted ending to this file filter, e.g., ".jpg". + * * @param e the ending to add */ public void addEnding(String e) { acceptedEndings.add(e); } } -