intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.css.ui / src / net / sourceforge / phpeclipse / css / ui / internal / text / CssColorManager.java
1 /*
2  * Copyright (c) 2003-2004 Christopher Lenz and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     Christopher Lenz - initial API and implementation
10  * 
11  * $Id: CssColorManager.java,v 1.1 2004-09-02 18:11:48 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.css.ui.internal.text;
15
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.Map;
19
20 import net.sourceforge.phpeclipse.css.ui.text.IColorManager;
21
22 import org.eclipse.swt.graphics.Color;
23 import org.eclipse.swt.graphics.RGB;
24 import org.eclipse.swt.widgets.Display;
25
26 /**
27  * CSS color manager.
28  */
29 public class CssColorManager implements IColorManager {
30
31         // Instance Variables ------------------------------------------------------
32
33         private Map keyTable = new HashMap(10);
34
35         private Map displayTable = new HashMap(2);
36
37         /** 
38          * Flag which tells if the colors are automatically disposed when the
39          * current display gets disposed.
40          */
41         private boolean autoDisposeOnDisplayDispose;
42
43         // Constructors ------------------------------------------------------------
44
45         /**
46          * Creates a new CSS color manager which automatically disposes the
47          * allocated colors when the current display gets disposed.
48          */ 
49         public CssColorManager() {
50                 this(true);
51         }
52
53         /**
54          * Creates a new CSS color manager.
55          * 
56          * @param autoDisposeOnDisplayDispose if <code>true</code> the color manager
57          *        automatically disposes all managed colors when the current display
58          *        gets disposed and all calls to
59          *        {@link org.eclipse.jface.text.source.ISharedTextColors#dispose()}
60          *        are ignored.
61          */
62         public CssColorManager(boolean autoDisposeOnDisplayDispose) {
63                 this.autoDisposeOnDisplayDispose = autoDisposeOnDisplayDispose;
64         }
65         
66         // IColorManager Implementation --------------------------------------------
67
68         /*
69          * @see org.eclipse.jface.text.source.ISharedTextColors#dispose()
70          */
71         public void dispose() {
72                 if (!this.autoDisposeOnDisplayDispose) {
73                         dispose(Display.getCurrent());
74                 }
75         }
76         
77         /*
78          * @see org.eclipse.jface.text.source.ISharedTextColors#getColor(RGB)
79          */
80         public Color getColor(RGB rgb) {
81                 if (rgb == null) {
82                         return null;
83                 }
84                 final Display display = Display.getCurrent();
85                 Map colorTable = (Map) this.displayTable.get(display);
86                 if (colorTable == null) {
87                         colorTable = new HashMap(10);
88                         this.displayTable.put(display, colorTable);
89                         if (this.autoDisposeOnDisplayDispose) {
90                                 display.disposeExec(new Runnable() {
91                                         public void run() {
92                                                 dispose(display);
93                                         }
94                                 });
95                         }
96                 }
97                 Color color = (Color) colorTable.get(rgb);
98                 if (color == null) {
99                         color = new Color(Display.getCurrent(), rgb);
100                         colorTable.put(rgb, color);
101                 }
102                 return color;
103         }
104
105         /*
106          * @see IColorManager#bindColor(String, RGB)
107          */
108         public void bindColor(String key, RGB rgb) {
109                 Object value = this.keyTable.get(key);
110                 if (value != null) {
111                         throw new IllegalStateException();
112                 }
113                 this.keyTable.put(key, rgb);
114         }
115
116         /*
117          * @see IColorManager#getColor(String)
118          */
119         public Color getColor(String key) {
120                 if (key == null) {
121                         return null;
122                 }
123                 RGB rgb = (RGB) this.keyTable.get(key);
124                 return getColor(rgb);
125         }
126
127         /*
128          * @see IColorManager#unbindColor(String)
129          */
130         public void unbindColor(String key) {
131                 this.keyTable.remove(key);
132         }
133
134         // Private Methods ---------------------------------------------------------
135
136         private void dispose(Display display) {
137                 Map colorTable = (Map) this.displayTable.get(display);
138                 if (colorTable != null) {
139                         for (Iterator i = colorTable.values().iterator(); i.hasNext(); ) {
140                                 Color color = (Color) i.next();
141                                 if (color != null && !color.isDisposed()) {
142                                         color.dispose();
143                                 }
144                         }
145                 }
146         }
147         
148 }