JAL-1517 update copyright to version 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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
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 extends FileView
28 {
29   static Hashtable alignSuffix = new Hashtable();
30
31   static
32   {
33     // TODO: these names should come from the FormatAdapter lists for readable/writable extensions 
34     alignSuffix.put("amsa", "AMSA file");
35     alignSuffix.put("fasta", "Fasta file");
36     alignSuffix.put("fa", "Fasta file");
37     alignSuffix.put("fastq", "Fasta file");
38     alignSuffix.put("mfa", "Fasta file");
39     alignSuffix.put("blc", "BLC file");
40     alignSuffix.put("msf", "MSF file");
41     alignSuffix.put("pfam", "PFAM file");
42     alignSuffix.put("aln", "Clustal file");
43     alignSuffix.put("pir", "PIR file");
44     alignSuffix.put("jar", "Jalview Project file (old)");
45     alignSuffix.put("jvp", "Jalview Project file");
46     alignSuffix.put("amsa", "AMSA file");
47     alignSuffix.put("sto", "Stockholm File");
48     alignSuffix.put("stk", "Stockholm File");
49     alignSuffix.put("sto", "Stockholm File");
50   }
51
52   public String getTypeDescription(File f)
53   {
54     String extension = getExtension(f);
55     String type = null;
56
57     if (extension != null)
58     {
59       if (alignSuffix.containsKey(extension))
60       {
61         type = alignSuffix.get(extension).toString();
62       }
63     }
64
65     return type;
66   }
67
68   public Icon getIcon(File f)
69   {
70     String extension = getExtension(f);
71     Icon icon = null;
72
73     if (extension != null)
74     {
75       if (alignSuffix.containsKey(extension))
76       {
77         icon = createImageIcon("/images/file.png");
78       }
79     }
80
81     return icon;
82   }
83
84   /*
85    * Get the extension of a file.
86    */
87   public static String getExtension(File f)
88   {
89     String ext = null;
90     String s = f.getName();
91     int i = s.lastIndexOf('.');
92
93     if ((i > 0) && (i < (s.length() - 1)))
94     {
95       ext = s.substring(i + 1).toLowerCase();
96     }
97
98     return ext;
99   }
100
101   /** Returns an ImageIcon, or null if the path was invalid. */
102   protected static ImageIcon createImageIcon(String path)
103   {
104     java.net.URL imgURL = JalviewFileView.class.getResource(path);
105
106     if (imgURL != null)
107     {
108       return new ImageIcon(imgURL);
109     }
110     else
111     {
112       System.err
113               .println("JalviewFileView.createImageIcon: Couldn't find file: "
114                       + path);
115
116       return null;
117     }
118   }
119 }