applied copyright 2008
[jalview.git] / src / jalview / io / JalviewFileFilter.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.io;
20
21 import java.io.*;
22 import java.util.*;
23
24 import javax.swing.filechooser.FileFilter;
25
26 public class JalviewFileFilter
27     extends FileFilter
28 {
29   public static Hashtable suffixHash = new Hashtable();
30   private Hashtable filters = null;
31   private String description = "no description";
32   private String fullDescription = "full description";
33   private boolean useExtensionsInDescription = true;
34
35   public JalviewFileFilter(String extension, String description)
36   {
37     StringTokenizer st = new StringTokenizer(extension, ",");
38
39     while (st.hasMoreElements())
40     {
41       addExtension(st.nextToken().trim());
42     }
43
44     setDescription(description);
45   }
46
47   public JalviewFileFilter(String[] filts)
48   {
49     this(filts, null);
50   }
51
52   public JalviewFileFilter(String[] filts, String description)
53   {
54     for (int i = 0; i < filts.length; i++)
55     {
56       // add filters one by one
57       addExtension(filts[i]);
58     }
59
60     if (description != null)
61     {
62       setDescription(description);
63     }
64   }
65
66   public String getAcceptableExtension()
67   {
68     return filters.keys().nextElement().toString();
69   }
70
71   // takes account of the fact that database is a directory
72   public boolean accept(File f)
73   {
74     if (f != null)
75     {
76       String extension = getExtension(f);
77
78       if (f.isDirectory())
79       {
80         return true;
81       }
82
83       if ( (extension != null) && (filters.get(getExtension(f)) != null))
84       {
85         return true;
86       }
87     }
88
89     return false;
90   }
91
92   public String getExtension(File f)
93   {
94     if (f != null)
95     {
96       String filename = f.getName();
97       int i = filename.lastIndexOf('.');
98
99       if ( (i > 0) && (i < (filename.length() - 1)))
100       {
101         return filename.substring(i + 1).toLowerCase();
102       }
103
104       ;
105     }
106
107     return "";
108   }
109
110   public void addExtension(String extension)
111   {
112     if (filters == null)
113     {
114       filters = new Hashtable(5);
115     }
116
117     filters.put(extension.toLowerCase(), this);
118     fullDescription = null;
119   }
120
121   public String getDescription()
122   {
123     if (fullDescription == null)
124     {
125       if ( (description == null) || isExtensionListInDescription())
126       {
127         fullDescription = (description == null) ? "(" : (description +
128             " (");
129
130         // build the description from the extension list
131         Enumeration extensions = filters.keys();
132
133         if (extensions != null)
134         {
135           fullDescription += ("." +
136                               (String) extensions.nextElement());
137
138           while (extensions.hasMoreElements())
139           {
140             fullDescription += (", " +
141                                 (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 }