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