new class
[jalview.git] / src / jalview / io / JalviewFileView.java
1 package jalview.io;\r
2 \r
3 import javax.swing.filechooser.FileView;\r
4 import javax.swing.*;\r
5 import java.io.*;\r
6 \r
7 public class JalviewFileView extends FileView\r
8 {\r
9   public String getTypeDescription(File f)\r
10   {\r
11     String extension = getExtension(f);\r
12     String type = null;\r
13 \r
14     if (extension != null)\r
15     {\r
16       if (extension.equals("fasta") || extension.equals("fa") || extension.equals("fastq"))\r
17       {\r
18         type = "Fasta file";\r
19       }\r
20     }\r
21     return type;\r
22   }\r
23 \r
24   public Icon getIcon(File f)\r
25   {\r
26     String extension = getExtension(f);\r
27     Icon icon = null;\r
28 \r
29     if (extension != null)\r
30     {\r
31       if (extension.equals("fasta") || extension.equals("fa") || extension.equals("fastq"))\r
32       {\r
33         icon = createImageIcon("/images/file.png");\r
34       }\r
35     }\r
36     return icon;\r
37   }\r
38 \r
39 \r
40   /*\r
41    * Get the extension of a file.\r
42    */\r
43   public static String getExtension(File f) {\r
44       String ext = null;\r
45       String s = f.getName();\r
46       int i = s.lastIndexOf('.');\r
47 \r
48       if (i > 0 &&  i < s.length() - 1) {\r
49           ext = s.substring(i+1).toLowerCase();\r
50       }\r
51       return ext;\r
52   }\r
53 \r
54   /** Returns an ImageIcon, or null if the path was invalid. */\r
55   protected static ImageIcon createImageIcon(String path) {\r
56       java.net.URL imgURL = JalviewFileView.class.getResource(path);\r
57       if (imgURL != null) {\r
58           return new ImageIcon(imgURL);\r
59       } else {\r
60           System.err.println("Couldn't find file: " + path);\r
61           return null;\r
62       }\r
63   }\r
64 \r
65 }\r
66 \r
67 \r