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;
27 import java.util.Locale;
29 import java.util.StringTokenizer;
31 import javax.swing.filechooser.FileFilter;
33 public class JalviewFileFilter extends FileFilter
35 public static Hashtable suffixHash = new Hashtable();
37 private Map<String, JalviewFileFilter> filters = null;
39 private String description = "no description";
41 private String fullDescription = "full description";
43 private boolean useExtensionsInDescription = true;
45 private JalviewFileChooser parentJFC = null;
47 public JalviewFileFilter(String extension, String description)
49 StringTokenizer st = new StringTokenizer(extension, ",");
51 while (st.hasMoreElements())
53 addExtension(st.nextToken().trim());
56 setDescription(description);
59 public JalviewFileFilter(String[] filts)
64 public JalviewFileFilter(String[] filts, String description)
66 for (int i = 0; i < filts.length; i++)
68 // add filters one by one
69 addExtension(filts[i]);
72 if (description != null)
74 setDescription(description);
78 public String getAcceptableExtension()
80 return filters.keySet().iterator().next().toString();
83 // takes account of the fact that database is a directory
85 public boolean accept(File f)
90 String extension = getExtension(f);
97 if ((extension != null) && (filters.get(extension) != null))
104 if (parentJFC != null && parentJFC.includeBackupFiles)
106 Iterator<String> it = filters.keySet().iterator();
107 EXTENSION: while (it.hasNext())
109 String ext = it.next();
111 // quick negative test
112 if (!f.getName().contains(ext))
117 BackupFilenameParts bfp = BackupFilenameParts
118 .currentBackupFilenameParts(f.getName(), ext, true);
119 if (bfp.isBackupFile())
129 public String getExtension(File f)
133 String filename = f.getName();
134 int i = filename.lastIndexOf('.');
136 if ((i > 0) && (i < (filename.length() - 1)))
138 return filename.substring(i + 1).toLowerCase(Locale.ROOT);
147 public void addExtension(String extension)
151 filters = new LinkedHashMap<>(5);
154 filters.put(extension.toLowerCase(Locale.ROOT), this);
155 fullDescription = null;
159 public String getDescription()
161 if (fullDescription == null)
163 if ((description == null) || isExtensionListInDescription())
165 fullDescription = (description == null) ? "("
166 : (description + " (");
168 // build the description from the extension list
169 Iterator<String> extensions = filters.keySet().iterator();
171 if (extensions != null)
173 fullDescription += ("." + extensions.next());
175 while (extensions.hasNext())
177 fullDescription += (", ." + extensions.next());
181 fullDescription += ")";
185 fullDescription = description;
189 return fullDescription;
192 public void setDescription(String description)
194 this.description = description;
195 fullDescription = null;
198 public void setExtensionListInDescription(boolean b)
200 useExtensionsInDescription = b;
201 fullDescription = null;
204 public boolean isExtensionListInDescription()
206 return useExtensionsInDescription;
209 protected void setParentJFC(JalviewFileChooser p)