Merge branch 'feature/JAL-3181linkOrdering' into develop
[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       EXTENSION: while (it.hasNext())
98       {
99         String ext = it.next();
100
101         // quick negative test
102         if (!f.getName().contains(ext))
103         {
104           continue EXTENSION;
105         }
106
107         BackupFilenameParts bfp = BackupFilenameParts
108                 .currentBackupFilenameParts(f.getName(), ext, true);
109         if (bfp.isBackupFile())
110         {
111           extension = ext;
112           type = getDescriptionForExtension(extension)
113                   + MessageManager.getString("label.backup");
114           break;
115         }
116       }
117     }
118
119     if (type != null)
120     {
121       icon = getImageIcon("/images/file.png");
122     }
123
124     return icon;
125   }
126
127   /**
128    * Returns the extension of a file (part of the name after the last period),
129    * in lower case, or null if the name ends in or does not include a period.
130    */
131   public static String getExtension(File f)
132   {
133     String ext = null;
134     String s = f.getName();
135     int i = s.lastIndexOf('.');
136
137     if ((i > 0) && (i < (s.length() - 1)))
138     {
139       ext = s.substring(i + 1).toLowerCase();
140     }
141
142     return ext;
143   }
144
145   /**
146    * Returns an ImageIcon, or null if the file was not found
147    * 
148    * @param filePath
149    */
150   protected ImageIcon getImageIcon(String filePath)
151   {
152     /*
153      * we reuse a single icon object per path here
154      */
155     synchronized (this)
156     {
157       if (icons == null)
158       {
159         icons = new HashMap<>();
160       }
161       if (!icons.containsKey(filePath))
162       {
163         ImageIcon icon = null;
164         URL imgURL = JalviewFileView.class.getResource(filePath);
165         if (imgURL != null)
166         {
167           icon = new ImageIcon(imgURL);
168         }
169         else
170         {
171           System.err.println(
172                   "JalviewFileView.createImageIcon: Couldn't find file: "
173                           + filePath);
174         }
175         icons.put(filePath, icon);
176       }
177     }
178
179     /*
180      * return the image from the table (which may be null if
181      * icon creation failed)
182      */
183     return icons.get(filePath);
184   }
185 }