update author list in license for (JAL-826)
[jalview.git] / src / jalview / io / JalviewFileFilter.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.io;
19
20 import java.io.*;
21 import java.util.*;
22
23 import javax.swing.filechooser.FileFilter;
24
25 public class JalviewFileFilter extends FileFilter
26 {
27   public static Hashtable suffixHash = new Hashtable();
28
29   private Hashtable filters = null;
30
31   private String description = "no description";
32
33   private String fullDescription = "full description";
34
35   private boolean useExtensionsInDescription = true;
36
37   public JalviewFileFilter(String extension, String description)
38   {
39     StringTokenizer st = new StringTokenizer(extension, ",");
40
41     while (st.hasMoreElements())
42     {
43       addExtension(st.nextToken().trim());
44     }
45
46     setDescription(description);
47   }
48
49   public JalviewFileFilter(String[] filts)
50   {
51     this(filts, null);
52   }
53
54   public JalviewFileFilter(String[] filts, String description)
55   {
56     for (int i = 0; i < filts.length; i++)
57     {
58       // add filters one by one
59       addExtension(filts[i]);
60     }
61
62     if (description != null)
63     {
64       setDescription(description);
65     }
66   }
67
68   public String getAcceptableExtension()
69   {
70     return filters.keys().nextElement().toString();
71   }
72
73   // takes account of the fact that database is a directory
74   public boolean accept(File f)
75   {
76     if (f != null)
77     {
78       String extension = getExtension(f);
79
80       if (f.isDirectory())
81       {
82         return true;
83       }
84
85       if ((extension != null) && (filters.get(getExtension(f)) != null))
86       {
87         return true;
88       }
89     }
90
91     return false;
92   }
93
94   public String getExtension(File f)
95   {
96     if (f != null)
97     {
98       String filename = f.getName();
99       int i = filename.lastIndexOf('.');
100
101       if ((i > 0) && (i < (filename.length() - 1)))
102       {
103         return filename.substring(i + 1).toLowerCase();
104       }
105
106       ;
107     }
108
109     return "";
110   }
111
112   public void addExtension(String extension)
113   {
114     if (filters == null)
115     {
116       filters = new Hashtable(5);
117     }
118
119     filters.put(extension.toLowerCase(), this);
120     fullDescription = null;
121   }
122
123   public String getDescription()
124   {
125     if (fullDescription == null)
126     {
127       if ((description == null) || isExtensionListInDescription())
128       {
129         fullDescription = (description == null) ? "("
130                 : (description + " (");
131
132         // build the description from the extension list
133         Enumeration extensions = filters.keys();
134
135         if (extensions != null)
136         {
137           fullDescription += ("." + (String) extensions.nextElement());
138
139           while (extensions.hasMoreElements())
140           {
141             fullDescription += (", " + (String) extensions.nextElement());
142           }
143         }
144
145         fullDescription += ")";
146       }
147       else
148       {
149         fullDescription = description;
150       }
151     }
152
153     return fullDescription;
154   }
155
156   public void setDescription(String description)
157   {
158     this.description = description;
159     fullDescription = null;
160   }
161
162   public void setExtensionListInDescription(boolean b)
163   {
164     useExtensionsInDescription = b;
165     fullDescription = null;
166   }
167
168   public boolean isExtensionListInDescription()
169   {
170     return useExtensionsInDescription;
171   }
172 }