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