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
29 public static Hashtable suffixHash = new Hashtable();
30 private Hashtable filters = null;
31 private String description = "no description";
32 private String fullDescription = "full description";
33 private boolean useExtensionsInDescription = true;
35 public JalviewFileFilter(String extension, String description)
37 StringTokenizer st = new StringTokenizer(extension, ",");
39 while (st.hasMoreElements())
41 addExtension(st.nextToken().trim());
44 setDescription(description);
47 public JalviewFileFilter(String[] filts)
52 public JalviewFileFilter(String[] filts, String description)
54 for (int i = 0; i < filts.length; i++)
56 // add filters one by one
57 addExtension(filts[i]);
60 if (description != null)
62 setDescription(description);
66 public String getAcceptableExtension()
68 return filters.keys().nextElement().toString();
71 // takes account of the fact that database is a directory
72 public boolean accept(File f)
76 String extension = getExtension(f);
83 if ( (extension != null) && (filters.get(getExtension(f)) != null))
92 public String getExtension(File f)
96 String filename = f.getName();
97 int i = filename.lastIndexOf('.');
99 if ( (i > 0) && (i < (filename.length() - 1)))
101 return filename.substring(i + 1).toLowerCase();
110 public void addExtension(String extension)
114 filters = new Hashtable(5);
117 filters.put(extension.toLowerCase(), this);
118 fullDescription = null;
121 public String getDescription()
123 if (fullDescription == null)
125 if ( (description == null) || isExtensionListInDescription())
127 fullDescription = (description == null) ? "(" : (description +
130 // build the description from the extension list
131 Enumeration extensions = filters.keys();
133 if (extensions != null)
135 fullDescription += ("." +
136 (String) extensions.nextElement());
138 while (extensions.hasMoreElements())
140 fullDescription += (", " +
141 (String) extensions.nextElement());
145 fullDescription += ")";
149 fullDescription = description;
153 return fullDescription;
156 public void setDescription(String description)
158 this.description = description;
159 fullDescription = null;
162 public void setExtensionListInDescription(boolean b)
164 useExtensionsInDescription = b;
165 fullDescription = null;
168 public boolean isExtensionListInDescription()
170 return useExtensionsInDescription;