Class |
Line # |
Actions |
|||
---|---|---|---|---|---|
JalviewFileView | 37 | 51 | 20 |
1 | /* | |
2 | * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) | |
3 | * Copyright (C) $$Year-Rel$$ The Jalview Authors | |
4 | * | |
5 | * This file is part of Jalview. | |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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. | |
20 | */ | |
21 | package jalview.io; | |
22 | ||
23 | import java.util.Locale; | |
24 | ||
25 | import jalview.util.MessageManager; | |
26 | ||
27 | import java.io.File; | |
28 | import java.net.URL; | |
29 | import java.util.HashMap; | |
30 | import java.util.Iterator; | |
31 | import java.util.Map; | |
32 | ||
33 | import javax.swing.Icon; | |
34 | import javax.swing.ImageIcon; | |
35 | import javax.swing.filechooser.FileView; | |
36 | ||
37 | public class JalviewFileView extends FileView | |
38 | { | |
39 | private static Map<String, String> extensions; | |
40 | ||
41 | private static Map<String, ImageIcon> icons; | |
42 | ||
43 | 1 | private void loadExtensions() |
44 | { | |
45 | 1 | extensions = new HashMap<>(); |
46 | 1 | for (FileFormatI ff : FileFormats.getInstance().getFormats()) |
47 | { | |
48 | 22 | String desc = ff.getName() + " file"; |
49 | 22 | String exts = ff.getExtensions(); |
50 | 22 | for (String ext : exts.split(",")) |
51 | { | |
52 | 31 | ext = ext.trim().toLowerCase(Locale.ROOT); |
53 | 31 | extensions.put(ext, desc + ("jar".equals(ext) ? " (old)" : "")); |
54 | } | |
55 | } | |
56 | } | |
57 | ||
58 | 26 | @Override |
59 | public String getTypeDescription(File f) | |
60 | { | |
61 | 26 | String extension = getExtension(f); |
62 | ||
63 | 26 | String type = getDescriptionForExtension(extension); |
64 | ||
65 | 26 | if (extension != null) |
66 | { | |
67 | 26 | if (extensions.containsKey(extension)) |
68 | { | |
69 | 25 | type = extensions.get(extension).toString(); |
70 | } | |
71 | } | |
72 | ||
73 | 26 | return type; |
74 | } | |
75 | ||
76 | 26 | private String getDescriptionForExtension(String extension) |
77 | { | |
78 | 26 | synchronized (this) |
79 | { | |
80 | 26 | if (extensions == null) |
81 | { | |
82 | 1 | loadExtensions(); |
83 | } | |
84 | } | |
85 | 26 | return extensions.get(extension); |
86 | } | |
87 | ||
88 | 0 | @Override |
89 | public Icon getIcon(File f) | |
90 | { | |
91 | 0 | String extension = getExtension(f); |
92 | 0 | Icon icon = null; |
93 | 0 | String type = getDescriptionForExtension(extension); |
94 | ||
95 | 0 | if (type == null) |
96 | { | |
97 | 0 | Iterator<String> it = extensions.keySet().iterator(); |
98 | 0 | EXTENSION: while (it.hasNext()) |
99 | { | |
100 | 0 | String ext = it.next(); |
101 | ||
102 | // quick negative test | |
103 | 0 | if (!f.getName().contains(ext)) |
104 | { | |
105 | 0 | continue EXTENSION; |
106 | } | |
107 | ||
108 | 0 | BackupFilenameParts bfp = BackupFilenameParts |
109 | .currentBackupFilenameParts(f.getName(), ext, true); | |
110 | 0 | if (bfp.isBackupFile()) |
111 | { | |
112 | 0 | extension = ext; |
113 | 0 | type = getDescriptionForExtension(extension) |
114 | + MessageManager.getString("label.backup"); | |
115 | 0 | break; |
116 | } | |
117 | } | |
118 | } | |
119 | ||
120 | 0 | if (type != null) |
121 | { | |
122 | 0 | icon = getImageIcon("/images/file.png"); |
123 | } | |
124 | ||
125 | 0 | return icon; |
126 | } | |
127 | ||
128 | /** | |
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. | |
131 | */ | |
132 | 30 | public static String getExtension(File f) |
133 | { | |
134 | 30 | String ext = null; |
135 | 30 | String s = f.getName(); |
136 | 30 | int i = s.lastIndexOf('.'); |
137 | ||
138 | 30 | if ((i > 0) && (i < (s.length() - 1))) |
139 | { | |
140 | 28 | ext = s.substring(i + 1).toLowerCase(Locale.ROOT); |
141 | } | |
142 | ||
143 | 30 | return ext; |
144 | } | |
145 | ||
146 | /** | |
147 | * Returns an ImageIcon, or null if the file was not found | |
148 | * | |
149 | * @param filePath | |
150 | */ | |
151 | 6 | protected ImageIcon getImageIcon(String filePath) |
152 | { | |
153 | /* | |
154 | * we reuse a single icon object per path here | |
155 | */ | |
156 | 6 | synchronized (this) |
157 | { | |
158 | 6 | if (icons == null) |
159 | { | |
160 | 1 | icons = new HashMap<>(); |
161 | } | |
162 | 6 | if (!icons.containsKey(filePath)) |
163 | { | |
164 | 4 | ImageIcon icon = null; |
165 | 4 | URL imgURL = JalviewFileView.class.getResource(filePath); |
166 | 4 | if (imgURL != null) |
167 | { | |
168 | 2 | icon = new ImageIcon(imgURL); |
169 | } | |
170 | else | |
171 | { | |
172 | 2 | jalview.bin.Console.errPrintln( |
173 | "JalviewFileView.createImageIcon: Couldn't find file: " | |
174 | + filePath); | |
175 | } | |
176 | 4 | icons.put(filePath, icon); |
177 | } | |
178 | } | |
179 | ||
180 | /* | |
181 | * return the image from the table (which may be null if | |
182 | * icon creation failed) | |
183 | */ | |
184 | 6 | return icons.get(filePath); |
185 | } | |
186 | } |