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