2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.
24 import java.util.Enumeration;
25 import java.util.Hashtable;
26 import java.util.StringTokenizer;
28 import javax.swing.filechooser.FileFilter;
30 public class JalviewFileFilter extends FileFilter
32 public static Hashtable suffixHash = new Hashtable();
34 private Hashtable filters = null;
36 private String description = "no description";
38 private String fullDescription = "full description";
40 private boolean useExtensionsInDescription = true;
42 public JalviewFileFilter(String extension, String description)
44 StringTokenizer st = new StringTokenizer(extension, ",");
46 while (st.hasMoreElements())
48 addExtension(st.nextToken().trim());
51 setDescription(description);
54 public JalviewFileFilter(String[] filts)
59 public JalviewFileFilter(String[] filts, String description)
61 for (int i = 0; i < filts.length; i++)
63 // add filters one by one
64 addExtension(filts[i]);
67 if (description != null)
69 setDescription(description);
73 public String getAcceptableExtension()
75 return filters.keys().nextElement().toString();
78 // takes account of the fact that database is a directory
79 public boolean accept(File f)
83 String extension = getExtension(f);
90 if ((extension != null) && (filters.get(getExtension(f)) != null))
99 public String getExtension(File f)
103 String filename = f.getName();
104 int i = filename.lastIndexOf('.');
106 if ((i > 0) && (i < (filename.length() - 1)))
108 return filename.substring(i + 1).toLowerCase();
117 public void addExtension(String extension)
121 filters = new Hashtable(5);
124 filters.put(extension.toLowerCase(), this);
125 fullDescription = null;
128 public String getDescription()
130 if (fullDescription == null)
132 if ((description == null) || isExtensionListInDescription())
134 fullDescription = (description == null) ? "("
135 : (description + " (");
137 // build the description from the extension list
138 Enumeration extensions = filters.keys();
140 if (extensions != null)
142 fullDescription += ("." + (String) extensions.nextElement());
144 while (extensions.hasMoreElements())
146 fullDescription += (", " + (String) extensions.nextElement());
150 fullDescription += ")";
154 fullDescription = description;
158 return fullDescription;
161 public void setDescription(String description)
163 this.description = description;
164 fullDescription = null;
167 public void setExtensionListInDescription(boolean b)
169 useExtensionsInDescription = b;
170 fullDescription = null;
173 public boolean isExtensionListInDescription()
175 return useExtensionsInDescription;