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 @ 1845
History | View | Annotate | Download (16.5 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.tools.swing.impl.icontheme; |
25 |
|
26 |
import java.awt.Image; |
27 |
import java.io.File; |
28 |
import java.io.IOException; |
29 |
import java.io.InputStream; |
30 |
import java.net.URL; |
31 |
import java.util.ArrayList; |
32 |
import java.util.Collections; |
33 |
import java.util.HashMap; |
34 |
import java.util.HashSet; |
35 |
import java.util.Iterator; |
36 |
import java.util.List; |
37 |
import java.util.Map; |
38 |
import java.util.Set; |
39 |
|
40 |
import javax.swing.ImageIcon; |
41 |
|
42 |
import org.apache.commons.io.FileUtils; |
43 |
import org.apache.commons.lang3.StringUtils; |
44 |
import org.slf4j.Logger; |
45 |
import org.slf4j.LoggerFactory; |
46 |
|
47 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
48 |
|
49 |
/**
|
50 |
* <p>
|
51 |
* This class represents an icon theme, which is basically a mapping of symbolic
|
52 |
* icon names, and real icons (or icon paths). This is useful to change an
|
53 |
* application's icons in an easy way. An icon theme is usually read from disk
|
54 |
* on start up, but can also be created or modified on a later time.</p>
|
55 |
*
|
56 |
*/
|
57 |
public class BaseIconTheme implements IconTheme { |
58 |
|
59 |
protected static Logger logger = LoggerFactory.getLogger(BaseIconTheme.class); |
60 |
|
61 |
protected String id = null; |
62 |
protected String name = null; |
63 |
protected String description = null; |
64 |
protected Map<String, Icon> iconList = null; |
65 |
protected IconTheme defaultTheme = null; |
66 |
protected String defaultIconName = null; |
67 |
|
68 |
class DefaultIcon implements Icon { |
69 |
|
70 |
private ImageIcon image; |
71 |
private final URL resource; |
72 |
private final String name; |
73 |
private final String group; |
74 |
private final String provider; |
75 |
|
76 |
DefaultIcon(String provider, String group, String name, ImageIcon image, URL resource) { |
77 |
this.image = image;
|
78 |
this.resource = resource;
|
79 |
this.group = group;
|
80 |
this.name = name;
|
81 |
this.provider = provider;
|
82 |
} |
83 |
|
84 |
public boolean existsIcon() { |
85 |
if (image != null) { |
86 |
return true; |
87 |
} |
88 |
|
89 |
InputStream ist = null; |
90 |
boolean resp;
|
91 |
|
92 |
try {
|
93 |
ist = resource.openStream(); |
94 |
resp = true;
|
95 |
} catch (Exception ex) { |
96 |
resp = false;
|
97 |
} |
98 |
try {
|
99 |
if (ist != null) { |
100 |
ist.close(); |
101 |
} |
102 |
} catch (Exception ex) { |
103 |
} |
104 |
return resp;
|
105 |
} |
106 |
|
107 |
@Override
|
108 |
public ImageIcon getImageIcon() { |
109 |
if (this.image != null) { |
110 |
return this.image; |
111 |
} |
112 |
try {
|
113 |
this.image = new ImageIcon((URL) this.resource); |
114 |
} catch (Exception ex) { |
115 |
return null; |
116 |
} |
117 |
return this.image; |
118 |
} |
119 |
|
120 |
@Override
|
121 |
public Image getImage() { |
122 |
ImageIcon icon = this.getImageIcon(); |
123 |
if (icon == null) { |
124 |
return null; |
125 |
} |
126 |
return icon.getImage();
|
127 |
} |
128 |
|
129 |
@Override
|
130 |
public String getName() { |
131 |
return name;
|
132 |
} |
133 |
|
134 |
@Override
|
135 |
public String getGroup() { |
136 |
return group;
|
137 |
} |
138 |
|
139 |
@Override
|
140 |
public Object getResource() { |
141 |
return resource;
|
142 |
} |
143 |
|
144 |
@Override
|
145 |
public URL getURL() { |
146 |
if (resource instanceof URL) { |
147 |
return (URL) this.resource; |
148 |
} |
149 |
return null; |
150 |
} |
151 |
|
152 |
@Override
|
153 |
public String getLabel() { |
154 |
if (resource != null) { |
155 |
return resource.toString();
|
156 |
} |
157 |
if (image != null) { |
158 |
return image.toString();
|
159 |
} |
160 |
return ""; |
161 |
} |
162 |
|
163 |
@Override
|
164 |
public int compareTo(Icon other) { |
165 |
String this_id = this.getProviderName() + "/" + this.getGroup() + "/" + this.getName(); |
166 |
String other_id = other.getProviderName() + "/" + other.getGroup() + "/" + other.getName(); |
167 |
return this_id.compareTo(other_id);
|
168 |
} |
169 |
|
170 |
@Override
|
171 |
public String getProviderName() { |
172 |
return provider;
|
173 |
} |
174 |
|
175 |
} |
176 |
|
177 |
public BaseIconTheme() {
|
178 |
this(null); |
179 |
} |
180 |
|
181 |
public BaseIconTheme(IconTheme defaultIconTheme) {
|
182 |
this.setDefault(defaultIconTheme);
|
183 |
this.id = "default"; // El id no traducirlo |
184 |
this.name = "default"; |
185 |
this.description = "Default icon theme"; |
186 |
this.iconList = new HashMap<>(); |
187 |
} |
188 |
|
189 |
/**
|
190 |
* Load the icons of the theme
|
191 |
*
|
192 |
* @param resource
|
193 |
*/
|
194 |
@Override
|
195 |
public void load(Object resource) { |
196 |
// Do nothing.
|
197 |
} |
198 |
|
199 |
/**
|
200 |
* Override this to load icons on demand instead of load on the creation of
|
201 |
* the theme.
|
202 |
*/
|
203 |
protected void deferredLoad() { |
204 |
// Do nothing
|
205 |
} |
206 |
|
207 |
private void logstack(String msg) { |
208 |
try {
|
209 |
throw new IllegalArgumentException(); |
210 |
} catch (IllegalArgumentException e) { |
211 |
logger.debug(msg, e); |
212 |
} |
213 |
} |
214 |
|
215 |
@Override
|
216 |
public void setDefault(IconTheme def) { |
217 |
if (def == this) { |
218 |
defaultTheme = null;
|
219 |
} else {
|
220 |
defaultTheme = def; |
221 |
} |
222 |
} |
223 |
|
224 |
@Override
|
225 |
public IconTheme getDefault() {
|
226 |
return defaultTheme;
|
227 |
} |
228 |
|
229 |
@Override
|
230 |
public boolean exists(String iconName) { |
231 |
if (StringUtils.isEmpty(iconName)) {
|
232 |
return false; |
233 |
} |
234 |
deferredLoad(); |
235 |
|
236 |
if (iconList.containsKey(iconName)) {
|
237 |
return true; |
238 |
} |
239 |
return defaultTheme != null && defaultTheme.exists(iconName); |
240 |
} |
241 |
|
242 |
@Override
|
243 |
public Iterator<String> iterator() { |
244 |
Set<String> names = new HashSet<>(); |
245 |
|
246 |
deferredLoad(); |
247 |
|
248 |
if (defaultTheme != null) { |
249 |
Iterator<String> it = defaultTheme.iterator(); |
250 |
while (it.hasNext()) {
|
251 |
names.add(it.next()); |
252 |
} |
253 |
} |
254 |
Iterator<String> it = iconList.keySet().iterator(); |
255 |
while (it.hasNext()) {
|
256 |
names.add(it.next()); |
257 |
} |
258 |
List<String> names2 = new ArrayList<>(names); |
259 |
Collections.sort(names2);
|
260 |
return names2.iterator();
|
261 |
} |
262 |
|
263 |
@Override
|
264 |
public Iterator<Icon> getThemeIcons() { |
265 |
Map<String, Icon> themeIcons = new HashMap<>(); |
266 |
|
267 |
deferredLoad(); |
268 |
if (defaultTheme != null) { |
269 |
Iterator<Icon> it = defaultTheme.getThemeIcons(); |
270 |
while (it.hasNext()) {
|
271 |
Icon icon = it.next();
|
272 |
themeIcons.put(icon.getName(), icon); |
273 |
} |
274 |
} |
275 |
Iterator<Icon> it = iconList.values().iterator(); |
276 |
while (it.hasNext()) {
|
277 |
Icon icon = it.next();
|
278 |
themeIcons.put(icon.getName(), icon); |
279 |
} |
280 |
List<Icon> themeIcons2 = new ArrayList<>(themeIcons.values()); |
281 |
Collections.sort(themeIcons2);
|
282 |
return themeIcons2.iterator();
|
283 |
} |
284 |
|
285 |
@Override
|
286 |
public Icon getThemeIcon(String name) { |
287 |
if (StringUtils.isEmpty(name)) {
|
288 |
return null; |
289 |
} |
290 |
deferredLoad(); |
291 |
Icon themeIcon = (Icon) iconList.get(name); |
292 |
if (themeIcon != null) { |
293 |
return themeIcon;
|
294 |
} |
295 |
if (defaultTheme != null && defaultTheme.exists(name)) { |
296 |
return defaultTheme.getThemeIcon(name);
|
297 |
} |
298 |
return null; |
299 |
} |
300 |
|
301 |
@Override
|
302 |
public ImageIcon get(String name) { |
303 |
ImageIcon icon;
|
304 |
|
305 |
if (!StringUtils.isEmpty(name)) {
|
306 |
deferredLoad(); |
307 |
Icon themeIcon = (Icon) iconList.get(name); |
308 |
if (themeIcon != null) { |
309 |
icon = themeIcon.getImageIcon(); |
310 |
if (icon != null) { |
311 |
return icon;
|
312 |
} |
313 |
} |
314 |
if (defaultTheme != null && defaultTheme.exists(name)) { |
315 |
return defaultTheme.get(name);
|
316 |
} |
317 |
} |
318 |
logger.info("get('" + name + "') icon not found"); |
319 |
logstack("get('" + name + "') icon not found"); |
320 |
return getNoIcon();
|
321 |
} |
322 |
|
323 |
@Override
|
324 |
public String getName() { |
325 |
return name;
|
326 |
} |
327 |
|
328 |
@Override
|
329 |
public void setName(String themeName) { |
330 |
name = themeName; |
331 |
} |
332 |
|
333 |
@Override
|
334 |
public String getID() { |
335 |
return this.id; |
336 |
} |
337 |
|
338 |
@Override
|
339 |
public void setID(String id) { |
340 |
this.id = id;
|
341 |
} |
342 |
|
343 |
@Override
|
344 |
public String getDescription() { |
345 |
return description;
|
346 |
} |
347 |
|
348 |
@Override
|
349 |
public void setDescription(String description) { |
350 |
this.description = description;
|
351 |
} |
352 |
|
353 |
/**
|
354 |
* Returns the name of the icon theme
|
355 |
*/
|
356 |
@Override
|
357 |
public String toString() { |
358 |
String s = this.getName(); |
359 |
if (StringUtils.isEmpty(s)) {
|
360 |
s = this.getID();
|
361 |
} |
362 |
return s;
|
363 |
// if( StringUtils.isEmpty(this.getDescription()) ) {
|
364 |
// return s;
|
365 |
// }
|
366 |
// return s + " - " + this.getDescription();
|
367 |
} |
368 |
|
369 |
@Override
|
370 |
public ImageIcon getDefaultIcon() { |
371 |
ImageIcon imageIcon;
|
372 |
Icon icon;
|
373 |
|
374 |
icon = this.getThemeIcon(defaultIconName);
|
375 |
if (icon != null) { |
376 |
imageIcon = icon.getImageIcon(); |
377 |
if (imageIcon != null) { |
378 |
return imageIcon;
|
379 |
} |
380 |
} |
381 |
icon = this.getThemeIcon(NO_ICON_NAME);
|
382 |
if (icon != null) { |
383 |
imageIcon = icon.getImageIcon(); |
384 |
if (imageIcon != null) { |
385 |
return imageIcon;
|
386 |
} |
387 |
} |
388 |
return new ImageIcon(); |
389 |
} |
390 |
|
391 |
@Override
|
392 |
public void setDefaultIcon(ImageIcon icon) { |
393 |
this.defaultIconName = null; |
394 |
this.register(null, null, NO_ICON_NAME, icon, null); |
395 |
} |
396 |
|
397 |
@Override
|
398 |
public void setDefaultIcon(URL resource) { |
399 |
this.defaultIconName = null; |
400 |
this.register(null, null, NO_ICON_NAME, null, resource); |
401 |
} |
402 |
|
403 |
@Override
|
404 |
public void setDefaultIcon(String name) { |
405 |
this.defaultIconName = name;
|
406 |
} |
407 |
|
408 |
@Override
|
409 |
public void register(String provider, String group, String name, |
410 |
ImageIcon icon, URL resource) { |
411 |
if (StringUtils.isEmpty(name)) {
|
412 |
throw new IllegalArgumentException("name is empty"); |
413 |
} |
414 |
deferredLoad(); |
415 |
if (icon == null && resource == null) { |
416 |
Icon themeIcon = new DefaultIcon(provider, group, name, null, null); |
417 |
iconList.put(name, themeIcon); |
418 |
throw new IllegalArgumentException("icon and resource for '" + getIconIdentifier(provider, group, name) + "' are null"); |
419 |
} |
420 |
DefaultIcon themeIcon = new DefaultIcon(provider, group, name, icon, resource);
|
421 |
|
422 |
iconList.put(name, themeIcon); |
423 |
if (!themeIcon.existsIcon()) {
|
424 |
throw new IllegalArgumentException("Resource not found for icon '" + getIconIdentifier(provider, group, name) + "'"); |
425 |
} |
426 |
} |
427 |
|
428 |
private String getIconIdentifier(String provider, String group, String name) { |
429 |
deferredLoad(); |
430 |
String identifier = null; |
431 |
if (!StringUtils.isEmpty(provider)) {
|
432 |
identifier = provider; |
433 |
} |
434 |
if (group != null) { |
435 |
identifier = identifier + "/" + group;
|
436 |
} |
437 |
if (name == null) { |
438 |
identifier = identifier + "/unknow";
|
439 |
} else {
|
440 |
identifier = identifier + "/" + name;
|
441 |
} |
442 |
return identifier;
|
443 |
} |
444 |
|
445 |
@Override
|
446 |
public void registerDefault(String provider, String group, |
447 |
String name, ImageIcon icon, URL resource) { |
448 |
deferredLoad(); |
449 |
if (defaultTheme != null) { |
450 |
if (!defaultTheme.exists(name)) {
|
451 |
defaultTheme.register(provider, group, name, icon, resource); |
452 |
} |
453 |
} else {
|
454 |
this.register(provider, group, name, icon, resource);
|
455 |
} |
456 |
} |
457 |
|
458 |
@Override
|
459 |
public void export(File folder) { |
460 |
if (!folder.exists()) {
|
461 |
folder.mkdir(); |
462 |
} |
463 |
folder = new File(folder, this.getID()); |
464 |
if (!folder.exists()) {
|
465 |
folder.mkdir(); |
466 |
} |
467 |
URL url_no_icon = this.getThemeIcon(NO_ICON_NAME).getURL(); |
468 |
|
469 |
Iterator<Icon> themeIcons = this.getThemeIcons(); |
470 |
while (themeIcons.hasNext()) {
|
471 |
Icon themeIcon = themeIcons.next();
|
472 |
URL url_src = themeIcon.getURL();
|
473 |
if (url_src == null) { |
474 |
url_src = url_no_icon; |
475 |
} |
476 |
File target;
|
477 |
if (themeIcon.getGroup() != null) { |
478 |
target = new File(folder, themeIcon.getGroup()); |
479 |
target.mkdir(); |
480 |
} else {
|
481 |
target = new File(folder.getAbsolutePath()); |
482 |
} |
483 |
target = new File(target, themeIcon.getName() + ".png"); |
484 |
try {
|
485 |
FileUtils.copyURLToFile(url_src, target); |
486 |
} catch (IOException e) { |
487 |
// TODO
|
488 |
} |
489 |
} |
490 |
} |
491 |
|
492 |
// ===================================================================
|
493 |
/**
|
494 |
* @deprecated use getDefaultIcon
|
495 |
*/
|
496 |
@Override
|
497 |
public ImageIcon getNoIcon() { |
498 |
return this.getDefaultIcon(); |
499 |
} |
500 |
|
501 |
/**
|
502 |
* @param name
|
503 |
* @param image
|
504 |
* @deprecated use
|
505 |
* {@link #registerDefault(PluginServices, String, String, ImageIcon, Object)}
|
506 |
*/
|
507 |
@Override
|
508 |
public void registerDefault(String name, ImageIcon image) { |
509 |
logstack("registerDefault('" + name + "'), deprecated method."); |
510 |
try {
|
511 |
registerDefault(null, null, name, image, null); |
512 |
} catch (IllegalArgumentException e) { |
513 |
logger.info(e.getLocalizedMessage(), e); |
514 |
} |
515 |
} |
516 |
|
517 |
/**
|
518 |
* @param name
|
519 |
* @param resource
|
520 |
* @deprecated use
|
521 |
* {@link #registerDefault(PluginServices, String, String, ImageIcon, Object)}
|
522 |
*/
|
523 |
@Override
|
524 |
public void registerDefault(String name, Object resource) { |
525 |
logstack("registerDefault('" + name + "'), deprecated method."); |
526 |
try {
|
527 |
registerDefault(null, null, name, null, (URL) resource); |
528 |
} catch (IllegalArgumentException e) { |
529 |
logger.info(e.getLocalizedMessage(), e); |
530 |
} |
531 |
} |
532 |
|
533 |
/**
|
534 |
* @param name
|
535 |
* @param image
|
536 |
* @deprecated use
|
537 |
* {@link #registerDefault(PluginServices, String, String, ImageIcon, Object)}
|
538 |
*/
|
539 |
@Override
|
540 |
public void register(String name, ImageIcon image) { |
541 |
logstack("register('" + name + "'), deprecated method."); |
542 |
try {
|
543 |
register(null, null, name, image, null); |
544 |
} catch (IllegalArgumentException e) { |
545 |
logger.info(e.getLocalizedMessage(), e); |
546 |
} |
547 |
} |
548 |
|
549 |
/**
|
550 |
* @param name
|
551 |
* @param resource
|
552 |
* @deprecated use
|
553 |
* {@link #registerDefault(PluginServices, String, String, ImageIcon, Object)}
|
554 |
*/
|
555 |
@Override
|
556 |
public void register(String name, Object resource) { |
557 |
logstack("register('" + name + "'), deprecated method."); |
558 |
try {
|
559 |
register(null, null, name, null, (URL) resource); |
560 |
} catch (IllegalArgumentException e) { |
561 |
logger.info(e.getLocalizedMessage(), e); |
562 |
} |
563 |
} |
564 |
|
565 |
/**
|
566 |
* @param iconName
|
567 |
* @param loader
|
568 |
* @return
|
569 |
* @deprecated use get(String iconName) instead.
|
570 |
*/
|
571 |
@Override
|
572 |
public ImageIcon get(String iconName, ClassLoader loader) { |
573 |
logstack("get('" + iconName + "', loader), deprecated method."); |
574 |
return this.get(iconName); |
575 |
} |
576 |
|
577 |
} |