1
2
3
4
5
6
7
8
9
10
11 package de.jaret.util.swt;
12
13 import java.util.HashMap;
14 import java.util.Map;
15
16 import org.eclipse.swt.graphics.Color;
17 import org.eclipse.swt.graphics.Device;
18 import org.eclipse.swt.graphics.RGB;
19 import org.eclipse.swt.widgets.Display;
20
21
22
23
24
25
26
27 public class ColorManager {
28 protected static Map<Device, ColorManager> _instances = new HashMap<Device, ColorManager>();
29
30 protected Map<RGB, Color> _colorTable = new HashMap<RGB, Color>(10);
31 protected Device _device;
32
33 public ColorManager(Device device) {
34 if (device != null) {
35 _device = device;
36 } else {
37 _device = Display.getCurrent();
38 }
39 }
40
41 public static ColorManager getColorManager(Device device) {
42 ColorManager cm = _instances.get(device);
43 if (cm == null) {
44 cm = new ColorManager(device);
45 _instances.put(device, cm);
46 }
47 return cm;
48 }
49
50 public static void disposeAll() {
51 for (Device device : _instances.keySet()) {
52 ColorManager cm = getColorManager(device);
53 cm.dispose();
54 }
55 _instances.clear();
56 }
57
58 public void dispose() {
59 for (Color color : _colorTable.values()) {
60 color.dispose();
61 }
62 }
63
64 public Color getColor(RGB rgb) {
65 Color color = _colorTable.get(rgb);
66 if (color == null) {
67 color = new Color(_device, rgb);
68 _colorTable.put(rgb, color);
69 }
70 return color;
71 }
72 }