Revision 10989

View differences:

trunk/frameworks/_fwAndami/src/com/iver/andami/iconthemes/IconTheme.java
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
import com.iver.andami.messages.Messages;
55

  
56
/**
57
 * <p>This class represents an icon theme, which is basically a mapping of
58
 * sybmolic icon names, and real icons (or icon paths). This is useful to
59
 * change an application's icons in an easy way. An icon theme
60
 * is usually read from disk on startup, but can also be created or
61
 * modified on a later time.</p>
62
 * 
63
 * <p>Developers are encouraged to always use the 
64
 * <code>get(iconName, fallbackImage)</code> methods to get icons,
65
 * as they ensure that the icons are not overwritten in the theme, but it
66
 * also ensures than an image is got in case the icon was still not
67
 * registered. Note that in this case, the iconName gets registered
68
 * (it is associated with the provided fallbackImage).
69
 * </p>
70
 * 
71
 * <p>Developers are encouraged to NOT override icons which are
72
 * present in the theme, as this defeats the purpose of IconThemes.</p>
73
 * 
74
 * @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
75
 */
76
public class IconTheme {
77
	HashMap iconList = new HashMap();
78
	Logger logger = PluginServices.getLogger();
79
	
80
	/**
81
	 * Returns <code>true</code> if the icon theme contains a mapping for the
82
	 * specified iconName.
83
	 * 
84
	 * @param iconName The key to check if it has been registered in this
85
	 * IconTheme
86
	 * 
87
	 * @return <code>true</code> if this IconTheme contains
88
	 * <code>iconName</code>, <code>false</code> otherwise.
89
	 */
90
	public boolean exists(String iconName) {
91
		return iconList.containsKey(iconName);
92
	}
93
	
94
	/**
95
	 * Gets the ImageIcon associated with the provided key, if the key
96
	 * is present in the theme, or returns <code>null</code> otherwise. 
97
	 * 
98
	 * @param iconName
99
	 * 			The key whose associated icon is to be retrieved
100
	 * 
101
	 * @return
102
	 * 			The icon associated with the provided key, or 
103
	 * <code>null</code> otherwise. 
104
	 */
105
	public ImageIcon get(String iconName) {
106
		Object object = iconList.get(iconName);
107
		if (object!=null) {
108
			if (object instanceof String)
109
				return new ImageIcon((String)object);
110
			else if (object instanceof ImageIcon)
111
				return (ImageIcon) object;
112
		}
113
		return null;
114
	}
115
	
116
	/**
117
	 * Gets the ImageIcon associated with the provided key, if the key
118
	 * is present in the theme, or register and returns the provided icon
119
	 * otherwise. 
120
	 * 
121
	 * @param iconName
122
	 * 			The key whose associated icon is to be retrieved
123
	 * @param fallbackImage
124
	 * 
125
	 * @return
126
	 * 			The icon associated with the provided key, or 
127
	 * <code>null</code> otherwise. 
128
	 */
129
	public ImageIcon get(String iconName, ImageIcon fallbackImage) {
130
		Object object = iconList.get(iconName);
131
		if (object!=null) {
132
			// key is registered
133
			if (object instanceof ImageIcon)
134
				return (ImageIcon) object;
135
			else if (object instanceof URL) {
136
				// ok, we got an imagePath, let's see if it's valid
137
				ImageIcon icon =  new ImageIcon((URL)object);
138
				if (icon.getImage()!=null)
139
					// the icon was succesfully created from the imagePath
140
					return icon;
141
				else {
142
					// it seems the imagePath was not valid, register the fallbackImage then
143
					logger.error("Registered icon does not map to a valid image -- key: "+iconName+" -- URL: "+object.toString());
144
					register(iconName, fallbackImage);
145
					return fallbackImage;
146
				}
147
			}
148
		}
149
		// the key was not in the list
150
		register(iconName, fallbackImage);
151
		return fallbackImage;
152
	}
153
	
154
	/**
155
	 * Gets the ImageIcon associated with the provided key, if the key
156
	 * is present in the theme, or register and returns the provided icon
157
	 * otherwise. 
158
	 * 
159
	 * @param iconName
160
	 * 			The key whose associated icon is to be retrieved
161
	 * @param fallbackImage
162
	 * 
163
	 * @return
164
	 * 			The icon associated with the provided key, or 
165
	 * <code>null</code> otherwise. 
166
	 */
167
	public ImageIcon get(String iconName, URL fallbackImage) {
168
		Object object = iconList.get(iconName);
169
		if (object!=null) {
170
			// key is registered
171
			if (object instanceof ImageIcon)
172
				return (ImageIcon) object;
173
			else if (object instanceof URL) {
174
				return tryToGetFromURLs(iconName, (URL) object, fallbackImage);
175
			}
176
		}
177
		// the key was not in the list
178
		ImageIcon icon =  new ImageIcon(fallbackImage);
179
		if (icon.getImage()!=null) {
180
			register(iconName, fallbackImage);
181
			return icon;
182
		}
183
		else {
184
			// we don't want to return an empty ImageIcon
185
			logger.error("Provided icon does not map to a valid image -- key: "+iconName+" -- URL: "+fallbackImage.toString());
186
			return null;
187
		}
188
	}
189
	
190
	/**
191
	 * Gets the ImageIcon associated with the provided key, if the key
192
	 * is present in the theme, or register and returns the provided icon
193
	 * otherwise. 
194
	 * 
195
	 * @param iconName
196
	 * 			The key whose associated icon is to be retrieved
197
	 * @param fallbackImage
198
	 * 
199
	 * @return
200
	 * 			The icon associated with the provided key, or 
201
	 * <code>null</code> otherwise. 
202
	 */
203
	public ImageIcon get(String iconName, String fallbackImage) {
204
		try {
205
			Object object = iconList.get(iconName);
206
			if (object!=null) {
207
				// key is registered
208
				if (object instanceof ImageIcon)
209
					return (ImageIcon) object;
210
				else if (object instanceof URL) {
211
					File fallbackFile = new File(fallbackImage);
212
					return tryToGetFromURLs(iconName, (URL)object, fallbackFile.toURL());
213
				}
214
			}
215
			// the key was not in the list
216
			ImageIcon icon = new ImageIcon(fallbackImage);
217
			if (icon.getImage()!=null) {
218
				File fallbackFile = new File(fallbackImage);
219
				register(iconName, fallbackFile.toURL());
220
				return icon;
221
			}
222
			else {
223
				// we don't want to return an empty ImageIcon
224
				logger.error("Provided icon does not map to a valid image -- key: "+iconName+" -- URL: "+fallbackImage.toString());
225
			}
226
		}
227
		catch (MalformedURLException ex) {}
228
		return null;
229
	}
230
	
231

  
232
	/**
233
	 * <p>Register in this theme the provided iconName and the associated
234
	 * image. Developers
235
	 * must not override icons already registered, as this defeats the
236
	 * purpose of the IconTheme. Therefore, use the <code>exists</code>
237
	 * method before using <code>register</code>, to ensure the icon
238
	 * is not previously registered.</p> 
239
	 * 
240
	 * @param iconName The name of the icon to register. It is the name
241
	 * that will be used later to retrieve the icon.
242
	 *  
243
	 * @param image The image that is going to be associated with the
244
	 * provided icon name.
245
	 */
246
	public void register(String iconName, ImageIcon image) {
247
		iconList.put(iconName, image);
248
	}
249
	
250
	/**
251
	 * <p>Register in this theme the provided iconName and the associated
252
	 * image. Developers
253
	 * must not override icons already registered, as this defeats the
254
	 * purpose of the IconTheme. Therefore, use the <code>exists</code>
255
	 * method before using <code>register</code>, to ensure the icon
256
	 * is not previously registered.</p> 
257
	 * 
258
	 * <p>Note that this method may fail if the provided path does not
259
	 * point to a valid image, so check the return value.</p>
260
	 * 
261
	 * @param iconName The name of the icon to register. It is the name
262
	 * that will be used later to retrieve the icon.
263
	 *  
264
	 * @param image The path to the image that is going to be
265
	 * associated with the provided icon name.
266
	 * 
267
	 * @return <code>true</code> if the provided path points to a
268
	 * valid file, <code>false</code> otherwise.
269
	 */
270
	public boolean register(String iconName, String imagePath) {
271
		try {
272
			File imageFile = new File(imagePath);
273
			if (imageFile.exists()) {
274
				iconList.put(iconName, imageFile.toURL());
275
				return true;
276
			}
277
		}
278
		catch (NullPointerException ex1) {}
279
		catch (MalformedURLException ex2) {}
280
		return false;
281
	}
282
	
283
	/**
284
	 * <p>Register in this theme the provided iconName and the associated
285
	 * image. Developers
286
	 * must not override icons already registered, as this defeats the
287
	 * purpose of the IconTheme. Therefore, use the <code>exists</code>
288
	 * method before using <code>register</code>, to ensure the icon
289
	 * is not previously registered.</p> 
290
	 * 
291
	 * @param iconName The name of the icon to register. It is the name
292
	 * that will be used later to retrieve the icon.
293
	 *  
294
	 * @param image The URL pointing to the image that is going to be
295
	 * associated with the provided icon name.
296
	 */
297
	public void register(String iconName, URL urlImage) {
298
		iconList.put(iconName, urlImage);
299
	}
300
	
301
	/**
302
	 * Try to load an image from <code>imageURL</code>; if it it does not
303
	 * point to a valid image, try with <code>fallbackURL</code>.
304
	 * 
305
	 * @param iconName
306
	 * @param imageURL
307
	 * @param fallbackURL
308
	 * 
309
	 * @return If any image was succesfully loaded, return this image,
310
	 * otherwise return <code>null</code>.
311
	 */
312
	private ImageIcon tryToGetFromURLs(String iconName, URL imageURL, URL fallbackURL) {
313
		// let's see if imageURL is valid
314
		ImageIcon icon =  new ImageIcon(imageURL);
315
		if (icon.getImage()!=null) {
316
			// the icon was succesfully created from the imagePath
317
			return icon;
318
		}
319
		else {
320
			// it seems the imagePath was not valid, try with fallbackImage then
321
			logger.error("Registered icon does not map to a valid image -- key: "+iconName+" -- URL: "+imageURL.toString());
322
			icon =  new ImageIcon(fallbackURL);
323
			if (icon.getImage()!=null) {
324
				register(iconName, fallbackURL);
325
				return icon;
326
			}
327
			else {
328
				// we don't want to return an empty ImageIcon
329
				logger.error("Provided icon does not map to a valid image -- key: "+iconName+" -- URL: "+fallbackURL.toString());
330
				return null;
331
			}
332
		}
333
	}
334
}
0 335

  

Also available in: Unified diff