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 @ 1847
History | View | Annotate | Download (17.7 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 |
@Override
|
323 |
public ImageIcon get(String name) { |
324 |
ImageIcon icon;
|
325 |
|
326 |
if (!StringUtils.isEmpty(name)) {
|
327 |
deferredLoad(); |
328 |
Icon themeIcon = (Icon) iconList.get(name); |
329 |
if (themeIcon != null) { |
330 |
icon = themeIcon.getImageIcon(); |
331 |
if (icon != null) { |
332 |
return icon;
|
333 |
} |
334 |
} |
335 |
if (defaultTheme != null && defaultTheme.exists(name)) { |
336 |
return defaultTheme.get(name);
|
337 |
} |
338 |
} |
339 |
logger.info("get('" + name + "') icon not found"); |
340 |
logstack("get('" + name + "') icon not found"); |
341 |
return getNoIcon();
|
342 |
} |
343 |
|
344 |
@Override
|
345 |
public String getName() { |
346 |
return name;
|
347 |
} |
348 |
|
349 |
@Override
|
350 |
public void setName(String themeName) { |
351 |
name = themeName; |
352 |
} |
353 |
|
354 |
@Override
|
355 |
public String getID() { |
356 |
return this.id; |
357 |
} |
358 |
|
359 |
@Override
|
360 |
public void setID(String id) { |
361 |
this.id = id;
|
362 |
} |
363 |
|
364 |
@Override
|
365 |
public String getDescription() { |
366 |
return description;
|
367 |
} |
368 |
|
369 |
@Override
|
370 |
public void setDescription(String description) { |
371 |
this.description = description;
|
372 |
} |
373 |
|
374 |
/**
|
375 |
* Returns the name of the icon theme
|
376 |
*/
|
377 |
@Override
|
378 |
public String toString() { |
379 |
String s = this.getName(); |
380 |
if (StringUtils.isEmpty(s)) {
|
381 |
s = this.getID();
|
382 |
} |
383 |
return s;
|
384 |
// if( StringUtils.isEmpty(this.getDescription()) ) {
|
385 |
// return s;
|
386 |
// }
|
387 |
// return s + " - " + this.getDescription();
|
388 |
} |
389 |
|
390 |
@Override
|
391 |
public ImageIcon getDefaultIcon() { |
392 |
ImageIcon imageIcon;
|
393 |
Icon icon;
|
394 |
|
395 |
icon = this.getThemeIcon(defaultIconName);
|
396 |
if (icon != null) { |
397 |
imageIcon = icon.getImageIcon(); |
398 |
if (imageIcon != null) { |
399 |
return imageIcon;
|
400 |
} |
401 |
} |
402 |
icon = this.getThemeIcon(NO_ICON_NAME);
|
403 |
if (icon != null) { |
404 |
imageIcon = icon.getImageIcon(); |
405 |
if (imageIcon != null) { |
406 |
return imageIcon;
|
407 |
} |
408 |
} |
409 |
return new ImageIcon(); |
410 |
} |
411 |
|
412 |
@Override
|
413 |
public void setDefaultIcon(ImageIcon icon) { |
414 |
this.defaultIconName = null; |
415 |
this.register(null, null, NO_ICON_NAME, icon, null); |
416 |
} |
417 |
|
418 |
@Override
|
419 |
public void setDefaultIcon(URL resource) { |
420 |
this.defaultIconName = null; |
421 |
this.register(null, null, NO_ICON_NAME, null, resource); |
422 |
} |
423 |
|
424 |
@Override
|
425 |
public void setDefaultIcon(String name) { |
426 |
this.defaultIconName = name;
|
427 |
} |
428 |
|
429 |
@Override
|
430 |
public void register(String provider, String group, String name, |
431 |
ImageIcon icon, URL resource) { |
432 |
if (StringUtils.isEmpty(name)) {
|
433 |
throw new IllegalArgumentException("name is empty"); |
434 |
} |
435 |
deferredLoad(); |
436 |
if (icon == null && resource == null) { |
437 |
Icon themeIcon = new DefaultIcon(provider, group, name, null, null); |
438 |
iconList.put(name, themeIcon); |
439 |
throw new IllegalArgumentException("icon and resource for '" + getIconIdentifier(provider, group, name) + "' are null"); |
440 |
} |
441 |
DefaultIcon themeIcon = new DefaultIcon(provider, group, name, icon, resource);
|
442 |
|
443 |
iconList.put(name, themeIcon); |
444 |
if (!themeIcon.existsIcon()) {
|
445 |
throw new IllegalArgumentException("Resource not found for icon '" + getIconIdentifier(provider, group, name) + "'"); |
446 |
} |
447 |
} |
448 |
|
449 |
private String getIconIdentifier(String provider, String group, String name) { |
450 |
deferredLoad(); |
451 |
String identifier = null; |
452 |
if (!StringUtils.isEmpty(provider)) {
|
453 |
identifier = provider; |
454 |
} |
455 |
if (group != null) { |
456 |
identifier = identifier + "/" + group;
|
457 |
} |
458 |
if (name == null) { |
459 |
identifier = identifier + "/unknow";
|
460 |
} else {
|
461 |
identifier = identifier + "/" + name;
|
462 |
} |
463 |
return identifier;
|
464 |
} |
465 |
|
466 |
@Override
|
467 |
public void registerDefault(String provider, String group, |
468 |
String name, ImageIcon icon, URL resource) { |
469 |
deferredLoad(); |
470 |
if (defaultTheme != null) { |
471 |
if (!defaultTheme.exists(name)) {
|
472 |
defaultTheme.register(provider, group, name, icon, resource); |
473 |
} |
474 |
} else {
|
475 |
this.register(provider, group, name, icon, resource);
|
476 |
} |
477 |
} |
478 |
|
479 |
@Override
|
480 |
public void export(File folder) { |
481 |
if (!folder.exists()) {
|
482 |
folder.mkdir(); |
483 |
} |
484 |
folder = new File(folder, this.getID()); |
485 |
if (!folder.exists()) {
|
486 |
folder.mkdir(); |
487 |
} |
488 |
URL url_no_icon = this.getThemeIcon(NO_ICON_NAME).getURL(); |
489 |
|
490 |
Iterator<Icon> themeIcons = this.getThemeIcons(); |
491 |
while (themeIcons.hasNext()) {
|
492 |
Icon themeIcon = themeIcons.next();
|
493 |
URL url_src = themeIcon.getURL();
|
494 |
if (url_src == null) { |
495 |
url_src = url_no_icon; |
496 |
} |
497 |
File target;
|
498 |
if (themeIcon.getGroup() != null) { |
499 |
target = new File(folder, themeIcon.getGroup()); |
500 |
target.mkdir(); |
501 |
} else {
|
502 |
target = new File(folder.getAbsolutePath()); |
503 |
} |
504 |
target = new File(target, themeIcon.getName() + ".png"); |
505 |
try {
|
506 |
FileUtils.copyURLToFile(url_src, target); |
507 |
} catch (IOException e) { |
508 |
// TODO
|
509 |
} |
510 |
} |
511 |
} |
512 |
|
513 |
// ===================================================================
|
514 |
/**
|
515 |
* @deprecated use getDefaultIcon
|
516 |
*/
|
517 |
@Override
|
518 |
public ImageIcon getNoIcon() { |
519 |
return this.getDefaultIcon(); |
520 |
} |
521 |
|
522 |
/**
|
523 |
* @param name
|
524 |
* @param image
|
525 |
* @deprecated use
|
526 |
* {@link #registerDefault(PluginServices, String, String, ImageIcon, Object)}
|
527 |
*/
|
528 |
@Override
|
529 |
public void registerDefault(String name, ImageIcon image) { |
530 |
logstack("registerDefault('" + name + "'), deprecated method."); |
531 |
try {
|
532 |
registerDefault(null, null, name, image, null); |
533 |
} catch (IllegalArgumentException e) { |
534 |
logger.info(e.getLocalizedMessage(), e); |
535 |
} |
536 |
} |
537 |
|
538 |
/**
|
539 |
* @param name
|
540 |
* @param resource
|
541 |
* @deprecated use
|
542 |
* {@link #registerDefault(PluginServices, String, String, ImageIcon, Object)}
|
543 |
*/
|
544 |
@Override
|
545 |
public void registerDefault(String name, Object resource) { |
546 |
logstack("registerDefault('" + name + "'), deprecated method."); |
547 |
try {
|
548 |
registerDefault(null, null, name, null, (URL) resource); |
549 |
} catch (IllegalArgumentException e) { |
550 |
logger.info(e.getLocalizedMessage(), e); |
551 |
} |
552 |
} |
553 |
|
554 |
/**
|
555 |
* @param name
|
556 |
* @param image
|
557 |
* @deprecated use
|
558 |
* {@link #registerDefault(PluginServices, String, String, ImageIcon, Object)}
|
559 |
*/
|
560 |
@Override
|
561 |
public void register(String name, ImageIcon image) { |
562 |
logstack("register('" + name + "'), deprecated method."); |
563 |
try {
|
564 |
register(null, null, name, image, null); |
565 |
} catch (IllegalArgumentException e) { |
566 |
logger.info(e.getLocalizedMessage(), e); |
567 |
} |
568 |
} |
569 |
|
570 |
/**
|
571 |
* @param name
|
572 |
* @param resource
|
573 |
* @deprecated use
|
574 |
* {@link #registerDefault(PluginServices, String, String, ImageIcon, Object)}
|
575 |
*/
|
576 |
@Override
|
577 |
public void register(String name, Object resource) { |
578 |
logstack("register('" + name + "'), deprecated method."); |
579 |
try {
|
580 |
register(null, null, name, null, (URL) resource); |
581 |
} catch (IllegalArgumentException e) { |
582 |
logger.info(e.getLocalizedMessage(), e); |
583 |
} |
584 |
} |
585 |
|
586 |
/**
|
587 |
* @param iconName
|
588 |
* @param loader
|
589 |
* @return
|
590 |
* @deprecated use get(String iconName) instead.
|
591 |
*/
|
592 |
@Override
|
593 |
public ImageIcon get(String iconName, ClassLoader loader) { |
594 |
logstack("get('" + iconName + "', loader), deprecated method."); |
595 |
return this.get(iconName); |
596 |
} |
597 |
|
598 |
} |