7578353e9d419e7d2b929ceaa23b848e406a54c1
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / text / JavaColorManager.java
1 package net.sourceforge.phpdt.internal.ui.text;
2
3 /*
4  * (c) Copyright IBM Corp. 2000, 2001.
5  * All Rights Reserved.
6  */
7
8 import java.util.HashMap;
9 import java.util.Iterator;
10 import java.util.Map;
11
12 import net.sourceforge.phpdt.ui.text.IColorManager;
13 import net.sourceforge.phpdt.ui.text.IColorManagerExtension;
14
15 import org.eclipse.swt.graphics.Color;
16 import org.eclipse.swt.graphics.RGB;
17 import org.eclipse.swt.widgets.Display;
18
19 /**
20  * Java color manager.
21  */
22 public class JavaColorManager implements IColorManager, IColorManagerExtension {
23         protected Map fKeyTable = new HashMap(10);
24
25         protected Map fDisplayTable = new HashMap(2);
26
27         /**
28          * Flag which tells if the colors are automatically disposed when the
29          * current display gets disposed.
30          */
31         private boolean fAutoDisposeOnDisplayDispose;
32
33         /**
34          * Creates a new Java color manager which automatically disposes the
35          * allocated colors when the current display gets disposed.
36          */
37         public JavaColorManager() {
38                 this(true);
39         }
40
41         /**
42          * Creates a new Java color manager.
43          * 
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()}
49          *            are ignored.
50          * 
51          * @since 2.1
52          */
53         public JavaColorManager(boolean autoDisposeOnDisplayDispose) {
54                 fAutoDisposeOnDisplayDispose = autoDisposeOnDisplayDispose;
55         }
56
57         public void dispose(Display display) {
58                 Map colorTable = (Map) fDisplayTable.get(display);
59                 if (colorTable != null) {
60                         Iterator e = colorTable.values().iterator();
61                         while (e.hasNext()) {
62                                 Color color = (Color) e.next();
63                                 if (color != null && !color.isDisposed())
64                                         color.dispose();
65                         }
66                 }
67         }
68
69         /*
70          * @see IColorManager#getColor(RGB)
71          */
72         public Color getColor(RGB rgb) {
73
74                 if (rgb == null)
75                         return null;
76
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() {
84                                         public void run() {
85                                                 dispose(display);
86                                         }
87                                 });
88                         }
89                 }
90
91                 Color color = (Color) colorTable.get(rgb);
92                 if (color == null) {
93                         color = new Color(Display.getCurrent(), rgb);
94                         colorTable.put(rgb, color);
95                 }
96
97                 return color;
98         }
99
100         /*
101          * @see IColorManager#dispose
102          */
103         public void dispose() {
104                 if (!fAutoDisposeOnDisplayDispose)
105                         dispose(Display.getCurrent());
106         }
107
108         /*
109          * @see IColorManager#getColor(String)
110          */
111         public Color getColor(String key) {
112
113                 if (key == null)
114                         return null;
115
116                 RGB rgb = (RGB) fKeyTable.get(key);
117                 return getColor(rgb);
118         }
119
120         /*
121          * @see IColorManagerExtension#bindColor(String, RGB)
122          */
123         public void bindColor(String key, RGB rgb) {
124                 Object value = fKeyTable.get(key);
125                 if (value != null)
126                         throw new UnsupportedOperationException();
127
128                 fKeyTable.put(key, rgb);
129         }
130
131         /*
132          * @see IColorManagerExtension#unbindColor(String)
133          */
134         public void unbindColor(String key) {
135                 fKeyTable.remove(key);
136         }
137 }