Formatting
[jalview.git] / src / jalview / io / JalviewFileView.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.io;\r
20 \r
21 import java.io.*;\r
22 import java.util.*;\r
23 \r
24 import javax.swing.*;\r
25 import javax.swing.filechooser.*;\r
26 \r
27 public class JalviewFileView\r
28     extends FileView\r
29 {\r
30   static Hashtable alignSuffix = new Hashtable();\r
31 \r
32   static\r
33   {\r
34     alignSuffix.put("fasta", "Fasta file");\r
35     alignSuffix.put("fa", "Fasta file");\r
36     alignSuffix.put("fastq", "Fasta file");\r
37     alignSuffix.put("blc", "BLC file");\r
38     alignSuffix.put("msf", "MSF file");\r
39     alignSuffix.put("pfam", "PFAM file");\r
40     alignSuffix.put("aln", "Clustal file");\r
41     alignSuffix.put("pir", "PIR file");\r
42     alignSuffix.put("jar", "Jalview file");\r
43   }\r
44 \r
45   public String getTypeDescription(File f)\r
46   {\r
47     String extension = getExtension(f);\r
48     String type = null;\r
49 \r
50     if (extension != null)\r
51     {\r
52       if (alignSuffix.containsKey(extension))\r
53       {\r
54         type = alignSuffix.get(extension).toString();\r
55       }\r
56     }\r
57 \r
58     return type;\r
59   }\r
60 \r
61   public Icon getIcon(File f)\r
62   {\r
63     String extension = getExtension(f);\r
64     Icon icon = null;\r
65 \r
66     if (extension != null)\r
67     {\r
68       if (alignSuffix.containsKey(extension))\r
69       {\r
70         icon = createImageIcon("/images/file.png");\r
71       }\r
72     }\r
73 \r
74     return icon;\r
75   }\r
76 \r
77   /*\r
78    * Get the extension of a file.\r
79    */\r
80   public static String getExtension(File f)\r
81   {\r
82     String ext = null;\r
83     String s = f.getName();\r
84     int i = s.lastIndexOf('.');\r
85 \r
86     if ( (i > 0) && (i < (s.length() - 1)))\r
87     {\r
88       ext = s.substring(i + 1).toLowerCase();\r
89     }\r
90 \r
91     return ext;\r
92   }\r
93 \r
94   /** Returns an ImageIcon, or null if the path was invalid. */\r
95   protected static ImageIcon createImageIcon(String path)\r
96   {\r
97     java.net.URL imgURL = JalviewFileView.class.getResource(path);\r
98 \r
99     if (imgURL != null)\r
100     {\r
101       return new ImageIcon(imgURL);\r
102     }\r
103     else\r
104     {\r
105       System.err.println(\r
106           "JalviewFileView.createImageIcon: Couldn't find file: " + path);\r
107 \r
108       return null;\r
109     }\r
110   }\r
111 }\r