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