Revision 11041

View differences:

trunk/frameworks/_fwAndami/src/com/iver/andami/iconthemes/IconThemeManager.java
57 57
import org.kxml2.io.KXmlParser;
58 58
import org.xmlpull.v1.XmlPullParserException;
59 59

  
60
import sun.security.action.GetIntegerAction;
60
import com.iver.andami.Launcher;
61
import com.iver.andami.Utilities;
61 62

  
62 63
/**
63 64
 * <p>This class deals with icon themes, it understands the on-disk theme
......
81 82
public class IconThemeManager {
82 83
	private File themesDir = null;
83 84
	private final String themeDefinitionFile = "theme.xml";
85
	private IconTheme defaultTheme = null;
84 86

  
85 87
	/**
86 88
	 * 
......
92 94
		this.themesDir = themesDir;	
93 95
	}
94 96
	
97
	/**
98
	 * 
99
	 * @param themesDir
100
	 */
101
	public IconThemeManager() {	}
95 102
	
103
	
96 104
	public IconTheme get(String themeName) {
97 105
		IconThemeInfo[] themeList = list();
98 106
		for (int i=0; i<themeList.length; i++) {
......
105 113
	
106 114
	public IconTheme get(IconThemeInfo themeInfo) {
107 115
		IconTheme theme = new IconTheme(themeInfo);
108
		if (themeInfo.getResource().isDirectory()) {
109
			File basedir = themeInfo.getResource();
110

  
111
			//TODO: crear el filtro de im?genes
112
			File[] imageList = basedir.listFiles(new FilenameFilter() {
113
				public boolean accept(File dir, String fileName) {
114
					String extension = "";
115
					int pointPos = fileName.lastIndexOf(".");
116
					if (pointPos>0 && pointPos < (fileName.length()-1) )
117
						extension = fileName.substring(pointPos+1).toLowerCase();
118
					if ( extension.equals("jpg") || extension.equals("jpeg")
119
							|| extension.equals("png")
120
							|| extension.equals("gif")
121
							
122
							)
123
						return true;
124
					else
125
						return false;
116
		if (themeInfo.getResource()==null) {
117
			// no real theme was selected
118
			return theme;
119
		}
120
		if (themeInfo.getResource() instanceof File) {
121
			File basedir = (File) themeInfo.getResource();
122
			if (basedir.isDirectory()) {
123
				//TODO: crear el filtro de im?genes
124
				File[] imageList = basedir.listFiles(new ImageFileFilter());
125
				String name;
126
				for (int i=imageList.length-1; i>=0; i--) {
127
					name = computeKey(imageList[i].getName());
128
					try {
129
						theme.register(name, imageList[i].toURL());
130
						System.out.println("registerning "+name+": "+imageList[i].toURL());
131
					} catch (MalformedURLException e) {}
126 132
				}
127
			});
128
			
129
			int pointPos;
130
			String name;
131
			for (int i=imageList.length-1; i>=0; i--) {
132
				pointPos = imageList[i].getName().lastIndexOf(".");
133
				name = imageList[i].getName().substring(0, pointPos);
134
				try {
135
					theme.register(name, imageList[i].toURL());
136
					System.out.println("registerning "+name+": "+imageList[i].toURL());
137
				} catch (MalformedURLException e) {}
138 133
			}
134
			else {
135
				return null;
136
			}
139 137
		}
140
		else {
141
			
138
		else if (themeInfo.getResource() instanceof ZipFile) {
139
			ZipFile zipFile = (ZipFile) themeInfo.getResource();
140
			ImageFileFilter filter = new ImageFileFilter();
141
			Enumeration entries = zipFile.entries();
142
			while (entries.hasMoreElements()) {
143
				ZipEntry entry = (ZipEntry) entries.nextElement();
144
				if (!entry.isDirectory() ) {
145
					if (filter.accept(new File(zipFile.getName()), entry.getName())) {
146
						theme.register(computeKey(entry.getName()), entry);
147
						System.out.println("registerning "+computeKey(entry.getName())+": "+entry.getName());
148
					}
149
				}
150
			}
142 151
		}
143 152
		return theme;
144 153
	}
145 154
	
155

  
156
	public IconTheme getDefault() {
157
		if (defaultTheme!=null) 
158
			return defaultTheme;
159
		init();
160
		return defaultTheme;
161
	}
162
	
163
	public void init() {
164
		IconThemeInfo info = readSelectedFromAndamiConfig();
165
		defaultTheme = get(info);
166
	}
167
	
168

  
169
	
170
	public void setDefault(IconThemeInfo iconTheme) {
171
		/*
172
		 *  TODO Currently we just change it in AndamiConfig, so that on next
173
		 *  gvSIG restart the theme gets changed. However, we could implement
174
		 *  here a real on-the-fly theme change.
175
		 */
176
		saveSelectedInAndamiConfig(iconTheme);
177
	}
178
	
179
	public void setDefault(IconTheme iconTheme) {
180
		setDefault(iconTheme.getInfo());
181
	}
182
	
146 183
	/**
147 184
	 * 
148 185
	 * @return
......
176 213
			// try reading the XML file
177 214
			if (themeDefinition.exists() && themeDefinition.isFile()) {
178 215
				themeInfo = readXML(new FileInputStream(themeDefinition));
216
				if (themeInfo.getDescription()==null)
217
					themeInfo.setDescription(dir.getName());
218
				if (themeInfo.getName()==null)
219
					themeInfo.setName(themeInfo.getDescription());
179 220
				themeInfo.setResource(dir);
180 221
				return themeInfo;
181 222
			}
......
209 250
					themeInfo.setVersion(parser.nextText());
210 251
				}
211 252
			}
212
		}	
253
		}
213 254
		return themeInfo;
214 255
	}
215 256
	
......
234 275
				// try with the XML file
235 276
				if (xmlEntry!=null) {
236 277
					themeInfo = readXML(file.getInputStream(xmlEntry));
278
					if (themeInfo.getDescription()==null)
279
						themeInfo.setDescription(zipFile.getName());
280
					if (themeInfo.getName()==null)
281
						themeInfo.setName(themeInfo.getDescription());
237 282
					themeInfo.setResource(zipFile);
238 283
					return themeInfo;
239 284
				}
......
245 290
			themeInfo = new IconThemeInfo();
246 291
			themeInfo.setResource(zipFile);
247 292
			// now try with the directory
248
			if (dirEntry!=null) {
249
				
293
			if (dirEntry!=null) {		
250 294
				themeInfo.setName(dirEntry.getName());
251 295
				themeInfo.setDescription(dirEntry.getName());
252 296
				return themeInfo;
......
272 316
		return parts[parts.length-1];
273 317
	}
274 318
	
275
	public File getThemesPath() {
319
	public File getThemesDir() {
276 320
		return themesDir;
277 321
	}
278 322
	
279
	public void setThemesPath(File themesDir) {
323
	public void setThemesDir(File themesDir) {
280 324
		this.themesDir = themesDir;
281 325
	}
282 326
	
283
	public IconTheme getDefault() {
284
		return new IconTheme(new IconThemeInfo());
285
		// TODO
327
	private IconThemeInfo readSelectedFromAndamiConfig() {
328
		com.iver.andami.config.generate.IconTheme selectedTheme = null;
329
		if (Launcher.getAndamiConfig().getAndamiOptions()!=null && Launcher.getAndamiConfig().getAndamiOptions().getIconTheme()!=null)
330
			selectedTheme = Launcher.getAndamiConfig().getAndamiOptions().getIconTheme();
331
		IconThemeInfo info = new IconThemeInfo();
332
		if (selectedTheme!=null) {
333
			info.setName(selectedTheme.getName());
334
			if (selectedTheme.getResource()!=null) {
335
				try {
336
					info.setResource(new ZipFile(selectedTheme.getResource()));
337
				} catch (ZipException e) {
338
					info.setResource(new File(selectedTheme.getResource()));
339
				} catch (IOException e) {
340
					info.setResource(new File(selectedTheme.getResource()));
341
				}
342
			}
343
			else {
344
				info.setResource(null);
345
			}
346
			setThemesDir(new File(selectedTheme.getBasedir()));
347
			if (selectedTheme.getVersion()!=null)
348
				info.setVersion(selectedTheme.getVersion());
349
			if (selectedTheme.getDescription()!=null) {
350
				info.setDescription(selectedTheme.getDescription());
351
			}
352
			else {
353
				info.setDescription(selectedTheme.getName());
354
			}
355
		}
356
		else {
357
			info.setName("default");
358
			info.setDescription("default");
359
			info.setResource(null); // null resource means that no real theme is loaded
360
		}
361
		return info;
286 362
	}
287 363
	
288
	public void setDefault(IconThemeInfo iconTheme) {
289
		// TODO
364
	private void saveSelectedInAndamiConfig(IconThemeInfo selectedTheme) {
365
		com.iver.andami.config.generate.AndamiOptions options = Launcher.getAndamiConfig().getAndamiOptions();
366
		if (options==null) {
367
			options = new com.iver.andami.config.generate.AndamiOptions();
368
		}
369
		com.iver.andami.config.generate.IconTheme themeConfig = options.getIconTheme();
370
		if (themeConfig==null) {
371
			themeConfig = new com.iver.andami.config.generate.IconTheme(); 
372
		}
373
		themeConfig.setName(selectedTheme.getName());
374
		themeConfig.setDescription(selectedTheme.getDescription());
375
		themeConfig.setVersion(selectedTheme.getVersion());
376
		if (selectedTheme.getResource()!=null) {
377
			if (selectedTheme.getResource() instanceof File) {
378
				File resource = (File) selectedTheme.getResource();
379
				themeConfig.setResource(resource.getName());				
380
			}
381
			else if (selectedTheme.getResource() instanceof ZipFile) {
382
				ZipFile resource = (ZipFile) selectedTheme.getResource();
383
				themeConfig.setResource(resource.getName());				
384
			}
385
		}
290 386
	}
291 387
	
292
public void setDefault(IconTheme iconTheme) {
293
		setDefault(iconTheme.getInfo());
388
	class ImageFileFilter implements FilenameFilter {
389
		public boolean accept(File dir, String fileName) {
390
			String extension = "";
391
			int pointPos = fileName.lastIndexOf(".");
392
			if (pointPos>0 && pointPos < (fileName.length()-1) )
393
				extension = fileName.substring(pointPos+1).toLowerCase();
394
			if ( extension.equals("jpg") || extension.equals("jpeg")
395
					|| extension.equals("png")
396
					|| extension.equals("gif")
397
					
398
					)
399
				return true;
400
			else
401
				return false;
402
		}
294 403
	}
404
	
405
	private String computeKey(String fileName) {
406
		int pointPos = fileName.lastIndexOf(".");
407
		if (pointPos!=-1)
408
			return fileName.substring(0, pointPos);
409
		else
410
			return fileName;
411
	}
295 412
}
trunk/frameworks/_fwAndami/src/com/iver/andami/iconthemes/IconThemeInfo.java
50 50
 *
51 51
 */
52 52
public class IconThemeInfo {
53
	private String name;
54
	private String description;
53
	private String name=null;
54
	private String description=null;
55 55
	private String version="1.0";
56
	private File resource;
56
	private Object resource=null;
57 57
	
58 58
	/**
59 59
	 * Gets the theme name.
......
113 113
	}
114 114
	
115 115
	/**
116
	 * Gets the file which contains physically contains this theme on disk.
117
	 * It may be a zipfile or jarfile, or a directory.
116
	 * Gets the Object which contains physically contains this theme on disk.
117
	 * It may be a ZipFile or JarFile, or a directory.
118 118
	 * 
119 119
	 * @return
120 120
	 */
121
	public File getResource() {
121
	public Object getResource() {
122 122
		return resource;
123 123
	}
124 124
	
125 125
	/**
126 126
	 * Sets the file which contains physically contains this theme on disk.
127
	 * It may be a zipfile or jarfile, or a directory.
127
	 * It may be a ZipFile or JarFile, or a directory.
128 128
	 * 
129 129
	 * @return
130 130
	 */
131
	public void setResource(File resource) {
131
	public void setResource(Object resource) {
132 132
		this.resource = resource;
133 133
	}
134 134
}
trunk/frameworks/_fwAndami/src/com/iver/andami/iconthemes/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