gvsig-tools / 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 @ 699
History | View | Annotate | Download (12.8 KB)
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 |
} |