Statistics
| Revision:

root / trunk / frameworks / _fwAndami / src / com / iver / andami / iconthemes / IconTheme.java @ 11026

History | View | Annotate | Download (11.5 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
        private HashMap iconList = new HashMap();
77
        private Logger logger = PluginServices.getLogger();
78
        private IconThemeInfo info;
79
        
80
        /**
81
         * Creates a new empty IconTheme. The next step is to register icons in order
82
         * the theme to be useful.
83
         * 
84
         * @param themeInfo An IconThemeInfo containing the name, the description, the
85
         * version and the source resource of the theme.
86
         */
87
        public IconTheme(IconThemeInfo themeInfo) {
88
                this.info = themeInfo;
89
        }
90

    
91
        
92
        /**
93
         * Returns <code>true</code> if the icon theme contains a mapping for the
94
         * specified iconName.
95
         * 
96
         * @param iconName The key to check if it has been registered in this
97
         * IconTheme
98
         * 
99
         * @return <code>true</code> if this IconTheme contains
100
         * <code>iconName</code>, <code>false</code> otherwise.
101
         */
102
        public boolean exists(String iconName) {
103
                return iconList.containsKey(iconName);
104
        }
105
        
106
        /**
107
         * Gets the ImageIcon associated with the provided key, if the key
108
         * is present in the theme, or returns <code>null</code> otherwise. 
109
         * 
110
         * @param iconName
111
         *                         The key whose associated icon is to be retrieved
112
         * 
113
         * @return
114
         *                         The icon associated with the provided key, or 
115
         * <code>null</code> otherwise. 
116
         */
117
        public ImageIcon get(String iconName) {
118
                Object object = iconList.get(iconName);
119
                if (object!=null) {
120
                        if (object instanceof String)
121
                                return new ImageIcon((String)object);
122
                        else if (object instanceof ImageIcon)
123
                                return (ImageIcon) object;
124
                }
125
                return null;
126
        }
127
        
128
        /**
129
         * Gets the ImageIcon associated with the provided key, if the key
130
         * is present in the theme, or register and returns the provided icon
131
         * otherwise. 
132
         * 
133
         * @param iconName
134
         *                         The key whose associated icon is to be retrieved
135
         * @param fallbackImage
136
         * 
137
         * @return
138
         *                         The icon associated with the provided key, or 
139
         * <code>null</code> otherwise. 
140
         */
141
        public ImageIcon get(String iconName, ImageIcon fallbackImage) {
142
                Object object = iconList.get(iconName);
143
                if (object!=null) {
144
                        // key is registered
145
                        if (object instanceof ImageIcon)
146
                                return (ImageIcon) object;
147
                        else if (object instanceof URL) {
148
                                // ok, we got an imagePath, let's see if it's valid
149
                                ImageIcon icon =  new ImageIcon((URL)object);
150
                                if (icon.getImage()!=null)
151
                                        // the icon was successfully created from the imagePath
152
                                        return icon;
153
                                else {
154
                                        // it seems the imagePath was not valid, register the fallbackImage then
155
                                        logger.error("Registered icon does not map to a valid image -- key: "+iconName+" -- URL: "+object.toString());
156
                                        register(iconName, fallbackImage);
157
                                        return fallbackImage;
158
                                }
159
                        }
160
                }
161
                // the key was not in the list
162
                register(iconName, fallbackImage);
163
                return fallbackImage;
164
        }
165
        
166
        /**
167
         * Gets the ImageIcon associated with the provided key, if the key
168
         * is present in the theme, or register and returns the provided icon
169
         * otherwise. 
170
         * 
171
         * @param iconName
172
         *                         The key whose associated icon is to be retrieved
173
         * @param fallbackImage
174
         * 
175
         * @return
176
         *                         The icon associated with the provided key, or 
177
         * <code>null</code> otherwise. 
178
         */
179
        public ImageIcon get(String iconName, URL fallbackImage) {
180
                Object object = iconList.get(iconName);
181
                if (object!=null) {
182
                        // key is registered
183
                        if (object instanceof ImageIcon)
184
                                return (ImageIcon) object;
185
                        else if (object instanceof URL) {
186
                                return tryToGetFromURLs(iconName, (URL) object, fallbackImage);
187
                        }
188
                }
189
                // the key was not in the list
190
                ImageIcon icon =  new ImageIcon(fallbackImage);
191
                if (icon.getImage()!=null) {
192
                        register(iconName, fallbackImage);
193
                        return icon;
194
                }
195
                else {
196
                        // we don't want to return an empty ImageIcon
197
                        logger.error("Provided icon does not map to a valid image -- key: "+iconName+" -- URL: "+fallbackImage.toString());
198
                        return null;
199
                }
200
        }
201
        
202
        /**
203
         * Gets the ImageIcon associated with the provided key, if the key
204
         * is present in the theme, or register and returns the provided icon
205
         * otherwise. 
206
         * 
207
         * @param iconName
208
         *                         The key whose associated icon is to be retrieved
209
         * @param fallbackImage
210
         * 
211
         * @return
212
         *                         The icon associated with the provided key, or 
213
         * <code>null</code> otherwise. 
214
         */
215
        public ImageIcon get(String iconName, String fallbackImage) {
216
                try {
217
                        Object object = iconList.get(iconName);
218
                        if (object!=null) {
219
                                // key is registered
220
                                if (object instanceof ImageIcon)
221
                                        return (ImageIcon) object;
222
                                else if (object instanceof URL) {
223
                                        File fallbackFile = new File(fallbackImage);
224
                                        return tryToGetFromURLs(iconName, (URL)object, fallbackFile.toURL());
225
                                }
226
                        }
227
                        // the key was not in the list
228
                        ImageIcon icon = new ImageIcon(fallbackImage);
229
                        if (icon.getImage()!=null) {
230
                                File fallbackFile = new File(fallbackImage);
231
                                register(iconName, fallbackFile.toURL());
232
                                return icon;
233
                        }
234
                        else {
235
                                // we don't want to return an empty ImageIcon
236
                                logger.error("Provided icon does not map to a valid image -- key: "+iconName+" -- URL: "+fallbackImage.toString());
237
                        }
238
                }
239
                catch (MalformedURLException ex) {}
240
                return null;
241
        }
242
        
243

    
244
        /**
245
         * <p>Register in this theme the provided iconName and the associated
246
         * image. Developers
247
         * must not override icons already registered, as this defeats the
248
         * purpose of the IconTheme. Therefore, use the <code>exists</code>
249
         * method before using <code>register</code>, to ensure the icon
250
         * is not previously registered.</p> 
251
         * 
252
         * @param iconName The name of the icon to register. It is the name
253
         * that will be used later to retrieve the icon.
254
         *  
255
         * @param image The image that is going to be associated with the
256
         * provided icon name.
257
         */
258
        public void register(String iconName, ImageIcon image) {
259
                iconList.put(iconName, image);
260
        }
261
        
262
        /**
263
         * <p>Register in this theme the provided iconName and the associated
264
         * image. Developers
265
         * must not override icons already registered, as this defeats the
266
         * purpose of the IconTheme. Therefore, use the <code>exists</code>
267
         * method before using <code>register</code>, to ensure the icon
268
         * is not previously registered.</p> 
269
         * 
270
         * <p>Note that this method may fail if the provided path does not
271
         * point to a valid image, so check the return value.</p>
272
         * 
273
         * @param iconName The name of the icon to register. It is the name
274
         * that will be used later to retrieve the icon.
275
         *  
276
         * @param image The path to the image that is going to be
277
         * associated with the provided icon name.
278
         * 
279
         * @return <code>true</code> if the provided path points to a
280
         * valid file, <code>false</code> otherwise.
281
         */
282
        public boolean register(String iconName, String imagePath) {
283
                try {
284
                        File imageFile = new File(imagePath);
285
                        if (imageFile.exists()) {
286
                                iconList.put(iconName, imageFile.toURL());
287
                                return true;
288
                        }
289
                }
290
                catch (NullPointerException ex1) {}
291
                catch (MalformedURLException ex2) {}
292
                return false;
293
        }
294
        
295
        /**
296
         * <p>Register in this theme the provided iconName and the associated
297
         * image. Developers
298
         * must not override icons already registered, as this defeats the
299
         * purpose of the IconTheme. Therefore, use the <code>exists</code>
300
         * method before using <code>register</code>, to ensure the icon
301
         * is not previously registered.</p> 
302
         * 
303
         * @param iconName The name of the icon to register. It is the name
304
         * that will be used later to retrieve the icon.
305
         *  
306
         * @param image The URL pointing to the image that is going to be
307
         * associated with the provided icon name.
308
         */
309
        public void register(String iconName, URL urlImage) {
310
                iconList.put(iconName, urlImage);
311
        }
312
        
313
        /**
314
         * Try to load an image from <code>imageURL</code>; if it it does not
315
         * point to a valid image, try with <code>fallbackURL</code>.
316
         * 
317
         * @param iconName
318
         * @param imageURL
319
         * @param fallbackURL
320
         * 
321
         * @return If any image was successfully loaded, return this image,
322
         * otherwise return <code>null</code>.
323
         */
324
        private ImageIcon tryToGetFromURLs(String iconName, URL imageURL, URL fallbackURL) {
325
                // let's see if imageURL is valid
326
                ImageIcon icon =  new ImageIcon(imageURL);
327
                if (icon.getImage()!=null) {
328
                        // the icon was successfully created from the imagePath
329
                        return icon;
330
                }
331
                else {
332
                        // it seems the imagePath was not valid, try with fallbackImage then
333
                        logger.error("Registered icon does not map to a valid image -- key: "+iconName+" -- URL: "+imageURL.toString());
334
                        icon =  new ImageIcon(fallbackURL);
335
                        if (icon.getImage()!=null) {
336
                                register(iconName, fallbackURL);
337
                                return icon;
338
                        }
339
                        else {
340
                                // we don't want to return an empty ImageIcon
341
                                logger.error("Provided icon does not map to a valid image -- key: "+iconName+" -- URL: "+fallbackURL.toString());
342
                                return null;
343
                        }
344
                }
345
        }
346
        
347
        /**
348
         * Return the URL which is currently associated with the
349
         * provided icon name, if this icon was registered as an
350
         * URL, or <code>null</code> if it is not present in the theme
351
         * or it was registered as an IconImage.
352
         * 
353
         * @param iconName
354
         * @return The URL which is currently associated with the
355
         * provided icon name, if this icon was registered as an
356
         * URL, or <code>null</code> if it is not present in the theme
357
         * or it was registered as an IconImage.
358
         */
359
        public URL getURL(String iconName) {
360
                Object object = iconList.get(iconName);
361
                if (object !=null && object instanceof URL)
362
                        return (URL) object;
363
                return null;
364
        }
365
        
366
        public IconThemeInfo getInfo() {
367
                return info;
368
        }
369
}