2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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/>.
23 import javax.swing.filechooser.FileFilter;
25 public class JalviewFileFilter extends FileFilter
27 public static Hashtable suffixHash = new Hashtable();
29 private Hashtable filters = null;
31 private String description = "no description";
33 private String fullDescription = "full description";
35 private boolean useExtensionsInDescription = true;
37 public JalviewFileFilter(String extension, String description)
39 StringTokenizer st = new StringTokenizer(extension, ",");
41 while (st.hasMoreElements())
43 addExtension(st.nextToken().trim());
46 setDescription(description);
49 public JalviewFileFilter(String[] filts)
54 public JalviewFileFilter(String[] filts, String description)
56 for (int i = 0; i < filts.length; i++)
58 // add filters one by one
59 addExtension(filts[i]);
62 if (description != null)
64 setDescription(description);
68 public String getAcceptableExtension()
70 return filters.keys().nextElement().toString();
73 // takes account of the fact that database is a directory
74 public boolean accept(File f)
78 String extension = getExtension(f);
85 if ((extension != null) && (filters.get(getExtension(f)) != null))
94 public String getExtension(File f)
98 String filename = f.getName();
99 int i = filename.lastIndexOf('.');
101 if ((i > 0) && (i < (filename.length() - 1)))
103 return filename.substring(i + 1).toLowerCase();
112 public void addExtension(String extension)
116 filters = new Hashtable(5);
119 filters.put(extension.toLowerCase(), this);
120 fullDescription = null;
123 public String getDescription()
125 if (fullDescription == null)
127 if ((description == null) || isExtensionListInDescription())
129 fullDescription = (description == null) ? "("
130 : (description + " (");
132 // build the description from the extension list
133 Enumeration extensions = filters.keys();
135 if (extensions != null)
137 fullDescription += ("." + (String) extensions.nextElement());
139 while (extensions.hasMoreElements())
141 fullDescription += (", " + (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;