From 6a2cb2371d69d60b9b607cc6d2fdae16bc19f61e Mon Sep 17 00:00:00 2001 From: gmungoc Date: Mon, 28 Nov 2016 14:24:37 +0000 Subject: [PATCH] JAL-2345 cache generated ImageIcon per file path --- src/jalview/io/JalviewFileView.java | 63 ++++++++++++------ test/jalview/io/JalviewFileViewTest.java | 107 ++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+), 20 deletions(-) create mode 100644 test/jalview/io/JalviewFileViewTest.java diff --git a/src/jalview/io/JalviewFileView.java b/src/jalview/io/JalviewFileView.java index 6e04561..3461110 100755 --- a/src/jalview/io/JalviewFileView.java +++ b/src/jalview/io/JalviewFileView.java @@ -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 extensions; + private static Map extensions; - static void loadExtensions() + private static Map icons; + + private void loadExtensions() { - extensions = new Hashtable(); + extensions = new HashMap(); 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)" : "")); } } @@ -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(); + } + 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); } } diff --git a/test/jalview/io/JalviewFileViewTest.java b/test/jalview/io/JalviewFileViewTest.java new file mode 100644 index 0000000..d867e5e --- /dev/null +++ b/test/jalview/io/JalviewFileViewTest.java @@ -0,0 +1,107 @@ +package jalview.io; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNotSame; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertSame; + +import java.io.File; + +import javax.swing.ImageIcon; + +import org.testng.annotations.Test; + +public class JalviewFileViewTest +{ + @Test + public void testGetImageIcon() + { + JalviewFileView jfv = new JalviewFileView(); + ImageIcon icon1 = jfv.getImageIcon("/images/file.png"); + ImageIcon icon2 = jfv.getImageIcon("/images/file.png"); + ImageIcon icon3 = jfv.getImageIcon("/images/dna.png"); + ImageIcon icon4 = jfv.getImageIcon("/images/dna.png"); + + /* + * verify a single image object is served per file path + */ + assertNotNull(icon1); + assertSame(icon1, icon2); + assertNotNull(icon3); + assertSame(icon3, icon4); + assertNotSame(icon1, icon3); + + assertNull(jfv.getImageIcon("/images/nosuchfile.png")); + assertNull(jfv.getImageIcon("images/file.png")); + } + + @Test + public void testGetExtension() + { + assertEquals(JalviewFileView.getExtension(new File("text.txt")), "txt"); + assertEquals(JalviewFileView.getExtension(new File( + "/a/longer/file/path/text.png.TXT")), "txt"); + assertNull(JalviewFileView.getExtension(new File( + "/a/longer/file/path/text."))); + assertNull(JalviewFileView.getExtension(new File( + "/a/longer/file/path/text"))); + } + + @Test + public void testGetTypeDescription() + { + JalviewFileView jfw = new JalviewFileView(); + assertEquals(jfw.getTypeDescription(new File("uniref50.fa")), + "Fasta file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.fasta")), + "Fasta file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.MFA")), + "Fasta file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.fastQ")), + "Fasta file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.pfam")), + "PFAM file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.stk")), + "Stockholm file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.sto")), + "Stockholm file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.pir")), + "PIR file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.blc")), + "BLC file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.amsa")), + "AMSA file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.html")), + "HTML file"); + assertNull(jfw.getTypeDescription(new File("uniref50.htm"))); + assertEquals(jfw.getTypeDescription(new File("uniref50.xml")), + "RNAML file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.rnaml")), + "RNAML file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.json")), + "JSON file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.pileup")), + "PileUp file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.msf")), + "MSF file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.aln")), + "Clustal file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.phy")), + "PHYLIP file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.gff2")), + "GFF or Jalview features file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.gff3")), + "GFF or Jalview features file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.pdb")), + "PDB file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.ent")), + "PDB file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.cif")), + "mmCIF file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.jvp")), + "Jalview file"); + assertEquals(jfw.getTypeDescription(new File("uniref50.jar")), + "Jalview file (old)"); + } +} -- 1.7.10.2