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