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