package jalview.io;
import java.io.File;
-import java.util.Hashtable;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
import javax.swing.Icon;
import javax.swing.ImageIcon;
public class JalviewFileView extends FileView
{
- static Hashtable<String, String> extensions;
+ private static Map<String, String> extensions;
- static void loadExtensions()
+ private static Map<String, ImageIcon> icons;
+
+ private void loadExtensions()
{
- extensions = new Hashtable<String, String>();
+ extensions = new HashMap<String, String>();
for (FileFormatI ff : FileFormat.values())
{
String desc = ff.toString() + " file";
String exts = ff.getExtensions();
for (String ext : exts.split(","))
{
- extensions.put(ext.trim(), desc
+ extensions.put(ext.trim().toLowerCase(), desc
+ ("jar".equals(ext) ? " (old)" : ""));
}
}
if (getDescriptionForExtension(extension) != null)
{
- icon = createImageIcon("/images/file.png");
+ icon = getImageIcon("/images/file.png");
}
return icon;
}
- /*
- * Get the extension of a file.
+ /**
+ * Returns the extension of a file (part of the name after the last period),
+ * in lower case, or null if the name ends in or does not include a period.
*/
public static String getExtension(File f)
{
*
* @param filePath
*/
- protected static ImageIcon createImageIcon(String filePath)
+ protected ImageIcon getImageIcon(String filePath)
{
- java.net.URL imgURL = JalviewFileView.class.getResource(filePath);
-
- if (imgURL != null)
+ /*
+ * we reuse a single icon object per path here
+ */
+ synchronized (this)
{
- return new ImageIcon(imgURL);
+ if (icons == null)
+ {
+ icons = new HashMap<String, ImageIcon>();
+ }
+ if (!icons.containsKey(filePath))
+ {
+ ImageIcon icon = null;
+ URL imgURL = JalviewFileView.class.getResource(filePath);
+ if (imgURL != null)
+ {
+ icon = new ImageIcon(imgURL);
+ }
+ else
+ {
+ System.err
+ .println("JalviewFileView.createImageIcon: Couldn't find file: "
+ + filePath);
+ }
+ icons.put(filePath, icon);
+ }
}
- else
- {
- System.err
- .println("JalviewFileView.createImageIcon: Couldn't find file: "
- + filePath);
- return null;
- }
+ /*
+ * return the image from the table (which may be null if
+ * icon creation failed)
+ */
+ return icons.get(filePath);
}
}