source: trunk/src/org/apollo/io/IconRepository.java

Last change on this file was 1102, checked in by davidb, 6 years ago

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

File size: 1.9 KB
Line 
1package org.apollo.io;
2
3import java.net.URL;
4import java.util.HashMap;
5
6import org.apollo.gui.Strokes;
7import org.expeditee.core.Colour;
8import org.expeditee.core.Image;
9import org.expeditee.core.Line;
10import org.expeditee.gio.EcosystemManager;
11import 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 */
19public 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}
Note: See TracBrowser for help on using the repository browser.