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

View differences:

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
}

Also available in: Unified diff