From: gmungoc Date: Fri, 25 Nov 2016 15:35:53 +0000 (+0000) Subject: JAL-2344 dynamically determine recognised extensions for file browser X-Git-Tag: Release_2_10_3b1~402 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=c98ec7d017c2f11909f8344fe5d44d97c7c6fc24;p=jalview.git JAL-2344 dynamically determine recognised extensions for file browser --- diff --git a/src/jalview/io/JalviewFileChooser.java b/src/jalview/io/JalviewFileChooser.java index 079cc09..d571013 100755 --- a/src/jalview/io/JalviewFileChooser.java +++ b/src/jalview/io/JalviewFileChooser.java @@ -399,10 +399,4 @@ public class JalviewFileChooser extends JFileChooser } } - - @Override - public void setFileView(FileView fileView) - { - // do nothing - does it matter? - } } diff --git a/src/jalview/io/JalviewFileView.java b/src/jalview/io/JalviewFileView.java index c42771d..6e04561 100755 --- a/src/jalview/io/JalviewFileView.java +++ b/src/jalview/io/JalviewFileView.java @@ -29,59 +29,60 @@ import javax.swing.filechooser.FileView; public class JalviewFileView extends FileView { - static Hashtable alignSuffix = new Hashtable(); + static Hashtable 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(); + 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; }