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.Hashtable;
25 import java.util.Iterator;
26 import java.util.LinkedHashMap;
28 import java.util.StringTokenizer;
30 import javax.swing.filechooser.FileFilter;
32 public class JalviewFileFilter extends FileFilter
34 public static Hashtable suffixHash = new Hashtable();
36 private Map<String, JalviewFileFilter> filters = null;
38 private String description = "no description";
40 private String fullDescription = "full description";
42 private boolean useExtensionsInDescription = true;
44 public JalviewFileFilter(String extension, String description)
46 StringTokenizer st = new StringTokenizer(extension, ",");
48 while (st.hasMoreElements())
50 addExtension(st.nextToken().trim());
53 setDescription(description);
56 public JalviewFileFilter(String[] filts)
61 public JalviewFileFilter(String[] filts, String description)
63 for (int i = 0; i < filts.length; i++)
65 // add filters one by one
66 addExtension(filts[i]);
69 if (description != null)
71 setDescription(description);
75 public String getAcceptableExtension()
77 return filters.keySet().iterator().next().toString();
80 // takes account of the fact that database is a directory
82 public boolean accept(File f)
86 String extension = getExtension(f);
93 if ((extension != null) && (filters.get(extension) != null))
102 public String getExtension(File f)
106 String filename = f.getName();
107 int i = filename.lastIndexOf('.');
109 if ((i > 0) && (i < (filename.length() - 1)))
111 return filename.substring(i + 1).toLowerCase();
120 public void addExtension(String extension)
124 filters = new LinkedHashMap<>(5);
127 filters.put(extension.toLowerCase(), this);
128 fullDescription = null;
132 public String getDescription()
134 if (fullDescription == null)
136 if ((description == null) || isExtensionListInDescription())
138 fullDescription = (description == null) ? "("
139 : (description + " (");
141 // build the description from the extension list
142 Iterator<String> extensions = filters.keySet().iterator();
144 if (extensions != null)
146 fullDescription += ("." + extensions.next());
148 while (extensions.hasNext())
150 fullDescription += (", " + extensions.next());
154 fullDescription += ")";
158 fullDescription = description;
162 return fullDescription;
165 public void setDescription(String description)
167 this.description = description;
168 fullDescription = null;
171 public void setExtensionListInDescription(boolean b)
173 useExtensionsInDescription = b;
174 fullDescription = null;
177 public boolean isExtensionListInDescription()
179 return useExtensionsInDescription;