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