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