2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
25 import java.util.HashMap;
28 import javax.swing.Icon;
29 import javax.swing.ImageIcon;
30 import javax.swing.filechooser.FileView;
32 public class JalviewFileView extends FileView
34 private static Map<String, String> extensions;
36 private static Map<String, ImageIcon> icons;
38 private void loadExtensions()
40 extensions = new HashMap<String, String>();
41 for (FileFormatI ff : FileFormats.getInstance().getFormats())
43 String desc = ff.getName() + " file";
44 String exts = ff.getExtensions();
45 for (String ext : exts.split(","))
47 extensions.put(ext.trim().toLowerCase(),
48 desc + ("jar".equals(ext) ? " (old)" : ""));
54 public String getTypeDescription(File f)
56 String extension = getExtension(f);
57 String type = getDescriptionForExtension(extension);
58 if (extension != null)
60 if (extensions.containsKey(extension))
62 type = extensions.get(extension).toString();
69 private String getDescriptionForExtension(String extension)
73 if (extensions == null)
78 return extensions.get(extension);
82 public Icon getIcon(File f)
84 String extension = getExtension(f);
87 if (getDescriptionForExtension(extension) != null)
89 icon = getImageIcon("/images/file.png");
96 * Returns the extension of a file (part of the name after the last period),
97 * in lower case, or null if the name ends in or does not include a period.
99 public static String getExtension(File f)
102 String s = f.getName();
103 int i = s.lastIndexOf('.');
105 if ((i > 0) && (i < (s.length() - 1)))
107 ext = s.substring(i + 1).toLowerCase();
114 * Returns an ImageIcon, or null if the file was not found
118 protected ImageIcon getImageIcon(String filePath)
121 * we reuse a single icon object per path here
127 icons = new HashMap<String, ImageIcon>();
129 if (!icons.containsKey(filePath))
131 ImageIcon icon = null;
132 URL imgURL = JalviewFileView.class.getResource(filePath);
135 icon = new ImageIcon(imgURL);
140 "JalviewFileView.createImageIcon: Couldn't find file: "
143 icons.put(filePath, icon);
148 * return the image from the table (which may be null if
149 * icon creation failed)
151 return icons.get(filePath);