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 private Map<String, JalviewFileFilter> filters = null;
35 private String description = "no description";
37 private String fullDescription = "full description";
39 private boolean useExtensionsInDescription = true;
41 private JalviewFileChooser parentJFC = null;
43 public JalviewFileFilter(String extension, String description)
45 StringTokenizer st = new StringTokenizer(extension, ",");
47 while (st.hasMoreElements())
49 addExtension(st.nextToken().trim());
52 setDescription(description);
55 public JalviewFileFilter(String[] filts)
60 public JalviewFileFilter(String[] filts, String description)
62 for (int i = 0; i < filts.length; i++)
64 // add filters one by one
65 addExtension(filts[i]);
68 if (description != null)
70 setDescription(description);
74 public String getAcceptableExtension()
76 return filters.keySet().iterator().next().toString();
79 // takes account of the fact that database is a directory
81 public boolean accept(File f)
86 String extension = getExtension(f);
93 if ((extension != null) && (filters.get(extension) != null))
100 if (parentJFC != null && parentJFC.includeBackupFiles)
102 Iterator<String> it = filters.keySet().iterator();
103 EXTENSION: while (it.hasNext())
105 String ext = it.next();
107 // quick negative test
108 if (!f.getName().contains(ext))
113 BackupFilenameParts bfp = BackupFilenameParts
114 .currentBackupFilenameParts(f.getName(), ext, true);
115 if (bfp.isBackupFile())
125 public String getExtension(File f)
129 String filename = f.getName();
130 int i = filename.lastIndexOf('.');
132 if ((i > 0) && (i < (filename.length() - 1)))
134 return filename.substring(i + 1).toLowerCase();
141 public void addExtension(String extension)
145 filters = new LinkedHashMap<>(5);
148 filters.put(extension.toLowerCase(), this);
149 fullDescription = null;
153 public String getDescription()
155 if (fullDescription == null)
157 if ((description == null) || isExtensionListInDescription())
159 fullDescription = (description == null) ? "("
160 : (description + " (");
162 // build the description from the extension list
163 Iterator<String> extensions = filters.keySet().iterator();
165 if (extensions != null)
167 fullDescription += ("." + extensions.next());
169 while (extensions.hasNext())
171 fullDescription += (", " + extensions.next());
175 fullDescription += ")";
179 fullDescription = description;
183 return fullDescription;
186 public void setDescription(String description)
188 this.description = description;
189 fullDescription = null;
192 public void setExtensionListInDescription(boolean b)
194 useExtensionsInDescription = b;
195 fullDescription = null;
198 public boolean isExtensionListInDescription()
200 return useExtensionsInDescription;
203 protected void setParentJFC(JalviewFileChooser p)