2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
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;