JAL-2738 copy to spikes/mungo
[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<String, String>();
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         extensions.put(ext.trim().toLowerCase(),
48                 desc + ("jar".equals(ext) ? " (old)" : ""));
49       }
50     }
51   }
52
53   @Override
54   public String getTypeDescription(File f)
55   {
56     String extension = getExtension(f);
57     String type = getDescriptionForExtension(extension);
58     if (extension != null)
59     {
60       if (extensions.containsKey(extension))
61       {
62         type = extensions.get(extension).toString();
63       }
64     }
65
66     return type;
67   }
68
69   private String getDescriptionForExtension(String extension)
70   {
71     synchronized (this)
72     {
73       if (extensions == null)
74       {
75         loadExtensions();
76       }
77     }
78     return extensions.get(extension);
79   }
80
81   @Override
82   public Icon getIcon(File f)
83   {
84     String extension = getExtension(f);
85     Icon icon = null;
86
87     if (getDescriptionForExtension(extension) != null)
88     {
89       icon = getImageIcon("/images/file.png");
90     }
91
92     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   public static String getExtension(File f)
100   {
101     String ext = null;
102     String s = f.getName();
103     int i = s.lastIndexOf('.');
104
105     if ((i > 0) && (i < (s.length() - 1)))
106     {
107       ext = s.substring(i + 1).toLowerCase();
108     }
109
110     return ext;
111   }
112
113   /**
114    * Returns an ImageIcon, or null if the file was not found
115    * 
116    * @param filePath
117    */
118   protected ImageIcon getImageIcon(String filePath)
119   {
120     /*
121      * we reuse a single icon object per path here
122      */
123     synchronized (this)
124     {
125       if (icons == null)
126       {
127         icons = new HashMap<String, ImageIcon>();
128       }
129       if (!icons.containsKey(filePath))
130       {
131         ImageIcon icon = null;
132         URL imgURL = JalviewFileView.class.getResource(filePath);
133         if (imgURL != null)
134         {
135           icon = new ImageIcon(imgURL);
136         }
137         else
138         {
139           System.err.println(
140                   "JalviewFileView.createImageIcon: Couldn't find file: "
141                           + filePath);
142         }
143         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     return icons.get(filePath);
152   }
153 }