a702aaed8a65ab7d6f86b2af2ff0dc7aa995a5f7
[jalview.git] / src / jalview / io / JalviewFileFilter.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.io;
22
23 import java.io.File;
24 import java.util.Iterator;
25 import java.util.LinkedHashMap;
26 import java.util.Map;
27 import java.util.StringTokenizer;
28
29 import javax.swing.filechooser.FileFilter;
30
31 public class JalviewFileFilter extends FileFilter
32 {
33   // public static Hashtable suffixHash = new Hashtable();
34
35   private Map<String, JalviewFileFilter> filters = null;
36
37   private String description = "no description";
38
39   private String fullDescription = "full description";
40
41   private boolean useExtensionsInDescription = true;
42
43   private JalviewFileChooser parentJFC = null;
44
45   public JalviewFileFilter(String extension, String description)
46   {
47     StringTokenizer st = new StringTokenizer(extension, ",");
48
49     while (st.hasMoreElements())
50     {
51       addExtension(st.nextToken().trim());
52     }
53
54     setDescription(description);
55   }
56
57   public JalviewFileFilter(String[] filts)
58   {
59     this(filts, null);
60   }
61
62   public JalviewFileFilter(String[] filts, String description)
63   {
64     for (int i = 0; i < filts.length; i++)
65     {
66       // add filters one by one
67       addExtension(filts[i]);
68     }
69
70     if (description != null)
71     {
72       setDescription(description);
73     }
74   }
75
76   public String getAcceptableExtension()
77   {
78     return filters.keySet().iterator().next().toString();
79   }
80
81   // takes account of the fact that database is a directory
82   @Override
83   public boolean accept(File f)
84   {
85
86     if (f != null)
87     {
88       String extension = getExtension(f);
89
90       if (f.isDirectory())
91       {
92         return true;
93       }
94
95       if ((extension != null) && (filters.get(extension) != null))
96       {
97         return true;
98       }
99
100     }
101
102     if (parentJFC != null && parentJFC.includeBackupFiles)
103     {
104       Iterator<String> it = filters.keySet().iterator();
105       EXTENSION: while (it.hasNext())
106       {
107         String ext = it.next();
108
109         // quick negative test
110         if (!f.getName().contains(ext))
111         {
112           continue EXTENSION;
113         }
114
115         BackupFilenameParts bfp = BackupFilenameParts
116                 .currentBackupFilenameParts(f.getName(), ext, true);
117         if (bfp.isBackupFile())
118         {
119           return true;
120         }
121       }
122     }
123     
124     return false;
125   }
126
127   public String getExtension(File f)
128   {
129     if (f != null)
130     {
131       String filename = f.getName();
132       int i = filename.lastIndexOf('.');
133
134       if ((i > 0) && (i < (filename.length() - 1)))
135       {
136         return filename.substring(i + 1).toLowerCase();
137       }
138
139       ;
140     }
141
142     return "";
143   }
144
145   public void addExtension(String extension)
146   {
147     if (filters == null)
148     {
149       filters = new LinkedHashMap<>(5);
150     }
151
152     filters.put(extension.toLowerCase(), this);
153     fullDescription = null;
154   }
155
156   @Override
157   public String getDescription()
158   {
159     if (fullDescription == null)
160     {
161       if ((description == null) || isExtensionListInDescription())
162       {
163         fullDescription = (description == null) ? "("
164                 : (description + " (");
165
166         // build the description from the extension list
167         Iterator<String> extensions = filters.keySet().iterator();
168
169         if (extensions != null)
170         {
171           fullDescription += ("." + extensions.next());
172
173           while (extensions.hasNext())
174           {
175             fullDescription += (", " + extensions.next());
176           }
177         }
178
179         fullDescription += ")";
180       }
181       else
182       {
183         fullDescription = description;
184       }
185     }
186
187     return fullDescription;
188   }
189
190   public void setDescription(String description)
191   {
192     this.description = description;
193     fullDescription = null;
194   }
195
196   public void setExtensionListInDescription(boolean b)
197   {
198     useExtensionsInDescription = b;
199     fullDescription = null;
200   }
201
202   public boolean isExtensionListInDescription()
203   {
204     return useExtensionsInDescription;
205   }
206
207   protected void setParentJFC(JalviewFileChooser p)
208   {
209     this.parentJFC = p;
210   }
211
212 }