svn-gvsig-desktop / tags / J2ME_compat_v1_2_Build_1209 / frameworks / _fwAndami / src / com / iver / andami / iconthemes / IconThemeManager.java @ 19520
History | View | Annotate | Download (13.2 KB)
1 |
package com.iver.andami.iconthemes; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileInputStream; |
5 |
import java.io.FileNotFoundException; |
6 |
import java.io.IOException; |
7 |
import java.io.InputStream; |
8 |
import java.util.ArrayList; |
9 |
import java.util.Collection; |
10 |
import java.util.Enumeration; |
11 |
import java.util.Iterator; |
12 |
import java.util.zip.ZipEntry; |
13 |
import java.util.zip.ZipException; |
14 |
import java.util.zip.ZipFile; |
15 |
|
16 |
import org.kxml2.io.KXmlParser; |
17 |
import org.xmlpull.v1.XmlPullParserException; |
18 |
|
19 |
import com.iver.andami.Launcher; |
20 |
import com.iver.andami.PluginServices; |
21 |
|
22 |
/**
|
23 |
* This class controls the icon theme. Contains two themes, the first is a default
|
24 |
* theme and the second is the current theme. When it creates the class the default and
|
25 |
* the current theme are the same. Allows change the current theme, register a new theme,
|
26 |
* delete one,... and all the methods that the <code>Collection</code> interface contains.
|
27 |
* The themes are stored in an ArrayList.
|
28 |
*
|
29 |
* @author eustaquio
|
30 |
*/
|
31 |
public class IconThemeManager implements Collection{ |
32 |
|
33 |
private IIconTheme def;
|
34 |
private IIconTheme current;
|
35 |
private File themesDir = null; |
36 |
ArrayList themes=new ArrayList(); |
37 |
private final String themeDefinitionFile = "theme.xml"; |
38 |
private static IconThemeManager iconThemeManager = null; |
39 |
|
40 |
public static IconThemeManager getIconThemeManager(){ |
41 |
if (iconThemeManager == null){ |
42 |
iconThemeManager = new IconThemeManager();
|
43 |
} |
44 |
return iconThemeManager;
|
45 |
} |
46 |
|
47 |
|
48 |
|
49 |
|
50 |
/**
|
51 |
* Default constructor. Creates a iconThemeMemory by default and assigns it
|
52 |
* like default and current. The default theme is add to the themes ArrayList.
|
53 |
*/
|
54 |
public IconThemeManager(){
|
55 |
IconThemeMemory aux = new IconThemeMemory(null); |
56 |
def = aux; |
57 |
def.setName("Default");
|
58 |
current = aux; |
59 |
themes.add(def); |
60 |
|
61 |
} |
62 |
|
63 |
/**
|
64 |
* Gets the default theme
|
65 |
* @return the default theme.
|
66 |
*/
|
67 |
public IIconTheme getDefault(){
|
68 |
return def;
|
69 |
} |
70 |
|
71 |
/**
|
72 |
* Sets the iconTheme like current theme
|
73 |
* @param iconTheme
|
74 |
*/
|
75 |
public void setCurrent(IIconTheme iconTheme){ |
76 |
if(themes.contains(iconTheme)){
|
77 |
current = iconTheme; |
78 |
//saveConfig(current);
|
79 |
}else{
|
80 |
register(iconTheme); |
81 |
current = iconTheme; |
82 |
//saveConfig(current);
|
83 |
} |
84 |
} |
85 |
|
86 |
/**
|
87 |
* Gets the current theme
|
88 |
* @return current the current theme
|
89 |
*/
|
90 |
public IIconTheme getCurrent(){
|
91 |
return current;
|
92 |
} |
93 |
|
94 |
/**
|
95 |
* Stores a icon theme that receives like a parametre
|
96 |
* @param iconTheme
|
97 |
*/
|
98 |
public void register(IIconTheme iconTheme){ |
99 |
themes.add(iconTheme); |
100 |
iconTheme.setDefault(def); |
101 |
} |
102 |
|
103 |
/**
|
104 |
* Returns the theme that has been registered with the name that receives like
|
105 |
* parameter
|
106 |
* @param themeName
|
107 |
* @return the theme that contains like name in its properties the parameter
|
108 |
*/
|
109 |
public IIconTheme getTheme(String themeName){ |
110 |
for(int i = 0; i<themes.size();i++){ |
111 |
if( ((IIconTheme) themes.get(i)).getName().equals(themeName)) return (IIconTheme) themes.get(i); |
112 |
} |
113 |
return null; |
114 |
} |
115 |
|
116 |
/**
|
117 |
* Set the directory to read the icon themes.
|
118 |
*
|
119 |
* @param themesDir The directory in which the icon themes
|
120 |
* are stored
|
121 |
*
|
122 |
* @throws FileNotFoundException If the provided directory does not
|
123 |
* exist or is not a directory
|
124 |
*/
|
125 |
public void setThemesDir(File themesDir) throws FileNotFoundException { |
126 |
if (!themesDir.exists() || !themesDir.isDirectory())
|
127 |
throw new FileNotFoundException(); |
128 |
this.themesDir = themesDir;
|
129 |
} |
130 |
|
131 |
/**
|
132 |
* Gets the themes directory
|
133 |
* @return
|
134 |
*/
|
135 |
public File getThemesDir() { |
136 |
return themesDir;
|
137 |
} |
138 |
|
139 |
/**
|
140 |
*
|
141 |
* Save the provided theme as default in the configuration file.
|
142 |
* This will not change the default theme for this session, it will
|
143 |
* just write the new selection in the configuration file (for
|
144 |
* the next session).
|
145 |
*
|
146 |
* @param info The theme to be set as default for next session.
|
147 |
*/
|
148 |
private void saveConfig(IIconTheme info) { |
149 |
if (getThemesDir()==null || info==null || info.getResource()==null) |
150 |
return;
|
151 |
com.iver.andami.config.generate.AndamiOptions options = Launcher.getAndamiConfig().getAndamiOptions(); |
152 |
if (options==null) { |
153 |
options = new com.iver.andami.config.generate.AndamiOptions();
|
154 |
Launcher.getAndamiConfig().setAndamiOptions(options); |
155 |
} |
156 |
com.iver.andami.config.generate.IconTheme themeConfig = options.getIconTheme(); |
157 |
if (themeConfig==null) { |
158 |
themeConfig = new com.iver.andami.config.generate.IconTheme();
|
159 |
options.setIconTheme(themeConfig); |
160 |
} |
161 |
themeConfig.setBasedir(getThemesDir().getPath()); |
162 |
themeConfig.setName(info.getName()); |
163 |
themeConfig.setDescription(info.getDescription()); |
164 |
themeConfig.setVersion(info.getVersion()); |
165 |
if (info.getResource()!=null) { |
166 |
if (info.getResource() instanceof File) { |
167 |
File resource = (File) info.getResource(); |
168 |
themeConfig.setResource(resource.getName()); |
169 |
} |
170 |
else if (info.getResource() instanceof ZipFile) { |
171 |
ZipFile resource = (ZipFile) info.getResource(); |
172 |
themeConfig.setResource(resource.getName()); |
173 |
} |
174 |
} |
175 |
} |
176 |
|
177 |
/**
|
178 |
* Gets the Icon theme stored in the configuration
|
179 |
* @return IIconTheme
|
180 |
*/
|
181 |
public IIconTheme getIconThemeFromConfig() {
|
182 |
if (Launcher.getAndamiConfig().getAndamiOptions()==null |
183 |
|| Launcher.getAndamiConfig().getAndamiOptions().getIconTheme()==null
|
184 |
|| Launcher.getAndamiConfig().getAndamiOptions().getIconTheme().getResource()==null)
|
185 |
return null; |
186 |
|
187 |
com.iver.andami.config.generate.IconTheme selectedTheme = Launcher.getAndamiConfig().getAndamiOptions().getIconTheme(); |
188 |
|
189 |
try {
|
190 |
setThemesDir(new File(selectedTheme.getBasedir())); |
191 |
File themeFile = new File(selectedTheme.getBasedir()+File.separator+selectedTheme.getResource()); |
192 |
if (themeFile.exists()) {
|
193 |
IIconTheme info; |
194 |
try {
|
195 |
// try to load a ZIPPED theme
|
196 |
ZipFile zipfile = new ZipFile(selectedTheme.getResource()); |
197 |
info = readInfoFromZip(zipfile); |
198 |
return info;
|
199 |
} catch (ZipException e) { |
200 |
// ZIPPED theme failed, try to load from directory
|
201 |
if (themeFile.isFile() && themeFile.canRead()) {
|
202 |
info = readInfoFromDir(new File(selectedTheme.getResource())); |
203 |
return info;
|
204 |
} |
205 |
} catch (IOException e) {} |
206 |
} |
207 |
} catch (FileNotFoundException e1) {} |
208 |
return null; |
209 |
} |
210 |
|
211 |
/**
|
212 |
* Gets the base name of a entry
|
213 |
* @param fullname
|
214 |
* @return
|
215 |
*/
|
216 |
private String basename(String fullname) { |
217 |
String[] parts = fullname.split(File.separator+"|/"); |
218 |
return parts[parts.length-1]; |
219 |
} |
220 |
|
221 |
/**
|
222 |
* Returns an icon theme from a file that receives like a parameter
|
223 |
* @param zipFile
|
224 |
* @return
|
225 |
* @throws ZipException
|
226 |
* @throws IOException
|
227 |
*/
|
228 |
private IconThemeZip readInfoFromZip(File zipFile) throws ZipException, IOException { |
229 |
return readInfoFromZip(new ZipFile(zipFile)); |
230 |
} |
231 |
|
232 |
/**
|
233 |
* Returns an icon theme from a zip file that receives like a parameter
|
234 |
* @param zipFile
|
235 |
* @return IconThemeZip
|
236 |
* @throws IOException
|
237 |
*/
|
238 |
private IconThemeZip readInfoFromZip(ZipFile zipFile) throws IOException { |
239 |
IconThemeZip themeInfo; |
240 |
Enumeration entries = zipFile.entries();
|
241 |
ZipEntry xmlEntry=null, dirEntry=null; |
242 |
// search for theme.xml and the directory names
|
243 |
while (entries.hasMoreElements() && (xmlEntry==null||dirEntry==null)) { |
244 |
ZipEntry entry = (ZipEntry) entries.nextElement(); |
245 |
if (entry.isDirectory()) {
|
246 |
dirEntry = entry; |
247 |
} |
248 |
if (basename(entry.getName()).equals(themeDefinitionFile)) {
|
249 |
xmlEntry = entry; |
250 |
} |
251 |
} |
252 |
|
253 |
try {
|
254 |
// try with the XML file
|
255 |
if (xmlEntry!=null) { |
256 |
themeInfo = readXML(zipFile.getInputStream(xmlEntry)); |
257 |
if (themeInfo.getDescription()==null) |
258 |
themeInfo.setDescription(zipFile.getName()); |
259 |
if (themeInfo.getName()==null) |
260 |
themeInfo.setName(themeInfo.getDescription()); |
261 |
themeInfo.setResource(zipFile); |
262 |
return themeInfo;
|
263 |
} |
264 |
} catch (XmlPullParserException e) {
|
265 |
PluginServices.getLogger().error("Error loading theme "+zipFile.getName()+". ", e); |
266 |
} |
267 |
|
268 |
themeInfo = new IconThemeZip(def);
|
269 |
themeInfo.setResource(zipFile); |
270 |
// now try with the directory
|
271 |
if (dirEntry!=null) { |
272 |
themeInfo.setName(dirEntry.getName()); |
273 |
themeInfo.setDescription(dirEntry.getName()); |
274 |
return themeInfo;
|
275 |
} |
276 |
else { // otherwise just use the zipName |
277 |
themeInfo.setName(zipFile.getName()); |
278 |
themeInfo.setDescription(zipFile.getName()); |
279 |
return themeInfo;
|
280 |
} |
281 |
} |
282 |
|
283 |
/**
|
284 |
* Read the properties of IconThemeZip from a XML
|
285 |
* @param xmlStream
|
286 |
* @return IconThemeZip
|
287 |
* @throws XmlPullParserException
|
288 |
* @throws IOException
|
289 |
*/
|
290 |
private IconThemeZip readXML(InputStream xmlStream) throws XmlPullParserException, IOException { |
291 |
KXmlParser parser = new KXmlParser();
|
292 |
// we use null encoding, in this way kxml2 tries to detect the encoding
|
293 |
parser.setInput(xmlStream, null);
|
294 |
IconThemeZip themeInfo=new IconThemeZip(def);
|
295 |
for (parser.next(); parser.getEventType()!=KXmlParser.END_DOCUMENT; parser.next()) {
|
296 |
// este bucle externo recorre las etiquetas de primer y segundo nivel
|
297 |
if (parser.getEventType()==KXmlParser.START_TAG) {
|
298 |
if (parser.getName().equals("name")) { |
299 |
themeInfo.setName(parser.nextText()); |
300 |
} |
301 |
else if (parser.getName().equals("description")) { |
302 |
themeInfo.setDescription(parser.nextText()); |
303 |
} |
304 |
else if (parser.getName().equals("version")) { |
305 |
themeInfo.setVersion(parser.nextText()); |
306 |
} |
307 |
} |
308 |
} |
309 |
return themeInfo;
|
310 |
} |
311 |
|
312 |
/**
|
313 |
* Returns a IconThemeDir from a directory
|
314 |
* @param dir
|
315 |
* @return
|
316 |
*/
|
317 |
private IconThemeDir readInfoFromDir(File dir){ |
318 |
File themeDefinition = new File(dir + File.separator + themeDefinitionFile); |
319 |
IconThemeDir themeInfo; |
320 |
try {
|
321 |
// try reading the XML file
|
322 |
if (themeDefinition.exists() && themeDefinition.isFile()) {
|
323 |
themeInfo = readXMLDir(new FileInputStream(themeDefinition)); |
324 |
if (themeInfo.getDescription()==null) |
325 |
themeInfo.setDescription(dir.getName()); |
326 |
if (themeInfo.getName()==null) |
327 |
themeInfo.setName(themeInfo.getDescription()); |
328 |
themeInfo.setResource(dir); |
329 |
return themeInfo;
|
330 |
} |
331 |
} catch (IOException e) {} |
332 |
catch (XmlPullParserException e) {
|
333 |
e.printStackTrace(); |
334 |
} |
335 |
// the XML parsing failed, just show the dir name
|
336 |
themeInfo = new IconThemeDir(def);
|
337 |
themeInfo.setName(dir.getName()); |
338 |
themeInfo.setDescription(dir.getName()); |
339 |
themeInfo.setResource(dir); |
340 |
return themeInfo;
|
341 |
} |
342 |
|
343 |
/**
|
344 |
* Read the properties of IconThemeDir from a XML
|
345 |
* @param xmlStream
|
346 |
* @return
|
347 |
* @throws XmlPullParserException
|
348 |
* @throws IOException
|
349 |
*/
|
350 |
private IconThemeDir readXMLDir(InputStream xmlStream) throws XmlPullParserException, IOException { |
351 |
KXmlParser parser = new KXmlParser();
|
352 |
// we use null encoding, in this way kxml2 tries to detect the encoding
|
353 |
parser.setInput(xmlStream, null);
|
354 |
IconThemeDir themeInfo=new IconThemeDir(def);
|
355 |
for (parser.next(); parser.getEventType()!=KXmlParser.END_DOCUMENT; parser.next()) {
|
356 |
// este bucle externo recorre las etiquetas de primer y segundo nivel
|
357 |
if (parser.getEventType()==KXmlParser.START_TAG) {
|
358 |
if (parser.getName().equals("name")) { |
359 |
themeInfo.setName(parser.nextText()); |
360 |
} |
361 |
else if (parser.getName().equals("description")) { |
362 |
themeInfo.setDescription(parser.nextText()); |
363 |
} |
364 |
else if (parser.getName().equals("version")) { |
365 |
themeInfo.setVersion(parser.nextText()); |
366 |
} |
367 |
} |
368 |
} |
369 |
return themeInfo;
|
370 |
} |
371 |
|
372 |
/*
|
373 |
* (non-Javadoc)
|
374 |
* @see java.util.Collection#size()
|
375 |
*/
|
376 |
public int size() { |
377 |
return themes.size();
|
378 |
} |
379 |
/*
|
380 |
* (non-Javadoc)
|
381 |
* @see java.util.Collection#isEmpty()
|
382 |
*/
|
383 |
public boolean isEmpty() { |
384 |
if (themes.size()==0)return true; |
385 |
else return false; |
386 |
} |
387 |
|
388 |
/*
|
389 |
* (non-Javadoc)
|
390 |
* @see java.util.Collection#contains(java.lang.Object)
|
391 |
*/
|
392 |
public boolean contains(Object o) { |
393 |
return themes.contains(o);
|
394 |
} |
395 |
|
396 |
/*
|
397 |
* (non-Javadoc)
|
398 |
* @see java.lang.Iterable#iterator()
|
399 |
*/
|
400 |
public Iterator iterator() { |
401 |
return themes.iterator();
|
402 |
} |
403 |
|
404 |
/*
|
405 |
* (non-Javadoc)
|
406 |
* @see java.util.Collection#toArray()
|
407 |
*/
|
408 |
public Object[] toArray() { |
409 |
return themes.toArray();
|
410 |
} |
411 |
|
412 |
/*
|
413 |
* (non-Javadoc)
|
414 |
* @see java.util.Collection#toArray(Object[] a)
|
415 |
*/
|
416 |
public Object[] toArray(Object[] a) { |
417 |
return themes.toArray(a);
|
418 |
} |
419 |
|
420 |
/*
|
421 |
* (non-Javadoc)
|
422 |
* @see java.util.Collection#remove(java.lang.Object)
|
423 |
*/
|
424 |
public boolean add(Object o) { |
425 |
return themes.add((IIconTheme) o);
|
426 |
} |
427 |
|
428 |
/*
|
429 |
* (non-Javadoc)
|
430 |
* @see java.util.Collection#remove(java.lang.Object)
|
431 |
*/
|
432 |
public boolean remove(Object o) { |
433 |
return themes.remove(o);
|
434 |
} |
435 |
|
436 |
/*
|
437 |
* (non-Javadoc)
|
438 |
* @see java.util.Collection#containsAll(Collection)
|
439 |
*/
|
440 |
public boolean containsAll(Collection c) { |
441 |
return themes.containsAll(c);
|
442 |
} |
443 |
|
444 |
/*
|
445 |
* (non-Javadoc)
|
446 |
* @see java.util.Collection#addAll(Collection)
|
447 |
*/
|
448 |
public boolean addAll(Collection c) { |
449 |
return themes.addAll(c);
|
450 |
} |
451 |
|
452 |
/*
|
453 |
* (non-Javadoc)
|
454 |
* @see java.util.Collection#removeAll(Collection)
|
455 |
*/
|
456 |
public boolean removeAll(Collection c) { |
457 |
return themes.removeAll(c);
|
458 |
} |
459 |
|
460 |
/*
|
461 |
* (non-Javadoc)
|
462 |
* @see java.util.Collection#retainAll(Collection)
|
463 |
*/
|
464 |
public boolean retainAll(Collection c) { |
465 |
return themes.retainAll(c);
|
466 |
} |
467 |
|
468 |
/*
|
469 |
* (non-Javadoc)
|
470 |
* @see java.util.Collection#clear()
|
471 |
*/
|
472 |
public void clear() { |
473 |
themes.clear(); |
474 |
} |
475 |
|
476 |
} |