new version with WorkingCopy Management
[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 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         protected Map fDisplayTable= new HashMap(2);
25
26         /** 
27          * Flag which tells if the colors are automatically disposed when
28          * the current display gets disposed.
29          */
30         private boolean fAutoDisposeOnDisplayDispose;
31
32
33         /**
34          * Creates a new Java color manager which automatically
35          * disposes the allocated colors when the current display
36          * gets disposed.
37          */     
38         public JavaColorManager() {
39                 this(true);
40         }
41
42         /**
43          * Creates a new Java color manager.
44          * 
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.
48          * 
49          * @since 2.1
50          */
51         public JavaColorManager(boolean autoDisposeOnDisplayDispose) {
52                 fAutoDisposeOnDisplayDispose= autoDisposeOnDisplayDispose;
53         }
54         
55         public void dispose(Display display) {
56                 Map colorTable= (Map) fDisplayTable.get(display);
57                 if (colorTable != null) {
58                         Iterator e= colorTable.values().iterator();
59                         while (e.hasNext()) {
60                                 Color color= (Color)e.next();
61                                 if (color != null && !color.isDisposed())
62                                         color.dispose();
63                         }
64                 }
65         }
66         
67         /*
68          * @see IColorManager#getColor(RGB)
69          */
70         public Color getColor(RGB rgb) {
71                 
72                 if (rgb == null)
73                         return null;
74                 
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() {
82                                         public void run() {
83                                                 dispose(display);
84                                         }
85                                 });
86                         }
87                 }
88                 
89                 Color color= (Color) colorTable.get(rgb);
90                 if (color == null) {
91                         color= new Color(Display.getCurrent(), rgb);
92                         colorTable.put(rgb, color);
93                 }
94                 
95                 return color;
96         }
97         
98         /*
99          * @see IColorManager#dispose
100          */
101         public void dispose() {
102                 if (!fAutoDisposeOnDisplayDispose)
103                         dispose(Display.getCurrent());
104         }
105         
106         /*
107          * @see IColorManager#getColor(String)
108          */
109         public Color getColor(String key) {
110                 
111                 if (key == null)
112                         return null;
113                         
114                 RGB rgb= (RGB) fKeyTable.get(key);
115                 return getColor(rgb);
116         }
117         
118         /*
119          * @see IColorManagerExtension#bindColor(String, RGB)
120          */
121         public void bindColor(String key, RGB rgb) {
122                 Object value= fKeyTable.get(key);
123                 if (value != null)
124                         throw new UnsupportedOperationException();
125                 
126                 fKeyTable.put(key, rgb);
127         }
128
129         /*
130          * @see IColorManagerExtension#unbindColor(String)
131          */
132         public void unbindColor(String key) {
133                 fKeyTable.remove(key);
134         }
135 }