JAL-2344 FileFormats singleton for formats, FileFormatI simplified
[jalview.git] / src / jalview / io / JalviewFileView.java
index 6e04561..6bfb3b9 100755 (executable)
@@ -21,7 +21,9 @@
 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;
@@ -29,18 +31,20 @@ import javax.swing.filechooser.FileView;
 
 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>();
-    for (FileFormatI ff : FileFormat.values())
+    extensions = new HashMap<String, String>();
+    for (FileFormatI ff : FileFormats.getInstance().getFormats())
     {
-      String desc = ff.toString() + " file";
+      String desc = ff.getName() + " 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)" : ""));
       }
     }
@@ -82,14 +86,15 @@ public class JalviewFileView extends FileView
 
     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)
   {
@@ -110,21 +115,39 @@ public class JalviewFileView extends FileView
    * 
    * @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);
   }
 }