b90bd7aca13d8626fa6a685d1413d6bd330f86ff
[jalview.git] / src / jalview / io / JalviewFileView.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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.util.Locale;
24
25 import jalview.util.MessageManager;
26
27 import java.io.File;
28 import java.net.URL;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.Map;
32
33 import javax.swing.Icon;
34 import javax.swing.ImageIcon;
35 import javax.swing.filechooser.FileView;
36
37 public class JalviewFileView extends FileView
38 {
39   private static Map<String, String> extensions;
40
41   private static Map<String, ImageIcon> icons;
42
43   private void loadExtensions()
44   {
45     extensions = new HashMap<>();
46     for (FileFormatI ff : FileFormats.getInstance().getFormats())
47     {
48       String desc = ff.getName() + " file";
49       String exts = ff.getExtensions();
50       for (String ext : exts.split(","))
51       {
52         ext = ext.trim().toLowerCase(Locale.ROOT);
53         extensions.put(ext,
54                 desc + ("jar".equals(ext) ? " (old)" : ""));
55       }
56     }
57   }
58
59   @Override
60   public String getTypeDescription(File f)
61   {
62     String extension = getExtension(f);
63     
64     String type = getDescriptionForExtension(extension);
65     
66     if (extension != null)
67     {
68       if (extensions.containsKey(extension))
69       {
70         type = extensions.get(extension).toString();
71       }
72     }
73
74     return type;
75   }
76
77   private String getDescriptionForExtension(String extension)
78   {
79     synchronized (this)
80     {
81       if (extensions == null)
82       {
83         loadExtensions();
84       }
85     }
86     return extensions.get(extension);
87   }
88
89   @Override
90   public Icon getIcon(File f)
91   {
92     String extension = getExtension(f);
93     Icon icon = null;
94     String type = getDescriptionForExtension(extension);
95
96     if (type == null)
97     {
98       Iterator<String> it = extensions.keySet().iterator();
99       EXTENSION: while (it.hasNext())
100       {
101         String ext = it.next();
102
103         // quick negative test
104         if (!f.getName().contains(ext))
105         {
106           continue EXTENSION;
107         }
108
109         BackupFilenameParts bfp = BackupFilenameParts
110                 .currentBackupFilenameParts(f.getName(), ext, true);
111         if (bfp.isBackupFile())
112         {
113           extension = ext;
114           type = getDescriptionForExtension(extension)
115                   + MessageManager.getString("label.backup");
116           break;
117         }
118       }
119     }
120
121     if (type != null)
122     {
123       icon = getImageIcon("/images/file.png");
124     }
125
126     return icon;
127   }
128
129   /**
130    * Returns the extension of a file (part of the name after the last period),
131    * in lower case, or null if the name ends in or does not include a period.
132    */
133   public static String getExtension(File f)
134   {
135     String ext = null;
136     String s = f.getName();
137     int i = s.lastIndexOf('.');
138
139     if ((i > 0) && (i < (s.length() - 1)))
140     {
141       ext = s.substring(i + 1).toLowerCase(Locale.ROOT);
142     }
143
144     return ext;
145   }
146
147   /**
148    * Returns an ImageIcon, or null if the file was not found
149    * 
150    * @param filePath
151    */
152   protected ImageIcon getImageIcon(String filePath)
153   {
154     /*
155      * we reuse a single icon object per path here
156      */
157     synchronized (this)
158     {
159       if (icons == null)
160       {
161         icons = new HashMap<>();
162       }
163       if (!icons.containsKey(filePath))
164       {
165         ImageIcon icon = null;
166         URL imgURL = JalviewFileView.class.getResource(filePath);
167         if (imgURL != null)
168         {
169           icon = new ImageIcon(imgURL);
170         }
171         else
172         {
173           System.err.println(
174                   "JalviewFileView.createImageIcon: Couldn't find file: "
175                           + filePath);
176         }
177         icons.put(filePath, icon);
178       }
179     }
180
181     /*
182      * return the image from the table (which may be null if
183      * icon creation failed)
184      */
185     return icons.get(filePath);
186   }
187 }