Clover icon

Coverage Report

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

File JalviewFileView.java

 

Coverage histogram

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

Code metrics

26
51
6
1
185
126
20
0.39
8.5
6
3.33

Classes

Class Line # Actions
JalviewFileView 35 51 20
0.650602465.1%
 

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 jalview.util.MessageManager;
24   
25    import java.io.File;
26    import java.net.URL;
27    import java.util.HashMap;
28    import java.util.Iterator;
29    import java.util.Map;
30   
31    import javax.swing.Icon;
32    import javax.swing.ImageIcon;
33    import javax.swing.filechooser.FileView;
34   
 
35    public class JalviewFileView extends FileView
36    {
37    private static Map<String, String> extensions;
38   
39    private static Map<String, ImageIcon> icons;
40   
 
41  1 toggle private void loadExtensions()
42    {
43  1 extensions = new HashMap<>();
44  1 for (FileFormatI ff : FileFormats.getInstance().getFormats())
45    {
46  19 String desc = ff.getName() + " file";
47  19 String exts = ff.getExtensions();
48  19 for (String ext : exts.split(","))
49    {
50  27 ext = ext.trim().toLowerCase();
51  27 extensions.put(ext,
52  27 desc + ("jar".equals(ext) ? " (old)" : ""));
53    }
54    }
55    }
56   
 
57  26 toggle @Override
58    public String getTypeDescription(File f)
59    {
60  26 String extension = getExtension(f);
61   
62  26 String type = getDescriptionForExtension(extension);
63   
64  26 if (extension != null)
65    {
66  26 if (extensions.containsKey(extension))
67    {
68  25 type = extensions.get(extension).toString();
69    }
70    }
71   
72  26 return type;
73    }
74   
 
75  26 toggle private String getDescriptionForExtension(String extension)
76    {
77  26 synchronized (this)
78    {
79  26 if (extensions == null)
80    {
81  1 loadExtensions();
82    }
83    }
84  26 return extensions.get(extension);
85    }
86   
 
87  0 toggle @Override
88    public Icon getIcon(File f)
89    {
90  0 String extension = getExtension(f);
91  0 Icon icon = null;
92  0 String type = getDescriptionForExtension(extension);
93   
94  0 if (type == null)
95    {
96  0 Iterator<String> it = extensions.keySet().iterator();
97  0 EXTENSION: while (it.hasNext())
98    {
99  0 String ext = it.next();
100   
101    // quick negative test
102  0 if (!f.getName().contains(ext))
103    {
104  0 continue EXTENSION;
105    }
106   
107  0 BackupFilenameParts bfp = BackupFilenameParts
108    .currentBackupFilenameParts(f.getName(), ext, true);
109  0 if (bfp.isBackupFile())
110    {
111  0 extension = ext;
112  0 type = getDescriptionForExtension(extension)
113    + MessageManager.getString("label.backup");
114  0 break;
115    }
116    }
117    }
118   
119  0 if (type != null)
120    {
121  0 icon = getImageIcon("/images/file.png");
122    }
123   
124  0 return icon;
125    }
126   
127    /**
128    * Returns the extension of a file (part of the name after the last period),
129    * in lower case, or null if the name ends in or does not include a period.
130    */
 
131  30 toggle public static String getExtension(File f)
132    {
133  30 String ext = null;
134  30 String s = f.getName();
135  30 int i = s.lastIndexOf('.');
136   
137  30 if ((i > 0) && (i < (s.length() - 1)))
138    {
139  28 ext = s.substring(i + 1).toLowerCase();
140    }
141   
142  30 return ext;
143    }
144   
145    /**
146    * Returns an ImageIcon, or null if the file was not found
147    *
148    * @param filePath
149    */
 
150  6 toggle protected ImageIcon getImageIcon(String filePath)
151    {
152    /*
153    * we reuse a single icon object per path here
154    */
155  6 synchronized (this)
156    {
157  6 if (icons == null)
158    {
159  1 icons = new HashMap<>();
160    }
161  6 if (!icons.containsKey(filePath))
162    {
163  4 ImageIcon icon = null;
164  4 URL imgURL = JalviewFileView.class.getResource(filePath);
165  4 if (imgURL != null)
166    {
167  2 icon = new ImageIcon(imgURL);
168    }
169    else
170    {
171  2 System.err.println(
172    "JalviewFileView.createImageIcon: Couldn't find file: "
173    + filePath);
174    }
175  4 icons.put(filePath, icon);
176    }
177    }
178   
179    /*
180    * return the image from the table (which may be null if
181    * icon creation failed)
182    */
183  6 return icons.get(filePath);
184    }
185    }