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.Iterator;
25 import java.util.LinkedHashMap;
27 import java.util.StringTokenizer;
29 import javax.swing.filechooser.FileFilter;
31 public class JalviewFileFilter extends FileFilter
33 // public static Hashtable suffixHash = new Hashtable();
35 private Map<String, JalviewFileFilter> filters = null;
37 private String description = "no description";
39 private String fullDescription = "full description";
41 private boolean useExtensionsInDescription = true;
43 private JalviewFileChooser parentJFC = null;
45 public JalviewFileFilter(String extension, String description)
47 StringTokenizer st = new StringTokenizer(extension, ",");
49 while (st.hasMoreElements())
51 addExtension(st.nextToken().trim());
54 setDescription(description);
57 public JalviewFileFilter(String[] filts)
62 public JalviewFileFilter(String[] filts, String description)
64 for (int i = 0; i < filts.length; i++)
66 // add filters one by one
67 addExtension(filts[i]);
70 if (description != null)
72 setDescription(description);
76 public String getAcceptableExtension()
78 return filters.keySet().iterator().next().toString();
81 // takes account of the fact that database is a directory
83 public boolean accept(File f)
88 String extension = getExtension(f);
95 if ((extension != null) && (filters.get(extension) != null))
102 if (parentJFC != null && parentJFC.includeBackupFiles)
104 Iterator<String> it = filters.keySet().iterator();
105 EXTENSION: while (it.hasNext())
107 String ext = it.next();
109 // quick negative test
110 if (!f.getName().contains(ext))
115 BackupFilenameParts bfp = BackupFilenameParts
116 .currentBackupFilenameParts(f.getName(), ext, true);
117 if (bfp.isBackupFile())
127 public String getExtension(File f)
131 String filename = f.getName();
132 int i = filename.lastIndexOf('.');
134 if ((i > 0) && (i < (filename.length() - 1)))
136 return filename.substring(i + 1).toLowerCase();
145 public void addExtension(String extension)
149 filters = new LinkedHashMap<>(5);
152 filters.put(extension.toLowerCase(), this);
153 fullDescription = null;
157 public String getDescription()
159 if (fullDescription == null)
161 if ((description == null) || isExtensionListInDescription())
163 fullDescription = (description == null) ? "("
164 : (description + " (");
166 // build the description from the extension list
167 Iterator<String> extensions = filters.keySet().iterator();
169 if (extensions != null)
171 fullDescription += ("." + extensions.next());
173 while (extensions.hasNext())
175 fullDescription += (", " + extensions.next());
179 fullDescription += ")";
183 fullDescription = description;
187 return fullDescription;
190 public void setDescription(String description)
192 this.description = description;
193 fullDescription = null;
196 public void setExtensionListInDescription(boolean b)
198 useExtensionsInDescription = b;
199 fullDescription = null;
202 public boolean isExtensionListInDescription()
204 return useExtensionsInDescription;
207 protected void setParentJFC(JalviewFileChooser p)