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