Clover icon

jalviewX

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

File JalviewFileView.java

 

Coverage histogram

../../img/srcFileCovDistChart9.png
12% of files have more coverage

Code metrics

18
38
6
1
153
101
16
0.42
6.33
6
2.67

Classes

Class Line # Actions
JalviewFileView 32 38 16 9
0.854838785.5%
 

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