1 package net.sourceforge.phpdt.internal.ui.text;
4 * (c) Copyright IBM Corp. 2000, 2001.
8 import java.util.HashMap;
9 import java.util.Iterator;
12 import net.sourceforge.phpdt.ui.text.IColorManager;
13 import net.sourceforge.phpdt.ui.text.IColorManagerExtension;
15 import org.eclipse.swt.graphics.Color;
16 import org.eclipse.swt.graphics.RGB;
17 import org.eclipse.swt.widgets.Display;
22 public class JavaColorManager implements IColorManager, IColorManagerExtension {
23 protected Map fKeyTable = new HashMap(10);
25 protected Map fDisplayTable = new HashMap(2);
28 * Flag which tells if the colors are automatically disposed when the
29 * current display gets disposed.
31 private boolean fAutoDisposeOnDisplayDispose;
34 * Creates a new Java color manager which automatically disposes the
35 * allocated colors when the current display gets disposed.
37 public JavaColorManager() {
42 * Creates a new Java color manager.
44 * @param autoDisposeOnDisplayDispose
45 * if <code>true</code> the color manager automatically
46 * disposes all managed colors when the current display gets
47 * disposed and all calls to
48 * {@link org.eclipse.jface.text.source.ISharedTextColors#dispose()}
53 public JavaColorManager(boolean autoDisposeOnDisplayDispose) {
54 fAutoDisposeOnDisplayDispose = autoDisposeOnDisplayDispose;
57 public void dispose(Display display) {
58 Map colorTable = (Map) fDisplayTable.get(display);
59 if (colorTable != null) {
60 Iterator e = colorTable.values().iterator();
62 Color color = (Color) e.next();
63 if (color != null && !color.isDisposed())
70 * @see IColorManager#getColor(RGB)
72 public Color getColor(RGB rgb) {
77 final Display display = Display.getCurrent();
78 Map colorTable = (Map) fDisplayTable.get(display);
79 if (colorTable == null) {
80 colorTable = new HashMap(10);
81 fDisplayTable.put(display, colorTable);
82 if (fAutoDisposeOnDisplayDispose) {
83 display.disposeExec(new Runnable() {
91 Color color = (Color) colorTable.get(rgb);
93 color = new Color(Display.getCurrent(), rgb);
94 colorTable.put(rgb, color);
101 * @see IColorManager#dispose
103 public void dispose() {
104 if (!fAutoDisposeOnDisplayDispose)
105 dispose(Display.getCurrent());
109 * @see IColorManager#getColor(String)
111 public Color getColor(String key) {
116 RGB rgb = (RGB) fKeyTable.get(key);
117 return getColor(rgb);
121 * @see IColorManagerExtension#bindColor(String, RGB)
123 public void bindColor(String key, RGB rgb) {
124 Object value = fKeyTable.get(key);
126 throw new UnsupportedOperationException();
128 fKeyTable.put(key, rgb);
132 * @see IColorManagerExtension#unbindColor(String)
134 public void unbindColor(String key) {
135 fKeyTable.remove(key);