X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FJalviewFileView.java;h=6bfb3b9bcea3238e583d170e01a02e582574e2c9;hb=6d6c9b65d01adbdd8b624bea6a766165e4497bb6;hp=6e045615a85cca707c3f588914833503f63b72db;hpb=c98ec7d017c2f11909f8344fe5d44d97c7c6fc24;p=jalview.git diff --git a/src/jalview/io/JalviewFileView.java b/src/jalview/io/JalviewFileView.java index 6e04561..6bfb3b9 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(); - 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 + 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); } }