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 java.util.Locale;
25 import jalview.util.MessageManager;
29 import java.util.HashMap;
30 import java.util.Iterator;
33 import javax.swing.Icon;
34 import javax.swing.ImageIcon;
35 import javax.swing.filechooser.FileView;
37 public class JalviewFileView extends FileView
39 private static Map<String, String> extensions;
41 private static Map<String, ImageIcon> icons;
43 private void loadExtensions()
45 extensions = new HashMap<>();
46 for (FileFormatI ff : FileFormats.getInstance().getFormats())
48 String desc = ff.getName() + " file";
49 String exts = ff.getExtensions();
50 for (String ext : exts.split(","))
52 ext = ext.trim().toLowerCase(Locale.ROOT);
53 extensions.put(ext, desc + ("jar".equals(ext) ? " (old)" : ""));
59 public String getTypeDescription(File f)
61 String extension = getExtension(f);
63 String type = getDescriptionForExtension(extension);
65 if (extension != null)
67 if (extensions.containsKey(extension))
69 type = extensions.get(extension).toString();
76 private String getDescriptionForExtension(String extension)
80 if (extensions == null)
85 return extensions.get(extension);
89 public Icon getIcon(File f)
91 String extension = getExtension(f);
93 String type = getDescriptionForExtension(extension);
97 Iterator<String> it = extensions.keySet().iterator();
98 EXTENSION: while (it.hasNext())
100 String ext = it.next();
102 // quick negative test
103 if (!f.getName().contains(ext))
108 BackupFilenameParts bfp = BackupFilenameParts
109 .currentBackupFilenameParts(f.getName(), ext, true);
110 if (bfp.isBackupFile())
113 type = getDescriptionForExtension(extension)
114 + MessageManager.getString("label.backup");
122 icon = getImageIcon("/images/file.png");
129 * Returns the extension of a file (part of the name after the last period),
130 * in lower case, or null if the name ends in or does not include a period.
132 public static String getExtension(File f)
135 String s = f.getName();
136 int i = s.lastIndexOf('.');
138 if ((i > 0) && (i < (s.length() - 1)))
140 ext = s.substring(i + 1).toLowerCase(Locale.ROOT);
147 * Returns an ImageIcon, or null if the file was not found
151 protected ImageIcon getImageIcon(String filePath)
154 * we reuse a single icon object per path here
160 icons = new HashMap<>();
162 if (!icons.containsKey(filePath))
164 ImageIcon icon = null;
165 URL imgURL = JalviewFileView.class.getResource(filePath);
168 icon = new ImageIcon(imgURL);
173 "JalviewFileView.createImageIcon: Couldn't find file: "
176 icons.put(filePath, icon);
181 * return the image from the table (which may be null if
182 * icon creation failed)
184 return icons.get(filePath);