69d31c531eb5b55ad1bc04ab2b64c621be28034c
[phpeclipse.git] / net.sourceforge.phpeclipse / 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 org.eclipse.swt.graphics.Color;
13 import org.eclipse.swt.graphics.RGB;
14 import org.eclipse.swt.widgets.Display;
15
16 import net.sourceforge.phpdt.ui.text.IColorManager;
17 import net.sourceforge.phpdt.ui.text.IColorManagerExtension;
18
19 /**
20  * Java color manager.
21  */
22 public class JavaColorManager implements IColorManager, IColorManagerExtension {
23         
24         protected Map fKeyTable= new HashMap(10);
25         protected Map fDisplayTable= new HashMap(2);
26         
27         
28         public JavaColorManager() {
29         }
30         
31         private void dispose(Display display) {         
32                 Map colorTable= (Map) fDisplayTable.get(display);
33                 if (colorTable != null) {
34                         Iterator e= colorTable.values().iterator();
35                         while (e.hasNext())
36                                 ((Color) e.next()).dispose();
37                 }
38         }
39         
40         /*
41          * @see IColorManager#getColor(RGB)
42          */
43         public Color getColor(RGB rgb) {
44                 
45                 if (rgb == null)
46                         return null;
47                 
48                 final Display display= Display.getCurrent();
49                 Map colorTable= (Map) fDisplayTable.get(display);
50                 if (colorTable == null) {
51                         colorTable= new HashMap(10);
52                         fDisplayTable.put(display, colorTable);
53                         display.disposeExec(new Runnable() {
54                                 public void run() {
55                                         dispose(display);
56                                 }
57                         });
58                 }
59                 
60                 Color color= (Color) colorTable.get(rgb);
61                 if (color == null) {
62                         color= new Color(Display.getCurrent(), rgb);
63                         colorTable.put(rgb, color);
64                 }
65                 
66                 return color;
67         }
68         
69         /*
70          * @see IColorManager#dispose
71          */
72         public void dispose() {
73                 // nothing to dispose
74         }
75         
76         /*
77          * @see IColorManager#getColor(String)
78          */
79         public Color getColor(String key) {
80                 
81                 if (key == null)
82                         return null;
83                         
84                 RGB rgb= (RGB) fKeyTable.get(key);
85                 return getColor(rgb);
86         }
87         
88         /*
89          * @see IColorManagerExtension#bindColor(String, RGB)
90          */
91         public void bindColor(String key, RGB rgb) {
92                 Object value= fKeyTable.get(key);
93                 if (value != null)
94                         throw new UnsupportedOperationException();
95                 
96                 fKeyTable.put(key, rgb);
97         }
98
99         /*
100          * @see IColorManagerExtension#unbindColor(String)
101          */
102         public void unbindColor(String key) {
103                 fKeyTable.remove(key);
104         }
105 }