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