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();
108 String ext = it.next();
110 BackupFilenameParts bfp = BackupFilenameParts
111 .currentBackupFilenameParts(f.getName(), ext, true);
112 if (bfp.isBackupFile())
122 public String getExtension(File f)
126 String filename = f.getName();
127 int i = filename.lastIndexOf('.');
129 if ((i > 0) && (i < (filename.length() - 1)))
131 return filename.substring(i + 1).toLowerCase();
140 public void addExtension(String extension)
144 filters = new LinkedHashMap<>(5);
147 filters.put(extension.toLowerCase(), this);
148 fullDescription = null;
152 public String getDescription()
154 if (fullDescription == null)
156 if ((description == null) || isExtensionListInDescription())
158 fullDescription = (description == null) ? "("
159 : (description + " (");
161 // build the description from the extension list
162 Iterator<String> extensions = filters.keySet().iterator();
164 if (extensions != null)
166 fullDescription += ("." + extensions.next());
168 while (extensions.hasNext())
170 fullDescription += (", " + extensions.next());
174 fullDescription += ")";
178 fullDescription = description;
182 return fullDescription;
185 public void setDescription(String description)
187 this.description = description;
188 fullDescription = null;
191 public void setExtensionListInDescription(boolean b)
193 useExtensionsInDescription = b;
194 fullDescription = null;
197 public boolean isExtensionListInDescription()
199 return useExtensionsInDescription;
202 protected void setParentJFC(JalviewFileChooser p)