*/
package jalview.io;
+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;
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();
+ extensions.put(ext,
+ desc + ("jar".equals(ext) ? " (old)" : ""));
}
}
}
public String getTypeDescription(File f)
{
String extension = getExtension(f);
+
String type = getDescriptionForExtension(extension);
+
if (extension != null)
{
if (extensions.containsKey(extension))
{
String extension = getExtension(f);
Icon icon = null;
+ String type = getDescriptionForExtension(extension);
- if (getDescriptionForExtension(extension) != null)
+ if (type == null)
{
- icon = createImageIcon("/images/file.png");
+ 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 (type != null)
+ {
+ 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)
{
*
* @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);
}
}