JAL-3365 expand range of allowed DSSP secondary structure symbols in Stockholm files
[jalview.git] / src / jalview / io / JalviewFileView.java
index 6e04561..a9fc7ed 100755 (executable)
  */
 package jalview.io;
 
+import java.util.Locale;
+
+import jalview.util.MessageManager;
+
 import java.io.File;
-import java.util.Hashtable;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
 
 import javax.swing.Icon;
 import javax.swing.ImageIcon;
@@ -29,19 +36,21 @@ import javax.swing.filechooser.FileView;
 
 public class JalviewFileView extends FileView
 {
-  static Hashtable<String, String> extensions;
+  private static Map<String, String> extensions;
+
+  private static Map<String, ImageIcon> icons;
 
-  static void loadExtensions()
+  private void loadExtensions()
   {
-    extensions = new Hashtable<String, String>();
-    for (FileFormatI ff : FileFormat.values())
+    extensions = new HashMap<>();
+    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
-                + ("jar".equals(ext) ? " (old)" : ""));
+        ext = ext.trim().toLowerCase(Locale.ROOT);
+        extensions.put(ext, desc + ("jar".equals(ext) ? " (old)" : ""));
       }
     }
   }
@@ -50,7 +59,9 @@ public class JalviewFileView extends FileView
   public String getTypeDescription(File f)
   {
     String extension = getExtension(f);
+
     String type = getDescriptionForExtension(extension);
+
     if (extension != null)
     {
       if (extensions.containsKey(extension))
@@ -79,17 +90,44 @@ public class JalviewFileView extends FileView
   {
     String extension = getExtension(f);
     Icon icon = null;
+    String type = getDescriptionForExtension(extension);
+
+    if (type == null)
+    {
+      Iterator<String> it = extensions.keySet().iterator();
+      EXTENSION: while (it.hasNext())
+      {
+        String ext = it.next();
+
+        // quick negative test
+        if (!f.getName().contains(ext))
+        {
+          continue EXTENSION;
+        }
+
+        BackupFilenameParts bfp = BackupFilenameParts
+                .currentBackupFilenameParts(f.getName(), ext, true);
+        if (bfp.isBackupFile())
+        {
+          extension = ext;
+          type = getDescriptionForExtension(extension)
+                  + MessageManager.getString("label.backup");
+          break;
+        }
+      }
+    }
 
-    if (getDescriptionForExtension(extension) != null)
+    if (type != 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)
   {
@@ -99,7 +137,7 @@ public class JalviewFileView extends FileView
 
     if ((i > 0) && (i < (s.length() - 1)))
     {
-      ext = s.substring(i + 1).toLowerCase();
+      ext = s.substring(i + 1).toLowerCase(Locale.ROOT);
     }
 
     return ext;
@@ -110,21 +148,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<>();
+      }
+      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);
   }
 }