c42771deae1d766665245bba429c9b75d808c1b6
[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 alignSuffix = new Hashtable();
33
34   static
35   {
36     // TODO: these names should come from the FormatAdapter lists for
37     // readable/writable extensions
38     alignSuffix.put("amsa", "AMSA file");
39     alignSuffix.put("fasta", "Fasta file");
40     alignSuffix.put("fa", "Fasta file");
41     alignSuffix.put("fastq", "Fasta file");
42     alignSuffix.put("mfa", "Fasta file");
43     alignSuffix.put("blc", "BLC file");
44     alignSuffix.put("msf", "MSF file");
45     alignSuffix.put("pfam", "PFAM file");
46     alignSuffix.put("aln", "Clustal file");
47     alignSuffix.put("pir", "PIR file");
48     alignSuffix.put("jar", "Jalview Project file (old)");
49     alignSuffix.put("jvp", "Jalview Project file");
50     alignSuffix.put("amsa", "AMSA file");
51     alignSuffix.put("sto", "Stockholm File");
52     alignSuffix.put("stk", "Stockholm File");
53     alignSuffix.put("sto", "Stockholm File");
54   }
55
56   @Override
57   public String getTypeDescription(File f)
58   {
59     String extension = getExtension(f);
60     String type = null;
61
62     if (extension != null)
63     {
64       if (alignSuffix.containsKey(extension))
65       {
66         type = alignSuffix.get(extension).toString();
67       }
68     }
69
70     return type;
71   }
72
73   @Override
74   public Icon getIcon(File f)
75   {
76     String extension = getExtension(f);
77     Icon icon = null;
78
79     if (extension != null)
80     {
81       if (alignSuffix.containsKey(extension))
82       {
83         icon = createImageIcon("/images/file.png");
84       }
85     }
86
87     return icon;
88   }
89
90   /*
91    * Get the extension of a file.
92    */
93   public static String getExtension(File f)
94   {
95     String ext = null;
96     String s = f.getName();
97     int i = s.lastIndexOf('.');
98
99     if ((i > 0) && (i < (s.length() - 1)))
100     {
101       ext = s.substring(i + 1).toLowerCase();
102     }
103
104     return ext;
105   }
106
107   /** Returns an ImageIcon, or null if the path was invalid. */
108   protected static ImageIcon createImageIcon(String path)
109   {
110     java.net.URL imgURL = JalviewFileView.class.getResource(path);
111
112     if (imgURL != null)
113     {
114       return new ImageIcon(imgURL);
115     }
116     else
117     {
118       System.err
119               .println("JalviewFileView.createImageIcon: Couldn't find file: "
120                       + path);
121
122       return null;
123     }
124   }
125 }