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