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 private JalviewFileChooser parentJFC = null;
46 public JalviewFileFilter(String extension, String description)
48 StringTokenizer st = new StringTokenizer(extension, ",");
50 while (st.hasMoreElements())
52 addExtension(st.nextToken().trim());
55 setDescription(description);
58 public JalviewFileFilter(String[] filts)
63 public JalviewFileFilter(String[] filts, String description)
65 for (int i = 0; i < filts.length; i++)
67 // add filters one by one
68 addExtension(filts[i]);
71 if (description != null)
73 setDescription(description);
77 public String getAcceptableExtension()
79 return filters.keySet().iterator().next().toString();
82 // takes account of the fact that database is a directory
84 public boolean accept(File f)
89 String extension = getExtension(f);
96 if ((extension != null) && (filters.get(extension) != null))
103 if (parentJFC != null && parentJFC.includeBackupFiles)
105 Iterator<String> it = filters.keySet().iterator();
106 EXTENSION: while (it.hasNext())
108 String ext = it.next();
110 // quick negative test
111 if (!f.getName().contains(ext))
116 BackupFilenameParts bfp = BackupFilenameParts
117 .currentBackupFilenameParts(f.getName(), ext, true);
118 if (bfp.isBackupFile())
128 public String getExtension(File f)
132 String filename = f.getName();
133 int i = filename.lastIndexOf('.');
135 if ((i > 0) && (i < (filename.length() - 1)))
137 return filename.substring(i + 1).toLowerCase();
146 public void addExtension(String extension)
150 filters = new LinkedHashMap<>(5);
153 filters.put(extension.toLowerCase(), this);
154 fullDescription = null;
158 public String getDescription()
160 if (fullDescription == null)
162 if ((description == null) || isExtensionListInDescription())
164 fullDescription = (description == null) ? "("
165 : (description + " (");
167 // build the description from the extension list
168 Iterator<String> extensions = filters.keySet().iterator();
170 if (extensions != null)
172 fullDescription += ("." + extensions.next());
174 while (extensions.hasNext())
176 fullDescription += (", " + extensions.next());
180 fullDescription += ")";
184 fullDescription = description;
188 return fullDescription;
191 public void setDescription(String description)
193 this.description = description;
194 fullDescription = null;
197 public void setExtensionListInDescription(boolean b)
199 useExtensionsInDescription = b;
200 fullDescription = null;
203 public boolean isExtensionListInDescription()
205 return useExtensionsInDescription;
208 protected void setParentJFC(JalviewFileChooser p)