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