root / trunk / frameworks / _fwAndami / src / com / iver / andami / iconthemes / IconTheme.java @ 11041
History | View | Annotate | Download (10.6 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
|
42 |
package com.iver.andami.iconthemes; |
43 |
|
44 |
import java.io.File; |
45 |
import java.io.IOException; |
46 |
import java.io.InputStream; |
47 |
import java.net.MalformedURLException; |
48 |
import java.net.URL; |
49 |
import java.util.HashMap; |
50 |
import java.util.zip.ZipEntry; |
51 |
import java.util.zip.ZipFile; |
52 |
|
53 |
import javax.swing.ImageIcon; |
54 |
|
55 |
import org.apache.log4j.Logger; |
56 |
|
57 |
/**
|
58 |
* <p>This class represents an icon theme, which is basically a mapping of
|
59 |
* symbolic icon names, and real icons (or icon paths). This is useful to
|
60 |
* change an application's icons in an easy way. An icon theme
|
61 |
* is usually read from disk on start up, but can also be created or
|
62 |
* modified on a later time.</p>
|
63 |
*
|
64 |
* <p>Developers are encouraged to always use the
|
65 |
* <code>get(iconName, fallbackImage)</code> methods to get icons,
|
66 |
* as they ensure that the icons are not overwritten in the theme, but it
|
67 |
* also ensures than an image is got in case the icon was still not
|
68 |
* registered. Note that in this case, the iconName gets registered
|
69 |
* (it is associated with the provided fallbackImage).
|
70 |
* </p>
|
71 |
*
|
72 |
* <p>Developers are encouraged to NOT override icons which are
|
73 |
* present in the theme, as this defeats the purpose of IconThemes.</p>
|
74 |
*
|
75 |
* @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
|
76 |
*/
|
77 |
public class IconTheme { |
78 |
private HashMap iconList = new HashMap(); |
79 |
private Logger logger; |
80 |
private IconThemeInfo info;
|
81 |
|
82 |
/**
|
83 |
* Creates a new empty IconTheme. The next step is to register icons in order
|
84 |
* the theme to be useful.
|
85 |
*
|
86 |
* @param themeInfo An IconThemeInfo containing the name, the description, the
|
87 |
* version and the source resource of the theme.
|
88 |
*/
|
89 |
public IconTheme(IconThemeInfo themeInfo) {
|
90 |
this.info = themeInfo;
|
91 |
} |
92 |
|
93 |
|
94 |
/**
|
95 |
* Returns <code>true</code> if the icon theme contains a mapping for the
|
96 |
* specified iconName.
|
97 |
*
|
98 |
* @param iconName The key to check if it has been registered in this
|
99 |
* IconTheme
|
100 |
*
|
101 |
* @return <code>true</code> if this IconTheme contains
|
102 |
* <code>iconName</code>, <code>false</code> otherwise.
|
103 |
*/
|
104 |
public boolean exists(String iconName) { |
105 |
return iconList.containsKey(iconName);
|
106 |
} |
107 |
|
108 |
/**
|
109 |
* Gets the ImageIcon associated with the provided key, if the key
|
110 |
* is present in the theme, or returns <code>null</code> otherwise.
|
111 |
*
|
112 |
* @param iconName
|
113 |
* The key whose associated icon is to be retrieved
|
114 |
*
|
115 |
* @return
|
116 |
* The icon associated with the provided key, or
|
117 |
* <code>null</code> otherwise.
|
118 |
*/
|
119 |
public ImageIcon get(String iconName) { |
120 |
Object object = iconList.get(iconName);
|
121 |
if (object!=null) { |
122 |
if (object instanceof ImageIcon) |
123 |
return (ImageIcon) object; |
124 |
else if (object instanceof URL) { |
125 |
// ok, we got an imagePath, let's see if it's valid
|
126 |
ImageIcon icon = new ImageIcon((URL)object); |
127 |
if (icon.getImage()!=null) { |
128 |
// the icon was successfully created from the imagePath
|
129 |
return icon;
|
130 |
} |
131 |
else {
|
132 |
getLogger().error("Registered icon does not map to a valid image -- key: "+iconName+" -- URL: "+object.toString()); |
133 |
return null; |
134 |
} |
135 |
|
136 |
} |
137 |
else if (object instanceof ZipEntry && info.getResource() instanceof ZipFile) { |
138 |
ZipEntry entry = (ZipEntry) object; |
139 |
ZipFile file = (ZipFile) info.getResource(); |
140 |
try {
|
141 |
InputStream is = file.getInputStream(entry);
|
142 |
int size=(int)entry.getSize(); |
143 |
if (size==-1) return null; |
144 |
byte[] b=new byte[size]; |
145 |
int offset=0; |
146 |
int chunk=0; |
147 |
while ((size - offset) > 0) { |
148 |
chunk=is.read(b, offset, size - offset); |
149 |
if (chunk==-1) { |
150 |
break;
|
151 |
} |
152 |
offset+=chunk; |
153 |
} |
154 |
} catch (IOException e) { |
155 |
} |
156 |
} |
157 |
} |
158 |
return null; |
159 |
} |
160 |
|
161 |
/**
|
162 |
* Gets the ImageIcon associated with the provided key, if the key
|
163 |
* is present in the theme, or register and returns the provided icon
|
164 |
* otherwise.
|
165 |
*
|
166 |
* @param iconName
|
167 |
* The key whose associated icon is to be retrieved
|
168 |
* @param fallbackImage
|
169 |
*
|
170 |
* @return
|
171 |
* The icon associated with the provided key, or
|
172 |
* <code>null</code> otherwise.
|
173 |
*/
|
174 |
public ImageIcon get(String iconName, ImageIcon fallbackImage) { |
175 |
ImageIcon icon = get(iconName);
|
176 |
if (icon!=null) { |
177 |
return icon;
|
178 |
} |
179 |
else {
|
180 |
register(iconName, fallbackImage); |
181 |
return fallbackImage;
|
182 |
} |
183 |
} |
184 |
|
185 |
/**
|
186 |
* Gets the ImageIcon associated with the provided key, if the key
|
187 |
* is present in the theme, or register and returns the provided icon
|
188 |
* otherwise.
|
189 |
*
|
190 |
* @param iconName
|
191 |
* The key whose associated icon is to be retrieved
|
192 |
* @param fallbackImage
|
193 |
*
|
194 |
* @return
|
195 |
* The icon associated with the provided key, or
|
196 |
* <code>null</code> otherwise.
|
197 |
*/
|
198 |
public ImageIcon get(String iconName, URL fallbackImage) { |
199 |
ImageIcon icon = get(iconName);
|
200 |
if (icon!=null) { |
201 |
return icon;
|
202 |
} |
203 |
else {
|
204 |
icon = new ImageIcon(fallbackImage); |
205 |
if (icon.getImage()!=null) { |
206 |
register(iconName, fallbackImage); |
207 |
return icon;
|
208 |
} |
209 |
else {
|
210 |
// we don't want to return an empty ImageIcon
|
211 |
getLogger().error("Provided icon does not map to a valid image -- key: "+iconName+" -- URL: "+fallbackImage.toString()); |
212 |
} |
213 |
return null; |
214 |
} |
215 |
} |
216 |
|
217 |
/**
|
218 |
* Gets the ImageIcon associated with the provided key, if the key
|
219 |
* is present in the theme, or register and returns the provided icon
|
220 |
* otherwise.
|
221 |
*
|
222 |
* @param iconName
|
223 |
* The key whose associated icon is to be retrieved
|
224 |
* @param fallbackImage
|
225 |
*
|
226 |
* @return
|
227 |
* The icon associated with the provided key, or
|
228 |
* <code>null</code> otherwise.
|
229 |
*/
|
230 |
public ImageIcon get(String iconName, String fallbackImage) { |
231 |
ImageIcon icon = get(iconName);
|
232 |
if (icon!=null) { |
233 |
return icon;
|
234 |
} |
235 |
else {
|
236 |
icon = new ImageIcon(fallbackImage); |
237 |
if (icon.getImage()!=null) { |
238 |
try {
|
239 |
File fallbackFile = new File(fallbackImage); |
240 |
register(iconName, fallbackFile.toURL()); |
241 |
return icon;
|
242 |
} catch (MalformedURLException e) { |
243 |
e.printStackTrace(); |
244 |
return null; |
245 |
} |
246 |
} |
247 |
else {
|
248 |
// we don't want to return an empty ImageIcon
|
249 |
getLogger().error("Provided icon does not map to a valid image -- key: "+iconName+" -- URL: "+fallbackImage.toString()); |
250 |
} |
251 |
return null; |
252 |
} |
253 |
} |
254 |
|
255 |
|
256 |
/**
|
257 |
* <p>Register in this theme the provided iconName and the associated
|
258 |
* image. Developers
|
259 |
* must not override icons already registered, as this defeats the
|
260 |
* purpose of the IconTheme. Therefore, use the <code>exists</code>
|
261 |
* method before using <code>register</code>, to ensure the icon
|
262 |
* is not previously registered.</p>
|
263 |
*
|
264 |
* @param iconName The name of the icon to register. It is the name
|
265 |
* that will be used later to retrieve the icon.
|
266 |
*
|
267 |
* @param image The image that is going to be associated with the
|
268 |
* provided icon name.
|
269 |
*/
|
270 |
public void register(String iconName, ImageIcon image) { |
271 |
iconList.put(iconName, image); |
272 |
} |
273 |
|
274 |
/**
|
275 |
* <p>Register in this theme the provided iconName and the associated
|
276 |
* image. Developers
|
277 |
* must not override icons already registered, as this defeats the
|
278 |
* purpose of the IconTheme. Therefore, use the <code>exists</code>
|
279 |
* method before using <code>register</code>, to ensure the icon
|
280 |
* is not previously registered.</p>
|
281 |
*
|
282 |
* <p>Note that this method may fail if the provided path does not
|
283 |
* point to a valid image, so check the return value.</p>
|
284 |
*
|
285 |
* @param iconName The name of the icon to register. It is the name
|
286 |
* that will be used later to retrieve the icon.
|
287 |
*
|
288 |
* @param image The path to the image that is going to be
|
289 |
* associated with the provided icon name.
|
290 |
*
|
291 |
* @return <code>true</code> if the provided path points to a
|
292 |
* valid file, <code>false</code> otherwise.
|
293 |
*/
|
294 |
public boolean register(String iconName, String imagePath) { |
295 |
try {
|
296 |
File imageFile = new File(imagePath); |
297 |
if (imageFile.exists()) {
|
298 |
iconList.put(iconName, imageFile.toURL()); |
299 |
return true; |
300 |
} |
301 |
} |
302 |
catch (NullPointerException ex1) {} |
303 |
catch (MalformedURLException ex2) {} |
304 |
return false; |
305 |
} |
306 |
|
307 |
/**
|
308 |
* <p>Register in this theme the provided iconName and the associated
|
309 |
* image. Developers
|
310 |
* must not override icons already registered, as this defeats the
|
311 |
* purpose of the IconTheme. Therefore, use the <code>exists</code>
|
312 |
* method before using <code>register</code>, to ensure the icon
|
313 |
* is not previously registered.</p>
|
314 |
*
|
315 |
* @param iconName The name of the icon to register. It is the name
|
316 |
* that will be used later to retrieve the icon.
|
317 |
*
|
318 |
* @param image The URL pointing to the image that is going to be
|
319 |
* associated with the provided icon name.
|
320 |
*/
|
321 |
public void register(String iconName, URL urlImage) { |
322 |
iconList.put(iconName, urlImage); |
323 |
} |
324 |
|
325 |
public void register(String iconName, ZipEntry zippedImage) { |
326 |
iconList.put(iconName, zippedImage); |
327 |
} |
328 |
|
329 |
/**
|
330 |
* Return the URL which is currently associated with the
|
331 |
* provided icon name, if this icon was registered as an
|
332 |
* URL, or <code>null</code> if it is not present in the theme
|
333 |
* or it was registered as an IconImage.
|
334 |
*
|
335 |
* @param iconName
|
336 |
* @return The URL which is currently associated with the
|
337 |
* provided icon name, if this icon was registered as an
|
338 |
* URL, or <code>null</code> if it is not present in the theme
|
339 |
* or it was registered as an IconImage.
|
340 |
*/
|
341 |
public URL getURL(String iconName) { |
342 |
Object object = iconList.get(iconName);
|
343 |
if (object !=null && object instanceof URL) |
344 |
return (URL) object; |
345 |
return null; |
346 |
} |
347 |
|
348 |
public IconThemeInfo getInfo() {
|
349 |
return info;
|
350 |
} |
351 |
|
352 |
private Logger getLogger() { |
353 |
if (logger!=null) |
354 |
return logger;
|
355 |
else
|
356 |
return Logger.getLogger(this.getClass()); |
357 |
} |
358 |
|
359 |
protected void setLogger(Logger logger) { |
360 |
this.logger = logger;
|
361 |
} |
362 |
} |