Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.io

File JalviewFileFilter.java

 

Coverage histogram

../../img/srcFileCovDistChart6.png
33% of files have more coverage

Code metrics

36
54
12
1
213
148
34
0.63
4.5
12
2.83

Classes

Class Line # Actions
JalviewFileFilter 32 54 34
0.588235358.8%
 

Contributing tests

This file is covered by 7 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.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    private JalviewFileChooser parentJFC = null;
45   
 
46  306 toggle public JalviewFileFilter(String extension, String description)
47    {
48  306 StringTokenizer st = new StringTokenizer(extension, ",");
49   
50  756 while (st.hasMoreElements())
51    {
52  450 addExtension(st.nextToken().trim());
53    }
54   
55  306 setDescription(description);
56    }
57   
 
58  0 toggle public JalviewFileFilter(String[] filts)
59    {
60  0 this(filts, null);
61    }
62   
 
63  0 toggle public JalviewFileFilter(String[] filts, String description)
64    {
65  0 for (int i = 0; i < filts.length; i++)
66    {
67    // add filters one by one
68  0 addExtension(filts[i]);
69    }
70   
71  0 if (description != null)
72    {
73  0 setDescription(description);
74    }
75    }
76   
 
77  0 toggle public String getAcceptableExtension()
78    {
79  0 return filters.keySet().iterator().next().toString();
80    }
81   
82    // takes account of the fact that database is a directory
 
83  233 toggle @Override
84    public boolean accept(File f)
85    {
86   
87  233 if (f != null)
88    {
89  233 String extension = getExtension(f);
90   
91  233 if (f.isDirectory())
92    {
93  191 return true;
94    }
95   
96  42 if ((extension != null) && (filters.get(extension) != null))
97    {
98  0 return true;
99    }
100   
101    }
102   
103  42 if (parentJFC != null && parentJFC.includeBackupFiles)
104    {
105  0 Iterator<String> it = filters.keySet().iterator();
106  0 EXTENSION: while (it.hasNext())
107    {
108  0 String ext = it.next();
109   
110    // quick negative test
111  0 if (!f.getName().contains(ext))
112    {
113  0 continue EXTENSION;
114    }
115   
116  0 BackupFilenameParts bfp = BackupFilenameParts
117    .currentBackupFilenameParts(f.getName(), ext, true);
118  0 if (bfp.isBackupFile())
119    {
120  0 return true;
121    }
122    }
123    }
124   
125  42 return false;
126    }
127   
 
128  233 toggle public String getExtension(File f)
129    {
130  233 if (f != null)
131    {
132  233 String filename = f.getName();
133  233 int i = filename.lastIndexOf('.');
134   
135  233 if ((i > 0) && (i < (filename.length() - 1)))
136    {
137  64 return filename.substring(i + 1).toLowerCase();
138    }
139   
140  169 ;
141    }
142   
143  169 return "";
144    }
145   
 
146  450 toggle public void addExtension(String extension)
147    {
148  450 if (filters == null)
149    {
150  306 filters = new LinkedHashMap<>(5);
151    }
152   
153  450 filters.put(extension.toLowerCase(), this);
154  450 fullDescription = null;
155    }
156   
 
157  306 toggle @Override
158    public String getDescription()
159    {
160  306 if (fullDescription == null)
161    {
162  306 if ((description == null) || isExtensionListInDescription())
163    {
164  306 fullDescription = (description == null) ? "("
165    : (description + " (");
166   
167    // build the description from the extension list
168  306 Iterator<String> extensions = filters.keySet().iterator();
169   
170  306 if (extensions != null)
171    {
172  306 fullDescription += ("." + extensions.next());
173   
174  450 while (extensions.hasNext())
175    {
176  144 fullDescription += (", " + extensions.next());
177    }
178    }
179   
180  306 fullDescription += ")";
181    }
182    else
183    {
184  0 fullDescription = description;
185    }
186    }
187   
188  306 return fullDescription;
189    }
190   
 
191  306 toggle public void setDescription(String description)
192    {
193  306 this.description = description;
194  306 fullDescription = null;
195    }
196   
 
197  0 toggle public void setExtensionListInDescription(boolean b)
198    {
199  0 useExtensionsInDescription = b;
200  0 fullDescription = null;
201    }
202   
 
203  306 toggle public boolean isExtensionListInDescription()
204    {
205  306 return useExtensionsInDescription;
206    }
207   
 
208  0 toggle protected void setParentJFC(JalviewFileChooser p)
209    {
210  0 this.parentJFC = p;
211    }
212   
213    }