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);
24 protected Map fDisplayTable= new HashMap(2);
27 * Flag which tells if the colors are automatically disposed when
28 * the current display gets disposed.
30 private boolean fAutoDisposeOnDisplayDispose;
34 * Creates a new Java color manager which automatically
35 * disposes the allocated colors when the current display
38 public JavaColorManager() {
43 * Creates a new Java color manager.
45 * @param autoDisposeOnDisplayDispose if <code>true</code> the color manager
46 * automatically disposes all managed colors when the current display gets disposed
47 * and all calls to {@link org.eclipse.jface.text.source.ISharedTextColors#dispose()} are ignored.
51 public JavaColorManager(boolean autoDisposeOnDisplayDispose) {
52 fAutoDisposeOnDisplayDispose= autoDisposeOnDisplayDispose;
55 public void dispose(Display display) {
56 Map colorTable= (Map) fDisplayTable.get(display);
57 if (colorTable != null) {
58 Iterator e= colorTable.values().iterator();
60 Color color= (Color)e.next();
61 if (color != null && !color.isDisposed())
68 * @see IColorManager#getColor(RGB)
70 public Color getColor(RGB rgb) {
75 final Display display= Display.getCurrent();
76 Map colorTable= (Map) fDisplayTable.get(display);
77 if (colorTable == null) {
78 colorTable= new HashMap(10);
79 fDisplayTable.put(display, colorTable);
80 if (fAutoDisposeOnDisplayDispose) {
81 display.disposeExec(new Runnable() {
89 Color color= (Color) colorTable.get(rgb);
91 color= new Color(Display.getCurrent(), rgb);
92 colorTable.put(rgb, color);
99 * @see IColorManager#dispose
101 public void dispose() {
102 if (!fAutoDisposeOnDisplayDispose)
103 dispose(Display.getCurrent());
107 * @see IColorManager#getColor(String)
109 public Color getColor(String key) {
114 RGB rgb= (RGB) fKeyTable.get(key);
115 return getColor(rgb);
119 * @see IColorManagerExtension#bindColor(String, RGB)
121 public void bindColor(String key, RGB rgb) {
122 Object value= fKeyTable.get(key);
124 throw new UnsupportedOperationException();
126 fKeyTable.put(key, rgb);
130 * @see IColorManagerExtension#unbindColor(String)
132 public void unbindColor(String key) {
133 fKeyTable.remove(key);