root / trunk / frameworks / _fwAndami / src / com / iver / andami / iconthemes / IconTheme.java @ 10999
History | View | Annotate | Download (11.1 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.net.MalformedURLException; |
46 |
import java.net.URL; |
47 |
import java.util.HashMap; |
48 |
|
49 |
import javax.swing.ImageIcon; |
50 |
|
51 |
import org.apache.log4j.Logger; |
52 |
|
53 |
import com.iver.andami.PluginServices; |
54 |
|
55 |
/**
|
56 |
* <p>This class represents an icon theme, which is basically a mapping of
|
57 |
* symbolic icon names, and real icons (or icon paths). This is useful to
|
58 |
* change an application's icons in an easy way. An icon theme
|
59 |
* is usually read from disk on start up, but can also be created or
|
60 |
* modified on a later time.</p>
|
61 |
*
|
62 |
* <p>Developers are encouraged to always use the
|
63 |
* <code>get(iconName, fallbackImage)</code> methods to get icons,
|
64 |
* as they ensure that the icons are not overwritten in the theme, but it
|
65 |
* also ensures than an image is got in case the icon was still not
|
66 |
* registered. Note that in this case, the iconName gets registered
|
67 |
* (it is associated with the provided fallbackImage).
|
68 |
* </p>
|
69 |
*
|
70 |
* <p>Developers are encouraged to NOT override icons which are
|
71 |
* present in the theme, as this defeats the purpose of IconThemes.</p>
|
72 |
*
|
73 |
* @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
|
74 |
*/
|
75 |
public class IconTheme { |
76 |
HashMap iconList = new HashMap(); |
77 |
Logger logger = PluginServices.getLogger();
|
78 |
|
79 |
/**
|
80 |
* Returns <code>true</code> if the icon theme contains a mapping for the
|
81 |
* specified iconName.
|
82 |
*
|
83 |
* @param iconName The key to check if it has been registered in this
|
84 |
* IconTheme
|
85 |
*
|
86 |
* @return <code>true</code> if this IconTheme contains
|
87 |
* <code>iconName</code>, <code>false</code> otherwise.
|
88 |
*/
|
89 |
public boolean exists(String iconName) { |
90 |
return iconList.containsKey(iconName);
|
91 |
} |
92 |
|
93 |
/**
|
94 |
* Gets the ImageIcon associated with the provided key, if the key
|
95 |
* is present in the theme, or returns <code>null</code> otherwise.
|
96 |
*
|
97 |
* @param iconName
|
98 |
* The key whose associated icon is to be retrieved
|
99 |
*
|
100 |
* @return
|
101 |
* The icon associated with the provided key, or
|
102 |
* <code>null</code> otherwise.
|
103 |
*/
|
104 |
public ImageIcon get(String iconName) { |
105 |
Object object = iconList.get(iconName);
|
106 |
if (object!=null) { |
107 |
if (object instanceof String) |
108 |
return new ImageIcon((String)object); |
109 |
else if (object instanceof ImageIcon) |
110 |
return (ImageIcon) object; |
111 |
} |
112 |
return null; |
113 |
} |
114 |
|
115 |
/**
|
116 |
* Gets the ImageIcon associated with the provided key, if the key
|
117 |
* is present in the theme, or register and returns the provided icon
|
118 |
* otherwise.
|
119 |
*
|
120 |
* @param iconName
|
121 |
* The key whose associated icon is to be retrieved
|
122 |
* @param fallbackImage
|
123 |
*
|
124 |
* @return
|
125 |
* The icon associated with the provided key, or
|
126 |
* <code>null</code> otherwise.
|
127 |
*/
|
128 |
public ImageIcon get(String iconName, ImageIcon fallbackImage) { |
129 |
Object object = iconList.get(iconName);
|
130 |
if (object!=null) { |
131 |
// key is registered
|
132 |
if (object instanceof ImageIcon) |
133 |
return (ImageIcon) object; |
134 |
else if (object instanceof URL) { |
135 |
// ok, we got an imagePath, let's see if it's valid
|
136 |
ImageIcon icon = new ImageIcon((URL)object); |
137 |
if (icon.getImage()!=null) |
138 |
// the icon was successfully created from the imagePath
|
139 |
return icon;
|
140 |
else {
|
141 |
// it seems the imagePath was not valid, register the fallbackImage then
|
142 |
logger.error("Registered icon does not map to a valid image -- key: "+iconName+" -- URL: "+object.toString()); |
143 |
register(iconName, fallbackImage); |
144 |
return fallbackImage;
|
145 |
} |
146 |
} |
147 |
} |
148 |
// the key was not in the list
|
149 |
register(iconName, fallbackImage); |
150 |
return fallbackImage;
|
151 |
} |
152 |
|
153 |
/**
|
154 |
* Gets the ImageIcon associated with the provided key, if the key
|
155 |
* is present in the theme, or register and returns the provided icon
|
156 |
* otherwise.
|
157 |
*
|
158 |
* @param iconName
|
159 |
* The key whose associated icon is to be retrieved
|
160 |
* @param fallbackImage
|
161 |
*
|
162 |
* @return
|
163 |
* The icon associated with the provided key, or
|
164 |
* <code>null</code> otherwise.
|
165 |
*/
|
166 |
public ImageIcon get(String iconName, URL fallbackImage) { |
167 |
Object object = iconList.get(iconName);
|
168 |
if (object!=null) { |
169 |
// key is registered
|
170 |
if (object instanceof ImageIcon) |
171 |
return (ImageIcon) object; |
172 |
else if (object instanceof URL) { |
173 |
return tryToGetFromURLs(iconName, (URL) object, fallbackImage); |
174 |
} |
175 |
} |
176 |
// the key was not in the list
|
177 |
ImageIcon icon = new ImageIcon(fallbackImage); |
178 |
if (icon.getImage()!=null) { |
179 |
register(iconName, fallbackImage); |
180 |
return icon;
|
181 |
} |
182 |
else {
|
183 |
// we don't want to return an empty ImageIcon
|
184 |
logger.error("Provided icon does not map to a valid image -- key: "+iconName+" -- URL: "+fallbackImage.toString()); |
185 |
return null; |
186 |
} |
187 |
} |
188 |
|
189 |
/**
|
190 |
* Gets the ImageIcon associated with the provided key, if the key
|
191 |
* is present in the theme, or register and returns the provided icon
|
192 |
* otherwise.
|
193 |
*
|
194 |
* @param iconName
|
195 |
* The key whose associated icon is to be retrieved
|
196 |
* @param fallbackImage
|
197 |
*
|
198 |
* @return
|
199 |
* The icon associated with the provided key, or
|
200 |
* <code>null</code> otherwise.
|
201 |
*/
|
202 |
public ImageIcon get(String iconName, String fallbackImage) { |
203 |
try {
|
204 |
Object object = iconList.get(iconName);
|
205 |
if (object!=null) { |
206 |
// key is registered
|
207 |
if (object instanceof ImageIcon) |
208 |
return (ImageIcon) object; |
209 |
else if (object instanceof URL) { |
210 |
File fallbackFile = new File(fallbackImage); |
211 |
return tryToGetFromURLs(iconName, (URL)object, fallbackFile.toURL()); |
212 |
} |
213 |
} |
214 |
// the key was not in the list
|
215 |
ImageIcon icon = new ImageIcon(fallbackImage); |
216 |
if (icon.getImage()!=null) { |
217 |
File fallbackFile = new File(fallbackImage); |
218 |
register(iconName, fallbackFile.toURL()); |
219 |
return icon;
|
220 |
} |
221 |
else {
|
222 |
// we don't want to return an empty ImageIcon
|
223 |
logger.error("Provided icon does not map to a valid image -- key: "+iconName+" -- URL: "+fallbackImage.toString()); |
224 |
} |
225 |
} |
226 |
catch (MalformedURLException ex) {} |
227 |
return null; |
228 |
} |
229 |
|
230 |
|
231 |
/**
|
232 |
* <p>Register in this theme the provided iconName and the associated
|
233 |
* image. Developers
|
234 |
* must not override icons already registered, as this defeats the
|
235 |
* purpose of the IconTheme. Therefore, use the <code>exists</code>
|
236 |
* method before using <code>register</code>, to ensure the icon
|
237 |
* is not previously registered.</p>
|
238 |
*
|
239 |
* @param iconName The name of the icon to register. It is the name
|
240 |
* that will be used later to retrieve the icon.
|
241 |
*
|
242 |
* @param image The image that is going to be associated with the
|
243 |
* provided icon name.
|
244 |
*/
|
245 |
public void register(String iconName, ImageIcon image) { |
246 |
iconList.put(iconName, image); |
247 |
} |
248 |
|
249 |
/**
|
250 |
* <p>Register in this theme the provided iconName and the associated
|
251 |
* image. Developers
|
252 |
* must not override icons already registered, as this defeats the
|
253 |
* purpose of the IconTheme. Therefore, use the <code>exists</code>
|
254 |
* method before using <code>register</code>, to ensure the icon
|
255 |
* is not previously registered.</p>
|
256 |
*
|
257 |
* <p>Note that this method may fail if the provided path does not
|
258 |
* point to a valid image, so check the return value.</p>
|
259 |
*
|
260 |
* @param iconName The name of the icon to register. It is the name
|
261 |
* that will be used later to retrieve the icon.
|
262 |
*
|
263 |
* @param image The path to the image that is going to be
|
264 |
* associated with the provided icon name.
|
265 |
*
|
266 |
* @return <code>true</code> if the provided path points to a
|
267 |
* valid file, <code>false</code> otherwise.
|
268 |
*/
|
269 |
public boolean register(String iconName, String imagePath) { |
270 |
try {
|
271 |
File imageFile = new File(imagePath); |
272 |
if (imageFile.exists()) {
|
273 |
iconList.put(iconName, imageFile.toURL()); |
274 |
return true; |
275 |
} |
276 |
} |
277 |
catch (NullPointerException ex1) {} |
278 |
catch (MalformedURLException ex2) {} |
279 |
return false; |
280 |
} |
281 |
|
282 |
/**
|
283 |
* <p>Register in this theme the provided iconName and the associated
|
284 |
* image. Developers
|
285 |
* must not override icons already registered, as this defeats the
|
286 |
* purpose of the IconTheme. Therefore, use the <code>exists</code>
|
287 |
* method before using <code>register</code>, to ensure the icon
|
288 |
* is not previously registered.</p>
|
289 |
*
|
290 |
* @param iconName The name of the icon to register. It is the name
|
291 |
* that will be used later to retrieve the icon.
|
292 |
*
|
293 |
* @param image The URL pointing to the image that is going to be
|
294 |
* associated with the provided icon name.
|
295 |
*/
|
296 |
public void register(String iconName, URL urlImage) { |
297 |
iconList.put(iconName, urlImage); |
298 |
} |
299 |
|
300 |
/**
|
301 |
* Try to load an image from <code>imageURL</code>; if it it does not
|
302 |
* point to a valid image, try with <code>fallbackURL</code>.
|
303 |
*
|
304 |
* @param iconName
|
305 |
* @param imageURL
|
306 |
* @param fallbackURL
|
307 |
*
|
308 |
* @return If any image was successfully loaded, return this image,
|
309 |
* otherwise return <code>null</code>.
|
310 |
*/
|
311 |
private ImageIcon tryToGetFromURLs(String iconName, URL imageURL, URL fallbackURL) { |
312 |
// let's see if imageURL is valid
|
313 |
ImageIcon icon = new ImageIcon(imageURL); |
314 |
if (icon.getImage()!=null) { |
315 |
// the icon was successfully created from the imagePath
|
316 |
return icon;
|
317 |
} |
318 |
else {
|
319 |
// it seems the imagePath was not valid, try with fallbackImage then
|
320 |
logger.error("Registered icon does not map to a valid image -- key: "+iconName+" -- URL: "+imageURL.toString()); |
321 |
icon = new ImageIcon(fallbackURL); |
322 |
if (icon.getImage()!=null) { |
323 |
register(iconName, fallbackURL); |
324 |
return icon;
|
325 |
} |
326 |
else {
|
327 |
// we don't want to return an empty ImageIcon
|
328 |
logger.error("Provided icon does not map to a valid image -- key: "+iconName+" -- URL: "+fallbackURL.toString()); |
329 |
return null; |
330 |
} |
331 |
} |
332 |
} |
333 |
|
334 |
/**
|
335 |
* Return the URL which is currently associated with the
|
336 |
* provided icon name, if this icon was registered as an
|
337 |
* URL, or <code>null</code> if it is not present in the theme
|
338 |
* or it was registered as an IconImage.
|
339 |
*
|
340 |
* @param iconName
|
341 |
* @return The URL which is currently associated with the
|
342 |
* provided icon name, if this icon was registered as an
|
343 |
* URL, or <code>null</code> if it is not present in the theme
|
344 |
* or it was registered as an IconImage.
|
345 |
*/
|
346 |
public URL getURL(String iconName) { |
347 |
Object object = iconList.get(iconName);
|
348 |
if (object !=null && object instanceof URL) |
349 |
return (URL) object; |
350 |
return null; |
351 |
} |
352 |
} |