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