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.
23 import jalview.util.MessageManager;
27 import java.util.HashMap;
28 import java.util.Iterator;
31 import javax.swing.Icon;
32 import javax.swing.ImageIcon;
33 import javax.swing.filechooser.FileView;
35 public class JalviewFileView extends FileView
37 private static Map<String, String> extensions;
39 private static Map<String, ImageIcon> icons;
41 private void loadExtensions()
43 extensions = new HashMap<>();
44 for (FileFormatI ff : FileFormats.getInstance().getFormats())
46 String desc = ff.getName() + " file";
47 String exts = ff.getExtensions();
48 for (String ext : exts.split(","))
50 ext = ext.trim().toLowerCase();
52 desc + ("jar".equals(ext) ? " (old)" : ""));
58 public String getTypeDescription(File f)
60 String extension = getExtension(f);
62 String type = getDescriptionForExtension(extension);
64 if (extension != null)
66 if (extensions.containsKey(extension))
68 type = extensions.get(extension).toString();
75 private String getDescriptionForExtension(String extension)
79 if (extensions == null)
84 return extensions.get(extension);
88 public Icon getIcon(File f)
90 String extension = getExtension(f);
92 String type = getDescriptionForExtension(extension);
96 Iterator<String> it = extensions.keySet().iterator();
97 EXTENSION: while (it.hasNext())
99 String ext = it.next();
101 // quick negative test
102 if (!f.getName().contains(ext))
107 BackupFilenameParts bfp = BackupFilenameParts
108 .currentBackupFilenameParts(f.getName(), ext, true);
109 if (bfp.isBackupFile())
112 type = getDescriptionForExtension(extension)
113 + MessageManager.getString("label.backup");
121 icon = getImageIcon("/images/file.png");
128 * Returns the extension of a file (part of the name after the last period),
129 * in lower case, or null if the name ends in or does not include a period.
131 public static String getExtension(File f)
134 String s = f.getName();
135 int i = s.lastIndexOf('.');
137 if ((i > 0) && (i < (s.length() - 1)))
139 ext = s.substring(i + 1).toLowerCase();
146 * Returns an ImageIcon, or null if the file was not found
150 protected ImageIcon getImageIcon(String filePath)
153 * we reuse a single icon object per path here
159 icons = new HashMap<>();
161 if (!icons.containsKey(filePath))
163 ImageIcon icon = null;
164 URL imgURL = JalviewFileView.class.getResource(filePath);
167 icon = new ImageIcon(imgURL);
172 "JalviewFileView.createImageIcon: Couldn't find file: "
175 icons.put(filePath, icon);
180 * return the image from the table (which may be null if
181 * icon creation failed)
183 return icons.get(filePath);