6e045615a85cca707c3f588914833503f63b72db
[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.util.Hashtable;
25
26 import javax.swing.Icon;
27 import javax.swing.ImageIcon;
28 import javax.swing.filechooser.FileView;
29
30 public class JalviewFileView extends FileView
31 {
32   static Hashtable<String, String> extensions;
33
34   static void loadExtensions()
35   {
36     extensions = new Hashtable<String, String>();
37     for (FileFormatI ff : FileFormat.values())
38     {
39       String desc = ff.toString() + " file";
40       String exts = ff.getExtensions();
41       for (String ext : exts.split(","))
42       {
43         extensions.put(ext.trim(), desc
44                 + ("jar".equals(ext) ? " (old)" : ""));
45       }
46     }
47   }
48
49   @Override
50   public String getTypeDescription(File f)
51   {
52     String extension = getExtension(f);
53     String type = getDescriptionForExtension(extension);
54     if (extension != null)
55     {
56       if (extensions.containsKey(extension))
57       {
58         type = extensions.get(extension).toString();
59       }
60     }
61
62     return type;
63   }
64
65   private String getDescriptionForExtension(String extension)
66   {
67     synchronized (this)
68     {
69       if (extensions == null)
70       {
71         loadExtensions();
72       }
73     }
74     return extensions.get(extension);
75   }
76
77   @Override
78   public Icon getIcon(File f)
79   {
80     String extension = getExtension(f);
81     Icon icon = null;
82
83     if (getDescriptionForExtension(extension) != null)
84     {
85       icon = createImageIcon("/images/file.png");
86     }
87
88     return icon;
89   }
90
91   /*
92    * Get the extension of a file.
93    */
94   public static String getExtension(File f)
95   {
96     String ext = null;
97     String s = f.getName();
98     int i = s.lastIndexOf('.');
99
100     if ((i > 0) && (i < (s.length() - 1)))
101     {
102       ext = s.substring(i + 1).toLowerCase();
103     }
104
105     return ext;
106   }
107
108   /**
109    * Returns an ImageIcon, or null if the file was not found
110    * 
111    * @param filePath
112    */
113   protected static ImageIcon createImageIcon(String filePath)
114   {
115     java.net.URL imgURL = JalviewFileView.class.getResource(filePath);
116
117     if (imgURL != null)
118     {
119       return new ImageIcon(imgURL);
120     }
121     else
122     {
123       System.err
124               .println("JalviewFileView.createImageIcon: Couldn't find file: "
125                       + filePath);
126
127       return null;
128     }
129   }
130 }