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.
23 import java.util.Locale;
26 import java.util.Hashtable;
27 import java.util.Iterator;
28 import java.util.LinkedHashMap;
30 import java.util.StringTokenizer;
32 import javax.swing.filechooser.FileFilter;
34 public class JalviewFileFilter extends FileFilter
36 public static Hashtable suffixHash = new Hashtable();
38 private Map<String, JalviewFileFilter> filters = null;
40 private String description = "no description";
42 private String fullDescription = "full description";
44 private boolean useExtensionsInDescription = true;
46 private JalviewFileChooser parentJFC = null;
48 public JalviewFileFilter(String extension, String description)
50 StringTokenizer st = new StringTokenizer(extension, ",");
52 while (st.hasMoreElements())
54 addExtension(st.nextToken().trim());
57 setDescription(description);
60 public JalviewFileFilter(String[] filts)
65 public JalviewFileFilter(String[] filts, String description)
67 for (int i = 0; i < filts.length; i++)
69 // add filters one by one
70 addExtension(filts[i]);
73 if (description != null)
75 setDescription(description);
79 public String getAcceptableExtension()
81 return filters.keySet().iterator().next().toString();
84 // takes account of the fact that database is a directory
86 public boolean accept(File f)
91 String extension = getExtension(f);
98 if ((extension != null) && (filters.get(extension) != null))
105 if (parentJFC != null && parentJFC.includeBackupFiles)
107 Iterator<String> it = filters.keySet().iterator();
108 EXTENSION: while (it.hasNext())
110 String ext = it.next();
112 // quick negative test
113 if (!f.getName().contains(ext))
118 BackupFilenameParts bfp = BackupFilenameParts
119 .currentBackupFilenameParts(f.getName(), ext, true);
120 if (bfp.isBackupFile())
130 public String getExtension(File f)
134 String filename = f.getName();
135 int i = filename.lastIndexOf('.');
137 if ((i > 0) && (i < (filename.length() - 1)))
139 return filename.substring(i + 1).toLowerCase(Locale.ROOT);
148 public void addExtension(String extension)
152 filters = new LinkedHashMap<>(5);
155 filters.put(extension.toLowerCase(Locale.ROOT), this);
156 fullDescription = null;
160 public String getDescription()
162 if (fullDescription == null)
164 if ((description == null) || isExtensionListInDescription())
166 fullDescription = (description == null) ? "("
167 : (description + " (");
169 // build the description from the extension list
170 Iterator<String> extensions = filters.keySet().iterator();
172 if (extensions != null)
174 fullDescription += ("." + extensions.next());
176 while (extensions.hasNext())
178 fullDescription += (", " + extensions.next());
182 fullDescription += ")";
186 fullDescription = description;
190 return fullDescription;
193 public void setDescription(String description)
195 this.description = description;
196 fullDescription = null;
199 public void setExtensionListInDescription(boolean b)
201 useExtensionsInDescription = b;
202 fullDescription = null;
205 public boolean isExtensionListInDescription()
207 return useExtensionsInDescription;
210 protected void setParentJFC(JalviewFileChooser p)