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