001 /*
002 * Copyright (c) 1995-2010, The University of Sheffield. See the file
003 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
004 *
005 * This file is part of GATE (see http://gate.ac.uk/), and is free
006 * software, licenced under the GNU Library General Public License,
007 * Version 2, June 1991 (in the distribution as file licence.html,
008 * and also available at http://gate.ac.uk/gate/licence.html).
009 *
010 * Valentin Tablan, 22/May/2000
011 *
012 * $Id: ExtensionFileFilter.java 12006 2009-12-01 17:24:28Z thomas_heitz $
013 */
014 package gate.util;
015
016 import java.io.File;
017 import java.io.FileFilter;
018 import java.util.ArrayList;
019 import java.util.List;
020 import java.util.Arrays;
021
022 /**
023 * Implementation of a file name filter.
024 * This class is used by {@link javax.swing.JFileChooser} to filter the
025 * displayed files by their extension.
026 */
027 public class ExtensionFileFilter extends javax.swing.filechooser.FileFilter
028 implements FileFilter {
029
030 /**
031 * Builds a new ExtensionFileFilter.
032 */
033 public ExtensionFileFilter() {
034 }
035
036 /**
037 * Creates a FileNameExtensionFilter with the specified description and
038 * file name extensions. The returned FileNameExtensionFilter will accept
039 * all directories and any file with a file name extension contained
040 * in extensions.
041 * @param description textual description for the filter, may be null
042 * @param extensions the accepted file name extensions
043 */
044 public ExtensionFileFilter(String description, String... extensions) {
045 setDescription(description);
046 for (String extension : extensions) {
047 addExtension(extension);
048 }
049 }
050
051 /**
052 * Checks a file for compliance with the requested extensions.
053 *
054 * @param f file to test with this filter
055 */
056 public boolean accept(File f){
057 String name = f.getName();
058 if(f.isDirectory()) return true;
059
060 for (String acceptedExtension : acceptedExtensions) {
061 if (name.endsWith(acceptedExtension)) return true;
062 }
063 return false;
064 }
065
066 /**
067 * Returns the user-frielndly description for the files, e.g. "Text files"
068 *
069 */
070 public String getDescription() {
071 return (description == null) ? toString() : description;
072 }
073
074 /**
075 * Adds a new extension to the list of accepted extensions.
076 *
077 * @param extension file extension used to filter files
078 */
079 public void addExtension(String extension) {
080 acceptedExtensions.add(extension);
081 }
082
083 /**
084 * Sets the user friendly description for the accepted files.
085 *
086 * @param description description for this file filter
087 */
088 public void setDescription(String description) {
089 this.description = description;
090 }
091
092 /**
093 * @return the set of file name extensions files are tested against
094 */
095 public String[] getExtensions() {
096 return acceptedExtensions.toArray(new String[acceptedExtensions.size()]);
097 }
098
099 /**
100 * @return a string representation of this file filter.
101 */
102 public String toString() {
103 return "Filter for " + Arrays.toString(acceptedExtensions.toArray());
104 }
105
106 /** The list of accepted file name extensions. */
107 private List<String> acceptedExtensions = new ArrayList<String>();
108
109 /** The description of this file filter. */
110 private String description;
111
112 } // ExtensionFileFilter
|