Revision 11041 trunk/frameworks/_fwAndami/src/com/iver/andami/iconthemes/IconTheme.java

View differences:

IconTheme.java
42 42
package com.iver.andami.iconthemes;
43 43

  
44 44
import java.io.File;
45
import java.io.IOException;
46
import java.io.InputStream;
45 47
import java.net.MalformedURLException;
46 48
import java.net.URL;
47 49
import java.util.HashMap;
50
import java.util.zip.ZipEntry;
51
import java.util.zip.ZipFile;
48 52

  
49 53
import javax.swing.ImageIcon;
50 54

  
51 55
import org.apache.log4j.Logger;
52 56

  
53
import com.iver.andami.PluginServices;
54

  
55 57
/**
56 58
 * <p>This class represents an icon theme, which is basically a mapping of
57 59
 * symbolic icon names, and real icons (or icon paths). This is useful to
......
74 76
 */
75 77
public class IconTheme {
76 78
	private HashMap iconList = new HashMap();
77
	private Logger logger = PluginServices.getLogger();
79
	private Logger logger;
78 80
	private IconThemeInfo info;
79 81
	
80 82
	/**
......
117 119
	public ImageIcon get(String iconName) {
118 120
		Object object = iconList.get(iconName);
119 121
		if (object!=null) {
120
			if (object instanceof String)
121
				return new ImageIcon((String)object);
122
			else if (object instanceof ImageIcon)
122
			if (object instanceof ImageIcon)
123 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
			}
124 157
		}
125 158
		return null;
126 159
	}
......
139 172
	 * <code>null</code> otherwise. 
140 173
	 */
141 174
	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
			}
175
		ImageIcon icon = get(iconName);
176
		if (icon!=null) {
177
			return icon;
160 178
		}
161
		// the key was not in the list
162
		register(iconName, fallbackImage);
163
		return fallbackImage;
179
		else {
180
			register(iconName, fallbackImage);
181
			return fallbackImage;
182
		}
164 183
	}
165 184
	
166 185
	/**
......
177 196
	 * <code>null</code> otherwise. 
178 197
	 */
179 198
	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);
199
		ImageIcon icon = get(iconName);
200
		if (icon!=null) {
193 201
			return icon;
194 202
		}
195 203
		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());
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
			}
198 213
			return null;
199 214
		}
200 215
	}
......
213 228
	 * <code>null</code> otherwise. 
214 229
	 */
215 230
	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) {
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 {
223 239
					File fallbackFile = new File(fallbackImage);
224
					return tryToGetFromURLs(iconName, (URL)object, fallbackFile.toURL());
240
					register(iconName, fallbackFile.toURL());
241
					return icon;
242
				} catch (MalformedURLException e) {
243
					e.printStackTrace();
244
					return null;
225 245
				}
226 246
			}
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 247
			else {
235 248
				// 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());
249
				getLogger().error("Provided icon does not map to a valid image -- key: "+iconName+" -- URL: "+fallbackImage.toString());
237 250
			}
251
			return null;
238 252
		}
239
		catch (MalformedURLException ex) {}
240
		return null;
241 253
	}
242 254
	
243 255

  
......
310 322
		iconList.put(iconName, urlImage);
311 323
	}
312 324
	
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
		}
325
	public void register(String iconName, ZipEntry zippedImage) {
326
		iconList.put(iconName, zippedImage);
345 327
	}
346 328
	
347 329
	/**
......
366 348
	public IconThemeInfo getInfo() {
367 349
		return info;
368 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
	}
369 362
}

Also available in: Unified diff