Clover icon

Coverage Report

  1. Project Clover database Thu Nov 7 2024 17:01:39 GMT
  2. Package jalview.io

File JalviewFileView.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
0% of files have more coverage

Code metrics

26
51
6
1
186
126
20
0.39
8.5
6
3.33

Classes

Class Line # Actions
JalviewFileView 37 51 20
0.00%
 

Contributing tests

No tests hitting this source file were found.

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