Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.io

File JalviewFileFilter.java

 

Coverage histogram

../../img/srcFileCovDistChart7.png
28% of files have more coverage

Code metrics

28
44
11
1
177
121
28
0.64
4
11
2.55

Classes

Class Line # Actions
JalviewFileFilter 30 44 28 25
0.698795269.9%
 

Contributing tests

This file is covered by 3 tests. .

Source view

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