d59e88a9cf420402b33b73cfb534fa90dd50998a
[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.Enumeration;
25 import java.util.Hashtable;
26 import java.util.StringTokenizer;
27
28 import javax.swing.filechooser.FileFilter;
29
30 public class JalviewFileFilter extends FileFilter
31 {
32   public static Hashtable suffixHash = new Hashtable();
33
34   private Hashtable filters = null;
35
36   private String description = "no description";
37
38   private String fullDescription = "full description";
39
40   private boolean useExtensionsInDescription = true;
41
42   public JalviewFileFilter(String extension, String description)
43   {
44     StringTokenizer st = new StringTokenizer(extension, ",");
45
46     while (st.hasMoreElements())
47     {
48       addExtension(st.nextToken().trim());
49     }
50
51     setDescription(description);
52   }
53
54   public JalviewFileFilter(String[] filts)
55   {
56     this(filts, null);
57   }
58
59   public JalviewFileFilter(String[] filts, String description)
60   {
61     for (int i = 0; i < filts.length; i++)
62     {
63       // add filters one by one
64       addExtension(filts[i]);
65     }
66
67     if (description != null)
68     {
69       setDescription(description);
70     }
71   }
72
73   public String getAcceptableExtension()
74   {
75     return filters.keys().nextElement().toString();
76   }
77
78   // takes account of the fact that database is a directory
79   public boolean accept(File f)
80   {
81     if (f != null)
82     {
83       String extension = getExtension(f);
84
85       if (f.isDirectory())
86       {
87         return true;
88       }
89
90       if ((extension != null) && (filters.get(getExtension(f)) != null))
91       {
92         return true;
93       }
94     }
95
96     return false;
97   }
98
99   public String getExtension(File f)
100   {
101     if (f != null)
102     {
103       String filename = f.getName();
104       int i = filename.lastIndexOf('.');
105
106       if ((i > 0) && (i < (filename.length() - 1)))
107       {
108         return filename.substring(i + 1).toLowerCase();
109       }
110
111       ;
112     }
113
114     return "";
115   }
116
117   public void addExtension(String extension)
118   {
119     if (filters == null)
120     {
121       filters = new Hashtable(5);
122     }
123
124     filters.put(extension.toLowerCase(), this);
125     fullDescription = null;
126   }
127
128   public String getDescription()
129   {
130     if (fullDescription == null)
131     {
132       if ((description == null) || isExtensionListInDescription())
133       {
134         fullDescription = (description == null) ? "("
135                 : (description + " (");
136
137         // build the description from the extension list
138         Enumeration extensions = filters.keys();
139
140         if (extensions != null)
141         {
142           fullDescription += ("." + (String) extensions.nextElement());
143
144           while (extensions.hasMoreElements())
145           {
146             fullDescription += (", " + (String) extensions.nextElement());
147           }
148         }
149
150         fullDescription += ")";
151       }
152       else
153       {
154         fullDescription = description;
155       }
156     }
157
158     return fullDescription;
159   }
160
161   public void setDescription(String description)
162   {
163     this.description = description;
164     fullDescription = null;
165   }
166
167   public void setExtensionListInDescription(boolean b)
168   {
169     useExtensionsInDescription = b;
170     fullDescription = null;
171   }
172
173   public boolean isExtensionListInDescription()
174   {
175     return useExtensionsInDescription;
176   }
177 }