2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
26 import javax.swing.filechooser.FileFilter;
28 public class JalviewFileFilter extends FileFilter
30 public static Hashtable suffixHash = new Hashtable();
32 private Hashtable filters = null;
34 private String description = "no description";
36 private String fullDescription = "full description";
38 private boolean useExtensionsInDescription = true;
40 public JalviewFileFilter(String extension, String description)
42 StringTokenizer st = new StringTokenizer(extension, ",");
44 while (st.hasMoreElements())
46 addExtension(st.nextToken().trim());
49 setDescription(description);
52 public JalviewFileFilter(String[] filts)
57 public JalviewFileFilter(String[] filts, String description)
59 for (int i = 0; i < filts.length; i++)
61 // add filters one by one
62 addExtension(filts[i]);
65 if (description != null)
67 setDescription(description);
71 public String getAcceptableExtension()
73 return filters.keys().nextElement().toString();
76 // takes account of the fact that database is a directory
77 public boolean accept(File f)
81 String extension = getExtension(f);
88 if ((extension != null) && (filters.get(getExtension(f)) != null))
97 public String getExtension(File f)
101 String filename = f.getName();
102 int i = filename.lastIndexOf('.');
104 if ((i > 0) && (i < (filename.length() - 1)))
106 return filename.substring(i + 1).toLowerCase();
115 public void addExtension(String extension)
119 filters = new Hashtable(5);
122 filters.put(extension.toLowerCase(), this);
123 fullDescription = null;
126 public String getDescription()
128 if (fullDescription == null)
130 if ((description == null) || isExtensionListInDescription())
132 fullDescription = (description == null) ? "("
133 : (description + " (");
135 // build the description from the extension list
136 Enumeration extensions = filters.keys();
138 if (extensions != null)
140 fullDescription += ("." + (String) extensions.nextElement());
142 while (extensions.hasMoreElements())
144 fullDescription += (", " + (String) extensions.nextElement());
148 fullDescription += ")";
152 fullDescription = description;
156 return fullDescription;
159 public void setDescription(String description)
161 this.description = description;
162 fullDescription = null;
165 public void setExtensionListInDescription(boolean b)
167 useExtensionsInDescription = b;
168 fullDescription = null;
171 public boolean isExtensionListInDescription()
173 return useExtensionsInDescription;