JAL-2344 dynamically determine recognised extensions for file browser
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 25 Nov 2016 15:35:53 +0000 (15:35 +0000)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 25 Nov 2016 15:35:53 +0000 (15:35 +0000)
src/jalview/io/JalviewFileChooser.java
src/jalview/io/JalviewFileView.java

index 079cc09..d571013 100755 (executable)
@@ -399,10 +399,4 @@ public class JalviewFileChooser extends JFileChooser
     }
 
   }
-
-  @Override
-  public void setFileView(FileView fileView)
-  {
-    // do nothing - does it matter?
-  }
 }
index c42771d..6e04561 100755 (executable)
@@ -29,59 +29,60 @@ import javax.swing.filechooser.FileView;
 
 public class JalviewFileView extends FileView
 {
-  static Hashtable alignSuffix = new Hashtable();
+  static Hashtable<String, String> extensions;
 
-  static
+  static void loadExtensions()
   {
-    // TODO: these names should come from the FormatAdapter lists for
-    // readable/writable extensions
-    alignSuffix.put("amsa", "AMSA file");
-    alignSuffix.put("fasta", "Fasta file");
-    alignSuffix.put("fa", "Fasta file");
-    alignSuffix.put("fastq", "Fasta file");
-    alignSuffix.put("mfa", "Fasta file");
-    alignSuffix.put("blc", "BLC file");
-    alignSuffix.put("msf", "MSF file");
-    alignSuffix.put("pfam", "PFAM file");
-    alignSuffix.put("aln", "Clustal file");
-    alignSuffix.put("pir", "PIR file");
-    alignSuffix.put("jar", "Jalview Project file (old)");
-    alignSuffix.put("jvp", "Jalview Project file");
-    alignSuffix.put("amsa", "AMSA file");
-    alignSuffix.put("sto", "Stockholm File");
-    alignSuffix.put("stk", "Stockholm File");
-    alignSuffix.put("sto", "Stockholm File");
+    extensions = new Hashtable<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
+                + ("jar".equals(ext) ? " (old)" : ""));
+      }
+    }
   }
 
   @Override
   public String getTypeDescription(File f)
   {
     String extension = getExtension(f);
-    String type = null;
-
+    String type = getDescriptionForExtension(extension);
     if (extension != null)
     {
-      if (alignSuffix.containsKey(extension))
+      if (extensions.containsKey(extension))
       {
-        type = alignSuffix.get(extension).toString();
+        type = extensions.get(extension).toString();
       }
     }
 
     return type;
   }
 
+  private String getDescriptionForExtension(String extension)
+  {
+    synchronized (this)
+    {
+      if (extensions == null)
+      {
+        loadExtensions();
+      }
+    }
+    return extensions.get(extension);
+  }
+
   @Override
   public Icon getIcon(File f)
   {
     String extension = getExtension(f);
     Icon icon = null;
 
-    if (extension != null)
+    if (getDescriptionForExtension(extension) != null)
     {
-      if (alignSuffix.containsKey(extension))
-      {
-        icon = createImageIcon("/images/file.png");
-      }
+      icon = createImageIcon("/images/file.png");
     }
 
     return icon;
@@ -104,10 +105,14 @@ public class JalviewFileView extends FileView
     return ext;
   }
 
-  /** Returns an ImageIcon, or null if the path was invalid. */
-  protected static ImageIcon createImageIcon(String path)
+  /**
+   * Returns an ImageIcon, or null if the file was not found
+   * 
+   * @param filePath
+   */
+  protected static ImageIcon createImageIcon(String filePath)
   {
-    java.net.URL imgURL = JalviewFileView.class.getResource(path);
+    java.net.URL imgURL = JalviewFileView.class.getResource(filePath);
 
     if (imgURL != null)
     {
@@ -117,7 +122,7 @@ public class JalviewFileView extends FileView
     {
       System.err
               .println("JalviewFileView.createImageIcon: Couldn't find file: "
-                      + path);
+                      + filePath);
 
       return null;
     }