JAL-3141. This adds quite a lot of code to allow an 'Include backup files' checkbox...
[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       while (it.hasNext())
107       {
108         String ext = it.next();
109
110         BackupFilenameParts bfp = BackupFilenameParts
111                 .currentBackupFilenameParts(f.getName(), ext, true);
112         if (bfp.isBackupFile())
113         {
114           return true;
115         }
116       }
117     }
118     
119     return false;
120   }
121
122   public String getExtension(File f)
123   {
124     if (f != null)
125     {
126       String filename = f.getName();
127       int i = filename.lastIndexOf('.');
128
129       if ((i > 0) && (i < (filename.length() - 1)))
130       {
131         return filename.substring(i + 1).toLowerCase();
132       }
133
134       ;
135     }
136
137     return "";
138   }
139
140   public void addExtension(String extension)
141   {
142     if (filters == null)
143     {
144       filters = new LinkedHashMap<>(5);
145     }
146
147     filters.put(extension.toLowerCase(), this);
148     fullDescription = null;
149   }
150
151   @Override
152   public String getDescription()
153   {
154     if (fullDescription == null)
155     {
156       if ((description == null) || isExtensionListInDescription())
157       {
158         fullDescription = (description == null) ? "("
159                 : (description + " (");
160
161         // build the description from the extension list
162         Iterator<String> extensions = filters.keySet().iterator();
163
164         if (extensions != null)
165         {
166           fullDescription += ("." + extensions.next());
167
168           while (extensions.hasNext())
169           {
170             fullDescription += (", " + extensions.next());
171           }
172         }
173
174         fullDescription += ")";
175       }
176       else
177       {
178         fullDescription = description;
179       }
180     }
181
182     return fullDescription;
183   }
184
185   public void setDescription(String description)
186   {
187     this.description = description;
188     fullDescription = null;
189   }
190
191   public void setExtensionListInDescription(boolean b)
192   {
193     useExtensionsInDescription = b;
194     fullDescription = null;
195   }
196
197   public boolean isExtensionListInDescription()
198   {
199     return useExtensionsInDescription;
200   }
201
202   protected void setParentJFC(JalviewFileChooser p)
203   {
204     this.parentJFC = p;
205   }
206
207 }