2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 import javax.swing.filechooser.FileFilter;
26 public class JalviewFileFilter extends FileFilter
28 public static Hashtable suffixHash = new Hashtable();
30 private Hashtable filters = null;
32 private String description = "no description";
34 private String fullDescription = "full description";
36 private boolean useExtensionsInDescription = true;
38 public JalviewFileFilter(String extension, String description)
40 StringTokenizer st = new StringTokenizer(extension, ",");
42 while (st.hasMoreElements())
44 addExtension(st.nextToken().trim());
47 setDescription(description);
50 public JalviewFileFilter(String[] filts)
55 public JalviewFileFilter(String[] filts, String description)
57 for (int i = 0; i < filts.length; i++)
59 // add filters one by one
60 addExtension(filts[i]);
63 if (description != null)
65 setDescription(description);
69 public String getAcceptableExtension()
71 return filters.keys().nextElement().toString();
74 // takes account of the fact that database is a directory
75 public boolean accept(File f)
79 String extension = getExtension(f);
86 if ((extension != null) && (filters.get(getExtension(f)) != null))
95 public String getExtension(File f)
99 String filename = f.getName();
100 int i = filename.lastIndexOf('.');
102 if ((i > 0) && (i < (filename.length() - 1)))
104 return filename.substring(i + 1).toLowerCase();
113 public void addExtension(String extension)
117 filters = new Hashtable(5);
120 filters.put(extension.toLowerCase(), this);
121 fullDescription = null;
124 public String getDescription()
126 if (fullDescription == null)
128 if ((description == null) || isExtensionListInDescription())
130 fullDescription = (description == null) ? "("
131 : (description + " (");
133 // build the description from the extension list
134 Enumeration extensions = filters.keys();
136 if (extensions != null)
138 fullDescription += ("." + (String) extensions.nextElement());
140 while (extensions.hasMoreElements())
142 fullDescription += (", " + (String) extensions.nextElement());
146 fullDescription += ")";
150 fullDescription = description;
154 return fullDescription;
157 public void setDescription(String description)
159 this.description = description;
160 fullDescription = null;
163 public void setExtensionListInDescription(boolean b)
165 useExtensionsInDescription = b;
166 fullDescription = null;
169 public boolean isExtensionListInDescription()
171 return useExtensionsInDescription;