1 | package org.apollo.io;
|
---|
2 |
|
---|
3 | import java.net.URL;
|
---|
4 | import java.util.HashMap;
|
---|
5 |
|
---|
6 | import org.apollo.gui.Strokes;
|
---|
7 | import org.expeditee.core.Colour;
|
---|
8 | import org.expeditee.core.Image;
|
---|
9 | import org.expeditee.core.Line;
|
---|
10 | import org.expeditee.gio.EcosystemManager;
|
---|
11 | import org.expeditee.gio.GraphicsManager;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * An icon provider. Only loads icons once and when they are requested (simple proxy approach).
|
---|
15 | *
|
---|
16 | * @author Brook Novak
|
---|
17 | *
|
---|
18 | */
|
---|
19 | public final class IconRepository {
|
---|
20 |
|
---|
21 | private static HashMap<String, Image> _loadedIcons = new HashMap<String, Image>();
|
---|
22 |
|
---|
23 | private static Image missingIcon = null;
|
---|
24 |
|
---|
25 | private IconRepository()
|
---|
26 | {
|
---|
27 | }
|
---|
28 |
|
---|
29 | private static Image getMissingIcon()
|
---|
30 | {
|
---|
31 | if (missingIcon == null) {
|
---|
32 |
|
---|
33 | Image image = EcosystemManager.getImageManager().createImage(16, 16, false);
|
---|
34 |
|
---|
35 | GraphicsManager g = EcosystemManager.getGraphicsManager();
|
---|
36 |
|
---|
37 | g.pushDrawingSurface(image);
|
---|
38 |
|
---|
39 | g.clear(Colour.WHITE);
|
---|
40 |
|
---|
41 | Line l1 = new Line(0, 0, 16, 16);
|
---|
42 | Line l2 = new Line(0, 16, 16, 0);
|
---|
43 |
|
---|
44 | g.drawLine(l1, Colour.RED, Strokes.SOLID_2);
|
---|
45 | g.drawLine(l2, Colour.RED, Strokes.SOLID_2);
|
---|
46 |
|
---|
47 | g.popDrawingSurface();
|
---|
48 |
|
---|
49 | missingIcon = image;
|
---|
50 | }
|
---|
51 |
|
---|
52 | return missingIcon;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | *
|
---|
57 | * @param name Exclude path / package
|
---|
58 | * @return
|
---|
59 | */
|
---|
60 | public static Image getIcon(String name)
|
---|
61 | {
|
---|
62 | Image icon = _loadedIcons.get(name);
|
---|
63 |
|
---|
64 | if (icon == null) {
|
---|
65 | try {
|
---|
66 | URL url;
|
---|
67 | ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
---|
68 | if (classLoader!=null) {
|
---|
69 | // Applet friendly
|
---|
70 | url = classLoader.getResource("org/apollo/assets/icons/" + name);
|
---|
71 | } else {
|
---|
72 | url = ClassLoader.getSystemResource("org/apollo/assets/icons/" + name);
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (url != null) icon = Image.getImage(url);
|
---|
76 | } catch (Exception e) {
|
---|
77 | e.printStackTrace();
|
---|
78 | }
|
---|
79 |
|
---|
80 | if (icon == null) {
|
---|
81 | System.err.println("WARNING: Cannot find icon named \"" + name +"\"");
|
---|
82 | return getMissingIcon();
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | return icon;
|
---|
87 | }
|
---|
88 |
|
---|
89 | }
|
---|