Revision 699
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/src/main/java/org/gvsig/tools/swing/impl/icontheme/BaseIconTheme.java | ||
---|---|---|
1 |
/* |
|
2 |
* Copyright (C) 2007 IVER T.I. and Generalitat Valenciana. |
|
3 |
* |
|
4 |
* This program is free software; you can redistribute it and/or |
|
5 |
* modify it under the terms of the GNU General Public License |
|
6 |
* as published by the Free Software Foundation; either version 2 |
|
7 |
* of the License, or (at your option) any later version. |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA. |
|
17 |
* |
|
18 |
* For more information, contact: |
|
19 |
* |
|
20 |
* Generalitat Valenciana |
|
21 |
* Conselleria d'Infraestructures i Transport |
|
22 |
* Av. Blasco Ib��ez, 50 |
|
23 |
* 46010 VALENCIA |
|
24 |
* SPAIN |
|
25 |
* |
|
26 |
* +34 963862235 |
|
27 |
* gvsig@gva.es |
|
28 |
* www.gvsig.gva.es |
|
29 |
* |
|
30 |
* or |
|
31 |
* |
|
32 |
* IVER T.I. S.A |
|
33 |
* Salamanca 50 |
|
34 |
* 46005 Valencia |
|
35 |
* Spain |
|
36 |
* |
|
37 |
* +34 963163400 |
|
38 |
* dac@iver.es |
|
39 |
*/ |
|
40 |
|
|
41 |
package org.gvsig.tools.swing.impl.icontheme; |
|
42 |
|
|
43 |
|
|
44 |
import java.awt.Image; |
|
45 |
import java.io.File; |
|
46 |
import java.io.IOException; |
|
47 |
import java.net.URL; |
|
48 |
import java.util.ArrayList; |
|
49 |
import java.util.Collections; |
|
50 |
import java.util.HashMap; |
|
51 |
import java.util.HashSet; |
|
52 |
import java.util.Iterator; |
|
53 |
import java.util.List; |
|
54 |
import java.util.Map; |
|
55 |
import java.util.Set; |
|
56 |
|
|
57 |
import javax.swing.ImageIcon; |
|
58 |
|
|
59 |
import org.apache.commons.io.FileUtils; |
|
60 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
|
61 |
import org.slf4j.Logger; |
|
62 |
import org.slf4j.LoggerFactory; |
|
63 |
|
|
64 |
|
|
65 |
/** |
|
66 |
* <p>This class represents an icon theme, which is basically a mapping of |
|
67 |
* symbolic icon names, and real icons (or icon paths). This is useful to |
|
68 |
* change an application's icons in an easy way. An icon theme |
|
69 |
* is usually read from disk on start up, but can also be created or |
|
70 |
* modified on a later time.</p> |
|
71 |
* |
|
72 |
*/ |
|
73 |
public class BaseIconTheme implements IconTheme { |
|
74 |
protected static Logger logger = LoggerFactory.getLogger(BaseIconTheme.class); |
|
75 |
|
|
76 |
protected String id = null; |
|
77 |
protected String name=null; |
|
78 |
protected String description=null; |
|
79 |
protected Map<String, Icon> iconList = null; |
|
80 |
protected IconTheme defaultTheme = null; |
|
81 |
protected String defaultIconName = null; |
|
82 |
|
|
83 |
class DefaultIcon implements Icon { |
|
84 |
private ImageIcon image; |
|
85 |
private URL resource; |
|
86 |
private String name; |
|
87 |
private String group; |
|
88 |
private String provider; |
|
89 |
|
|
90 |
DefaultIcon(String provider, String group, String name, ImageIcon image,URL resource) { |
|
91 |
this.image = image; |
|
92 |
this.resource = resource; |
|
93 |
this.group = group; |
|
94 |
this.name = name; |
|
95 |
this.provider = provider; |
|
96 |
} |
|
97 |
|
|
98 |
public ImageIcon getImageIcon() { |
|
99 |
if (this.image != null){ |
|
100 |
return this.image; |
|
101 |
} |
|
102 |
try { |
|
103 |
this.image = new ImageIcon((URL)this.resource); |
|
104 |
} catch(Exception ex) { |
|
105 |
return null; |
|
106 |
} |
|
107 |
return this.image; |
|
108 |
} |
|
109 |
|
|
110 |
public Image getImage() { |
|
111 |
ImageIcon icon = this.getImageIcon(); |
|
112 |
if( icon == null ) { |
|
113 |
return null; |
|
114 |
} |
|
115 |
return icon.getImage(); |
|
116 |
} |
|
117 |
|
|
118 |
public String getName() { |
|
119 |
return name; |
|
120 |
} |
|
121 |
|
|
122 |
public String getGroup() { |
|
123 |
return group; |
|
124 |
} |
|
125 |
|
|
126 |
public Object getResource() { |
|
127 |
return resource; |
|
128 |
} |
|
129 |
|
|
130 |
public URL getURL() { |
|
131 |
if( resource instanceof URL ) { |
|
132 |
return (URL) this.resource; |
|
133 |
} |
|
134 |
return null; |
|
135 |
} |
|
136 |
|
|
137 |
public String getLabel() { |
|
138 |
if( resource != null ) { |
|
139 |
return resource.toString(); |
|
140 |
} |
|
141 |
if( image != null ) { |
|
142 |
return image.toString(); |
|
143 |
} |
|
144 |
return ""; |
|
145 |
} |
|
146 |
|
|
147 |
public int compareTo(Icon other) { |
|
148 |
String this_id = this.getProviderName() + "/" + this.getGroup() + "/" + this.getName(); |
|
149 |
String other_id = other.getProviderName() + "/" + other.getGroup() + "/" + other.getName(); |
|
150 |
return this_id.compareTo(other_id); |
|
151 |
} |
|
152 |
|
|
153 |
public String getProviderName() { |
|
154 |
return provider; |
|
155 |
} |
|
156 |
|
|
157 |
} |
|
158 |
|
|
159 |
public BaseIconTheme() { |
|
160 |
this(null); |
|
161 |
} |
|
162 |
|
|
163 |
public BaseIconTheme(IconTheme defaultIconTheme) { |
|
164 |
this.setDefault(defaultIconTheme); |
|
165 |
this.id = "default"; // El id no traducirlo |
|
166 |
this.name = "default"; |
|
167 |
this.description = "Default icon theme"; |
|
168 |
this.iconList = new HashMap<String, Icon>(); |
|
169 |
} |
|
170 |
|
|
171 |
/** |
|
172 |
* Load the icons of the theme |
|
173 |
*/ |
|
174 |
public void load(Object resource) { |
|
175 |
// Do nothing. |
|
176 |
} |
|
177 |
|
|
178 |
/** |
|
179 |
* Override this to load icons on demand instead of |
|
180 |
* load on the creation of the theme. |
|
181 |
*/ |
|
182 |
protected void deferredLoad() { |
|
183 |
// Do nothing |
|
184 |
} |
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
private void logstack(String msg) { |
|
189 |
try { |
|
190 |
throw new IllegalArgumentException(); |
|
191 |
} catch (IllegalArgumentException e) { |
|
192 |
logger.debug(msg,e); |
|
193 |
} |
|
194 |
} |
|
195 |
|
|
196 |
private boolean isEmptyString(String s) { |
|
197 |
if( s==null ) { |
|
198 |
return true; |
|
199 |
} |
|
200 |
if( s.trim().length() == 0 ) { |
|
201 |
return true; |
|
202 |
} |
|
203 |
return false; |
|
204 |
} |
|
205 |
|
|
206 |
public void setDefault(IconTheme def){ |
|
207 |
if( def == this ) { |
|
208 |
defaultTheme = null; |
|
209 |
} else { |
|
210 |
defaultTheme = def; |
|
211 |
} |
|
212 |
} |
|
213 |
|
|
214 |
public IconTheme getDefault(){ |
|
215 |
return defaultTheme; |
|
216 |
} |
|
217 |
|
|
218 |
public boolean exists(String iconName) { |
|
219 |
if( isEmptyString(iconName) ) { |
|
220 |
return false; |
|
221 |
} |
|
222 |
deferredLoad(); |
|
223 |
|
|
224 |
if (iconList.containsKey(iconName)) { |
|
225 |
return true; |
|
226 |
} |
|
227 |
if (defaultTheme !=null && defaultTheme.exists(iconName)){ |
|
228 |
return true; |
|
229 |
} |
|
230 |
return false; |
|
231 |
} |
|
232 |
|
|
233 |
public Iterator<String> iterator() { |
|
234 |
Set<String> names = new HashSet<String>(); |
|
235 |
|
|
236 |
deferredLoad(); |
|
237 |
|
|
238 |
if( defaultTheme !=null ) { |
|
239 |
Iterator<String> it = defaultTheme.iterator(); |
|
240 |
while( it.hasNext() ) { |
|
241 |
names.add(it.next()); |
|
242 |
} |
|
243 |
} |
|
244 |
Iterator<String> it = iconList.keySet().iterator(); |
|
245 |
while( it.hasNext() ) { |
|
246 |
names.add(it.next()); |
|
247 |
} |
|
248 |
List<String> names2 = new ArrayList<String>(names); |
|
249 |
Collections.sort(names2); |
|
250 |
return names2.iterator(); |
|
251 |
} |
|
252 |
|
|
253 |
public Iterator<Icon> getThemeIcons() { |
|
254 |
Set<Icon> themeIcons = new HashSet<Icon>(); |
|
255 |
|
|
256 |
deferredLoad(); |
|
257 |
if( defaultTheme !=null ) { |
|
258 |
Iterator<Icon> it = defaultTheme.getThemeIcons(); |
|
259 |
while( it.hasNext() ) { |
|
260 |
themeIcons.add(it.next()); |
|
261 |
} |
|
262 |
} |
|
263 |
Iterator<Icon> it = iconList.values().iterator(); |
|
264 |
while( it.hasNext() ) { |
|
265 |
themeIcons.add(it.next()); |
|
266 |
} |
|
267 |
List<Icon> themeIcons2 = new ArrayList<Icon>(themeIcons); |
|
268 |
Collections.sort(themeIcons2); |
|
269 |
return themeIcons2.iterator(); |
|
270 |
} |
|
271 |
|
|
272 |
public Icon getThemeIcon(String name) { |
|
273 |
if( isEmptyString(name) ) { |
|
274 |
return null; |
|
275 |
} |
|
276 |
deferredLoad(); |
|
277 |
Icon themeIcon = (Icon) iconList.get(name); |
|
278 |
if( themeIcon!=null ) { |
|
279 |
return themeIcon; |
|
280 |
} |
|
281 |
if( defaultTheme!=null && defaultTheme.exists(name)) { |
|
282 |
return defaultTheme.getThemeIcon(name); |
|
283 |
} |
|
284 |
return null; |
|
285 |
} |
|
286 |
|
|
287 |
public ImageIcon get(String name) { |
|
288 |
ImageIcon icon = null; |
|
289 |
|
|
290 |
if( ! isEmptyString(name) ) { |
|
291 |
deferredLoad(); |
|
292 |
Icon themeIcon = (Icon) iconList.get(name); |
|
293 |
if( themeIcon != null ) { |
|
294 |
icon = themeIcon.getImageIcon(); |
|
295 |
if( icon != null ) { |
|
296 |
return icon; |
|
297 |
} |
|
298 |
} |
|
299 |
if( defaultTheme!=null && defaultTheme.exists(name)) { |
|
300 |
return defaultTheme.get(name); |
|
301 |
} |
|
302 |
} |
|
303 |
logger.info("get('"+name+"') icon not found"); |
|
304 |
logstack("get('"+name+"') icon not found"); |
|
305 |
return getNoIcon(); |
|
306 |
} |
|
307 |
|
|
308 |
|
|
309 |
public String getName() { |
|
310 |
return name; |
|
311 |
} |
|
312 |
|
|
313 |
public void setName(String themeName) { |
|
314 |
name = themeName; |
|
315 |
} |
|
316 |
|
|
317 |
public String getID() { |
|
318 |
return this.id; |
|
319 |
} |
|
320 |
|
|
321 |
public void setID(String id) { |
|
322 |
this.id = id; |
|
323 |
} |
|
324 |
|
|
325 |
public String getDescription() { |
|
326 |
return description; |
|
327 |
} |
|
328 |
|
|
329 |
public void setDescription(String description) { |
|
330 |
this.description = description; |
|
331 |
} |
|
332 |
|
|
333 |
/** |
|
334 |
* Returns the name of the icon theme |
|
335 |
*/ |
|
336 |
public String toString() { |
|
337 |
String s = null; |
|
338 |
if( isEmptyString(this.getName()) ) { |
|
339 |
s = this.getID(); |
|
340 |
} |
|
341 |
if( isEmptyString(this.getDescription()) ) { |
|
342 |
return s; |
|
343 |
} |
|
344 |
return s + " - " + this.getDescription(); |
|
345 |
} |
|
346 |
|
|
347 |
public ImageIcon getDefaultIcon() { |
|
348 |
ImageIcon imageIcon = null; |
|
349 |
Icon icon = null; |
|
350 |
|
|
351 |
icon = this.getThemeIcon(defaultIconName); |
|
352 |
if( icon != null ) { |
|
353 |
imageIcon = icon.getImageIcon(); |
|
354 |
if( imageIcon != null ) { |
|
355 |
return imageIcon; |
|
356 |
} |
|
357 |
} |
|
358 |
icon = this.getThemeIcon(NO_ICON_NAME); |
|
359 |
if( icon != null ) { |
|
360 |
imageIcon = icon.getImageIcon(); |
|
361 |
if( imageIcon != null ) { |
|
362 |
return imageIcon; |
|
363 |
} |
|
364 |
} |
|
365 |
return new ImageIcon(); |
|
366 |
} |
|
367 |
|
|
368 |
public void setDefaultIcon(ImageIcon icon) { |
|
369 |
this.defaultIconName = null; |
|
370 |
this.register(null, null, NO_ICON_NAME, icon, null); |
|
371 |
} |
|
372 |
|
|
373 |
public void setDefaultIcon(URL resource) { |
|
374 |
this.defaultIconName = null; |
|
375 |
this.register(null, null, NO_ICON_NAME, null, resource); |
|
376 |
} |
|
377 |
|
|
378 |
public void setDefaultIcon(String name) { |
|
379 |
this.defaultIconName = name; |
|
380 |
} |
|
381 |
|
|
382 |
public void register(String provider, String group, String name, |
|
383 |
ImageIcon icon, URL resource) { |
|
384 |
if( isEmptyString(name) ) { |
|
385 |
throw new IllegalArgumentException("name is empty"); |
|
386 |
} |
|
387 |
deferredLoad(); |
|
388 |
if( icon == null && resource == null ) { |
|
389 |
Icon themeIcon = new DefaultIcon(provider, group, name, null, null); |
|
390 |
iconList.put(name, themeIcon); |
|
391 |
throw new IllegalArgumentException("icon and resource for '"+getIconIdentifier(provider,group, name)+"' are null"); |
|
392 |
} |
|
393 |
Icon themeIcon = new DefaultIcon(provider, group, name, icon, resource); |
|
394 |
iconList.put(name, themeIcon); |
|
395 |
} |
|
396 |
|
|
397 |
private String getIconIdentifier(String provider, String group, String name) { |
|
398 |
deferredLoad(); |
|
399 |
String identifier = null; |
|
400 |
if( !isEmptyString(provider) ) { |
|
401 |
identifier = provider ; |
|
402 |
} |
|
403 |
if( group!= null ) { |
|
404 |
identifier = identifier + "/" + group; |
|
405 |
} |
|
406 |
if( name == null ) { |
|
407 |
identifier = identifier + "/unknow"; |
|
408 |
} else { |
|
409 |
identifier = identifier + "/" + name; |
|
410 |
} |
|
411 |
return identifier; |
|
412 |
} |
|
413 |
|
|
414 |
public void registerDefault(String provider, String group, |
|
415 |
String name, ImageIcon icon, URL resource) { |
|
416 |
deferredLoad(); |
|
417 |
if (defaultTheme!=null){ |
|
418 |
if( !defaultTheme.exists(name)) { |
|
419 |
defaultTheme.register(provider, group, name, icon, resource); |
|
420 |
} |
|
421 |
} else { |
|
422 |
this.register(provider, group, name, icon, resource); |
|
423 |
} |
|
424 |
} |
|
425 |
|
|
426 |
|
|
427 |
public void export(File folder) { |
|
428 |
if( !folder.exists() ) { |
|
429 |
folder.mkdir(); |
|
430 |
} |
|
431 |
folder = new File(folder, this.getID()); |
|
432 |
if( !folder.exists() ) { |
|
433 |
folder.mkdir(); |
|
434 |
} |
|
435 |
URL url_no_icon = this.getThemeIcon(NO_ICON_NAME).getURL(); |
|
436 |
|
|
437 |
Iterator<Icon> themeIcons = this.getThemeIcons(); |
|
438 |
while (themeIcons.hasNext()) { |
|
439 |
Icon themeIcon = themeIcons.next(); |
|
440 |
URL url_src = themeIcon.getURL(); |
|
441 |
if( url_src == null ) { |
|
442 |
url_src = url_no_icon; |
|
443 |
} |
|
444 |
File target; |
|
445 |
if( themeIcon.getGroup()!=null ) { |
|
446 |
target = new File(folder, themeIcon.getGroup()); |
|
447 |
target.mkdir(); |
|
448 |
} else { |
|
449 |
target = new File(folder.getAbsolutePath()); |
|
450 |
} |
|
451 |
target = new File(target,themeIcon.getName()+".png"); |
|
452 |
try { |
|
453 |
FileUtils.copyURLToFile(url_src,target); |
|
454 |
} catch (IOException e) { |
|
455 |
// TODO |
|
456 |
} |
|
457 |
} |
|
458 |
} |
|
459 |
|
|
460 |
// =================================================================== |
|
461 |
|
|
462 |
/** |
|
463 |
* @deprecated use getDefaultIcon |
|
464 |
*/ |
|
465 |
public ImageIcon getNoIcon() { |
|
466 |
return this.getDefaultIcon(); |
|
467 |
} |
|
468 |
|
|
469 |
|
|
470 |
/** |
|
471 |
* @deprecated use {@link #registerDefault(PluginServices, String, String, ImageIcon, Object)} |
|
472 |
*/ |
|
473 |
public void registerDefault(String name, ImageIcon image) { |
|
474 |
logstack("registerDefault('"+name+"'), deprecated method."); |
|
475 |
try { |
|
476 |
registerDefault(null, null, name, image, null); |
|
477 |
} catch(IllegalArgumentException e) { |
|
478 |
logger.info(e.getLocalizedMessage(), e); |
|
479 |
} |
|
480 |
} |
|
481 |
|
|
482 |
/** |
|
483 |
* @deprecated use {@link #registerDefault(PluginServices, String, String, ImageIcon, Object)} |
|
484 |
*/ |
|
485 |
public void registerDefault(String name, Object resource) { |
|
486 |
logstack("registerDefault('"+name+"'), deprecated method."); |
|
487 |
try { |
|
488 |
registerDefault(null, null, name, null, (URL)resource); |
|
489 |
} catch(IllegalArgumentException e) { |
|
490 |
logger.info(e.getLocalizedMessage(), e); |
|
491 |
} |
|
492 |
} |
|
493 |
|
|
494 |
/** |
|
495 |
* @deprecated use {@link #registerDefault(PluginServices, String, String, ImageIcon, Object)} |
|
496 |
*/ |
|
497 |
public void register(String name, ImageIcon image) { |
|
498 |
logstack("register('"+name+"'), deprecated method."); |
|
499 |
try { |
|
500 |
register(null, null, name, image, null); |
|
501 |
} catch(IllegalArgumentException e) { |
|
502 |
logger.info(e.getLocalizedMessage(), e); |
|
503 |
} |
|
504 |
} |
|
505 |
|
|
506 |
/** |
|
507 |
* @deprecated use {@link #registerDefault(PluginServices, String, String, ImageIcon, Object)} |
|
508 |
*/ |
|
509 |
public void register(String name, Object resource) { |
|
510 |
logstack("register('"+name+"'), deprecated method."); |
|
511 |
try { |
|
512 |
register(null, null, name, null, (URL)resource); |
|
513 |
} catch(IllegalArgumentException e) { |
|
514 |
logger.info(e.getLocalizedMessage(), e); |
|
515 |
} |
|
516 |
} |
|
517 |
|
|
518 |
/** |
|
519 |
* @deprecated use get(String iconName) instead. |
|
520 |
*/ |
|
521 |
public ImageIcon get(String iconName, ClassLoader loader) { |
|
522 |
logstack("get('"+iconName+"', loader), deprecated method."); |
|
523 |
return this.get(iconName); |
|
524 |
} |
|
525 |
|
|
526 |
} |
|
0 | 527 |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/src/main/java/org/gvsig/tools/swing/impl/icontheme/FolderIconTheme.java | ||
---|---|---|
1 |
package org.gvsig.tools.swing.impl.icontheme; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.io.FilenameFilter; |
|
5 |
import java.net.MalformedURLException; |
|
6 |
import java.util.HashMap; |
|
7 |
|
|
8 |
import org.gvsig.installer.lib.api.InstallerLocator; |
|
9 |
import org.gvsig.installer.lib.api.InstallerManager; |
|
10 |
import org.gvsig.installer.lib.api.PackageInfo; |
|
11 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
|
12 |
|
|
13 |
|
|
14 |
/** |
|
15 |
* This class extends AbstractIconTheme and implements the abstract methods of this |
|
16 |
* class. This methods are <code>loadIcon</code> and <code>load</code>. This methods |
|
17 |
* allows load one icon or all icons in the resource. |
|
18 |
*/ |
|
19 |
public class FolderIconTheme extends BaseIconTheme{ |
|
20 |
|
|
21 |
protected File resource = null; |
|
22 |
protected PackageInfo packageInfo = null; |
|
23 |
|
|
24 |
/** |
|
25 |
* Constructor.Constructs an Icon Theme with a default one. |
|
26 |
* @param def. The default icon theme |
|
27 |
*/ |
|
28 |
public FolderIconTheme(IconTheme def) { |
|
29 |
super(def); |
|
30 |
} |
|
31 |
|
|
32 |
public void load(Object resource) throws IllegalArgumentException { |
|
33 |
if( !(resource instanceof File) ) { |
|
34 |
throw new IllegalArgumentException(); |
|
35 |
} |
|
36 |
File res = (File) resource; |
|
37 |
if( !res.isDirectory() ) { |
|
38 |
throw new IllegalArgumentException(); |
|
39 |
} |
|
40 |
this.resource = res; |
|
41 |
this.iconList = null; |
|
42 |
|
|
43 |
this.loadPackageInfo(); |
|
44 |
this.setName(this.packageInfo.getName()); |
|
45 |
this.setDescription(this.packageInfo.getDescription()); |
|
46 |
} |
|
47 |
|
|
48 |
private void loadPackageInfo() { |
|
49 |
File pkgfile = new File(this.resource,"package.info"); |
|
50 |
InstallerManager manager = InstallerLocator.getInstallerManager(); |
|
51 |
try { |
|
52 |
this.packageInfo = manager.createPackageInfo(pkgfile.toURI().toURL().openStream()); |
|
53 |
} catch (Exception e) { |
|
54 |
throw new IllegalArgumentException(e); |
|
55 |
} |
|
56 |
|
|
57 |
} |
|
58 |
|
|
59 |
protected void deferredLoad() { |
|
60 |
if( this.iconList != null ) { |
|
61 |
return; |
|
62 |
} |
|
63 |
iconList = new HashMap<String, IconTheme.Icon>(); |
|
64 |
|
|
65 |
load(resource, ""); |
|
66 |
|
|
67 |
File[] files = resource.listFiles(); |
|
68 |
for (int i=files.length-1; i>=0; i--) { |
|
69 |
if( files[i].isDirectory() ) { |
|
70 |
load(files[i],files[i].getName()); |
|
71 |
} |
|
72 |
} |
|
73 |
} |
|
74 |
|
|
75 |
private void load(File folder, String group) { |
|
76 |
File[] imagefiles = folder.listFiles(new ImageFileFilter()); |
|
77 |
String name; |
|
78 |
for (int i=imagefiles.length-1; i>=0; i--) { |
|
79 |
name = computeKey(imagefiles[i].getName()); |
|
80 |
try { |
|
81 |
this.register(null, group, name, null, imagefiles[i].toURI().toURL()); |
|
82 |
} catch (MalformedURLException e) { |
|
83 |
logger.info("Can't load icon", e); |
|
84 |
} |
|
85 |
} |
|
86 |
} |
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
/** |
|
91 |
* This class allows filter the images in the directory. Allows to load the images |
|
92 |
* with a apropiate extension. |
|
93 |
*/ |
|
94 |
private class ImageFileFilter implements FilenameFilter { |
|
95 |
public boolean accept(File dir, String fileName) { |
|
96 |
String extension = ""; |
|
97 |
int pointPos = fileName.lastIndexOf("."); |
|
98 |
if (pointPos>0 && pointPos < (fileName.length()-1) ) |
|
99 |
extension = fileName.substring(pointPos+1).toLowerCase(); |
|
100 |
if ( extension.equals("jpg") || extension.equals("jpeg") |
|
101 |
|| extension.equals("png") |
|
102 |
|| extension.equals("gif") |
|
103 |
|
|
104 |
) |
|
105 |
return true; |
|
106 |
else |
|
107 |
return false; |
|
108 |
} |
|
109 |
} |
|
110 |
|
|
111 |
/** |
|
112 |
* Returns the key of the image without extension |
|
113 |
* @param fileName |
|
114 |
* @return string |
|
115 |
*/ |
|
116 |
private String computeKey(String fileName) { |
|
117 |
int pointPos = fileName.lastIndexOf("."); |
|
118 |
if (pointPos!=-1) |
|
119 |
return fileName.substring(0, pointPos); |
|
120 |
else |
|
121 |
return fileName; |
|
122 |
} |
|
123 |
|
|
124 |
} |
|
125 |
|
|
0 | 126 |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/src/main/java/org/gvsig/tools/swing/impl/icontheme/DefaultIconThemeManager.java | ||
---|---|---|
1 |
package org.gvsig.tools.swing.impl.icontheme; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.util.HashMap; |
|
5 |
import java.util.Iterator; |
|
6 |
import java.util.Map; |
|
7 |
|
|
8 |
import org.gvsig.tools.observer.ComplexObserver; |
|
9 |
import org.gvsig.tools.observer.Observable; |
|
10 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
|
11 |
import org.gvsig.tools.swing.icontheme.IconThemeManager; |
|
12 |
import org.gvsig.tools.util.FolderSet; |
|
13 |
import org.gvsig.tools.util.impl.DefaultFolderSet; |
|
14 |
|
|
15 |
public class DefaultIconThemeManager implements IconThemeManager , ComplexObserver{ |
|
16 |
private static IconThemeManager iconThemeManager = null; |
|
17 |
|
|
18 |
private IconTheme defaultTheme = null; |
|
19 |
private IconTheme currentTheme = null; |
|
20 |
private Map<String, IconTheme> themes = null; |
|
21 |
private FolderSet repository = null; |
|
22 |
|
|
23 |
public static IconThemeManager getIconThemeManager(){ |
|
24 |
if (iconThemeManager == null){ |
|
25 |
iconThemeManager = new DefaultIconThemeManager(); |
|
26 |
} |
|
27 |
return iconThemeManager; |
|
28 |
} |
|
29 |
|
|
30 |
public DefaultIconThemeManager() { |
|
31 |
this.defaultTheme = new BaseIconTheme(); |
|
32 |
this.currentTheme = this.defaultTheme; |
|
33 |
this.themes = null; |
|
34 |
this.repository = new DefaultFolderSet(); |
|
35 |
this.repository.addObserver(this); |
|
36 |
} |
|
37 |
|
|
38 |
public IconTheme getDefault() { |
|
39 |
return this.defaultTheme; |
|
40 |
} |
|
41 |
|
|
42 |
public void setCurrent(IconTheme iconTheme) { |
|
43 |
this.defaultTheme = iconTheme; |
|
44 |
} |
|
45 |
|
|
46 |
public IconTheme getCurrent() { |
|
47 |
return this.currentTheme; |
|
48 |
} |
|
49 |
|
|
50 |
public IconTheme get(String themeID) { |
|
51 |
return this.get(themeID); |
|
52 |
} |
|
53 |
|
|
54 |
public Iterator<IconTheme> iterator() { |
|
55 |
return this.getThemes().values().iterator(); |
|
56 |
} |
|
57 |
|
|
58 |
public boolean contains(IconTheme theme) { |
|
59 |
return this.getThemes().containsValue(theme); |
|
60 |
} |
|
61 |
|
|
62 |
public boolean add(IconTheme theme) { |
|
63 |
this.getThemes().put(theme.getID(), theme); |
|
64 |
return true; |
|
65 |
} |
|
66 |
|
|
67 |
public boolean remove(IconTheme theme) { |
|
68 |
return this.getThemes().remove(theme)==null ? false: true; |
|
69 |
} |
|
70 |
|
|
71 |
public void clear() { |
|
72 |
this.themes = null; |
|
73 |
} |
|
74 |
|
|
75 |
public FolderSet getRepository() { |
|
76 |
return this.repository; |
|
77 |
} |
|
78 |
|
|
79 |
public void update(Observable observable, Object notification) { |
|
80 |
if( observable != this.repository ) { |
|
81 |
return; |
|
82 |
} |
|
83 |
this.themes = null; |
|
84 |
} |
|
85 |
|
|
86 |
private Map<String, IconTheme> getThemes() { |
|
87 |
if( this.themes != null ) { |
|
88 |
return this.themes; |
|
89 |
} |
|
90 |
Map<String, IconTheme> themes = new HashMap<String, IconTheme>(); |
|
91 |
File[] folders = this.repository.listFiles(); |
|
92 |
for( int i=0; i<folders.length; i++ ) { |
|
93 |
if( folders[i].isDirectory() ) { |
|
94 |
FolderIconTheme theme = new FolderIconTheme(this.getDefault()); |
|
95 |
try { |
|
96 |
theme.load(folders[i]); |
|
97 |
themes.put(theme.getID(), theme); |
|
98 |
} catch( IllegalArgumentException ex) { |
|
99 |
// Do nothing |
|
100 |
} |
|
101 |
} |
|
102 |
} |
|
103 |
this.themes = themes; |
|
104 |
return themes; |
|
105 |
} |
|
106 |
} |
|
0 | 107 |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/src/main/java/org/gvsig/tools/swing/impl/icontheme/IconThemeZip.java | ||
---|---|---|
1 |
package org.gvsig.tools.swing.impl.icontheme; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.io.InputStream; |
|
5 |
import java.net.MalformedURLException; |
|
6 |
import java.net.URI; |
|
7 |
import java.net.URISyntaxException; |
|
8 |
import java.net.URL; |
|
9 |
import java.util.Enumeration; |
|
10 |
import java.util.HashMap; |
|
11 |
import java.util.zip.ZipEntry; |
|
12 |
import java.util.zip.ZipFile; |
|
13 |
|
|
14 |
import org.gvsig.installer.lib.api.InstallerLocator; |
|
15 |
import org.gvsig.installer.lib.api.InstallerManager; |
|
16 |
import org.gvsig.installer.lib.api.PackageInfo; |
|
17 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
|
18 |
|
|
19 |
/** |
|
20 |
* Extends BaseIconTheme to add support for themes stored in zip files. |
|
21 |
* |
|
22 |
* TODO: This class is under construction. |
|
23 |
*/ |
|
24 |
public class IconThemeZip extends BaseIconTheme{ |
|
25 |
|
|
26 |
protected File resource = null; |
|
27 |
protected PackageInfo packageInfo = null; |
|
28 |
|
|
29 |
public IconThemeZip(IconTheme def) { |
|
30 |
super(def); |
|
31 |
} |
|
32 |
|
|
33 |
public void load(Object resource) { |
|
34 |
if( !(resource instanceof File) ) { |
|
35 |
throw new IllegalArgumentException(); |
|
36 |
} |
|
37 |
File res = (File) resource; |
|
38 |
if( !res.isDirectory() ) { |
|
39 |
throw new IllegalArgumentException(); |
|
40 |
} |
|
41 |
this.resource = res; |
|
42 |
this.iconList = null; |
|
43 |
|
|
44 |
this.loadPackageInfo(); |
|
45 |
this.setName(this.packageInfo .getName()); |
|
46 |
this.setDescription(this.packageInfo.getDescription()); |
|
47 |
} |
|
48 |
|
|
49 |
private void loadPackageInfo() { |
|
50 |
InstallerManager manager = InstallerLocator.getInstallerManager(); |
|
51 |
ZipFile zipFile = null; |
|
52 |
try { |
|
53 |
zipFile = new ZipFile(resource); |
|
54 |
InputStream pkgstream = zipFile.getInputStream( zipFile.getEntry("packages.zip") ); |
|
55 |
this.packageInfo = manager.createPackageInfo(pkgstream); |
|
56 |
} catch (Exception e) { |
|
57 |
throw new IllegalArgumentException(e); |
|
58 |
} |
|
59 |
} |
|
60 |
|
|
61 |
protected void deferredLoad() { |
|
62 |
if (this.iconList != null) { |
|
63 |
return; |
|
64 |
} |
|
65 |
iconList = new HashMap<String, IconTheme.Icon>(); |
|
66 |
|
|
67 |
ZipFile zipFile = null; |
|
68 |
try { |
|
69 |
zipFile = new ZipFile(resource); |
|
70 |
} catch (Exception e) { |
|
71 |
throw new IllegalArgumentException(); |
|
72 |
} |
|
73 |
|
|
74 |
Enumeration<?> entries = zipFile.entries(); |
|
75 |
while (entries.hasMoreElements()) { |
|
76 |
ZipEntry entry = (ZipEntry) entries.nextElement(); |
|
77 |
if (!entry.isDirectory()) { |
|
78 |
try { |
|
79 |
if (isImage(entry.getName())) { |
|
80 |
String name = getNameOfEntry(entry); |
|
81 |
String group = getGroupOfEntry(entry); |
|
82 |
URL resource = getURLOfEntry(this.resource, entry); |
|
83 |
this.register(null, group, name, null, resource); |
|
84 |
} |
|
85 |
} catch (Exception e) { |
|
86 |
logger.info("Can't register icon. Entry = " +entry.toString(), e); |
|
87 |
} |
|
88 |
} |
|
89 |
} |
|
90 |
|
|
91 |
} |
|
92 |
|
|
93 |
private URL getURLOfEntry(File zip, ZipEntry entry) throws MalformedURLException, URISyntaxException { |
|
94 |
String s = "jar:" + zip.toString() + "!" + entry.getName(); |
|
95 |
return new URI(s).toURL(); |
|
96 |
} |
|
97 |
|
|
98 |
private String getGroupOfEntry(ZipEntry entry) { |
|
99 |
// FIXME: |
|
100 |
return ""; |
|
101 |
} |
|
102 |
|
|
103 |
private String getNameOfEntry(ZipEntry entry) { |
|
104 |
// FIXME: |
|
105 |
return entry.getName(); |
|
106 |
} |
|
107 |
|
|
108 |
private boolean isImage(String fileName) { |
|
109 |
String extension = ""; |
|
110 |
int pointPos = fileName.lastIndexOf("."); |
|
111 |
if (pointPos>0 && pointPos < (fileName.length()-1) ) |
|
112 |
extension = fileName.substring(pointPos+1).toLowerCase(); |
|
113 |
if ( extension.equals("jpg") || extension.equals("jpeg") |
|
114 |
|| extension.equals("png") |
|
115 |
|| extension.equals("gif")) |
|
116 |
return true; |
|
117 |
else |
|
118 |
return false; |
|
119 |
} |
|
120 |
|
|
121 |
} |
|
0 | 122 |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/pom.xml | ||
---|---|---|
20 | 20 |
<artifactId>org.gvsig.tools.swing.spi</artifactId> |
21 | 21 |
<version>3.0.0-SNAPSHOT</version> |
22 | 22 |
</dependency> |
23 |
<dependency> |
|
24 |
<groupId>org.apache.directory.studio</groupId> |
|
25 |
<artifactId>org.apache.commons.io</artifactId> |
|
26 |
<version>2.4</version> |
|
27 |
<scope>compile</scope> |
|
28 |
</dependency> |
|
29 |
<dependency> |
|
30 |
<groupId>org.gvsig</groupId> |
|
31 |
<artifactId>org.gvsig.installer.lib.api</artifactId> |
|
32 |
<scope>compile</scope> |
|
33 |
<version>1.0.1-SNAPSHOT</version> |
|
34 |
</dependency> |
|
23 | 35 |
</dependencies> |
24 | 36 |
</project> |
Also available in: Unified diff