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