Mercurial > projects > dwt-addons
annotate dwtx/jface/resource/ColorRegistry.d @ 70:46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 22 May 2008 01:36:46 +0200 |
parents | ea8ff534f622 |
children | 4878bef4a38e |
rev | line source |
---|---|
9 | 1 /******************************************************************************* |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
2 * Copyright (c) 2003, 2007 IBM Corporation and others. |
9 | 3 * All rights reserved. This program and the accompanying materials |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.resource.ColorRegistry; | |
14 | |
15 import dwtx.jface.resource.ResourceRegistry; | |
16 import dwtx.jface.resource.ColorDescriptor; | |
17 | |
18 import tango.util.collection.ArraySeq; | |
19 import tango.util.collection.HashMap; | |
20 import tango.util.collection.HashSet; | |
21 import tango.util.collection.model.Map; | |
22 import tango.util.collection.model.Set; | |
23 import tango.util.collection.model.SetView; | |
24 import tango.util.collection.model.Seq; | |
25 | |
26 import dwt.graphics.Color; | |
27 import dwt.graphics.RGB; | |
28 import dwt.widgets.Display; | |
29 import dwtx.core.runtime.Assert; | |
30 | |
31 import dwt.dwthelper.utils; | |
32 import dwt.dwthelper.Runnable; | |
33 | |
34 /** | |
35 * A color registry maintains a mapping between symbolic color names and DWT | |
36 * <code>Color</code>s. | |
37 * <p> | |
38 * A color registry owns all of the <code>Color</code> objects registered with | |
39 * it, and automatically disposes of them when the DWT Display that creates the | |
40 * <code>Color</code>s is disposed. Because of this, clients do not need to | |
41 * (indeed, must not attempt to) dispose of <code>Color</code> objects | |
42 * themselves. | |
43 * </p> | |
44 * <p> | |
45 * Methods are provided for registering listeners that will be kept | |
46 * apprised of changes to list of registed colors. | |
47 * </p> | |
48 * <p> | |
49 * Clients may instantiate this class (it was not designed to be subclassed). | |
50 * </p> | |
51 * | |
52 * @since 3.0 | |
53 */ | |
54 public class ColorRegistry : ResourceRegistry { | |
55 | |
56 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
57 * Default color value. This is cyan (very unappetizing). |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
58 * @since 3.4 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
59 */ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
60 private static final ColorDescriptor DEFAULT_COLOR = new RGBColorDescriptor(new RGB(0, 255, 255)); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
61 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
62 /** |
9 | 63 * This registries <code>Display</code>. All colors will be allocated using |
64 * it. | |
65 */ | |
66 protected Display display; | |
67 | |
68 /** | |
69 * Collection of <code>Color</code> that are now stale to be disposed when | |
70 * it is safe to do so (i.e. on shutdown). | |
71 */ | |
72 private Seq!(Color) staleColors; | |
73 | |
74 /** | |
75 * Table of known colors, keyed by symbolic color name (key type: <code>String</code>, | |
76 * value type: <code>dwt.graphics.Color</code>. | |
77 */ | |
78 private Map!(String,Color) stringToColor; | |
79 | |
80 /** | |
81 * Table of known color data, keyed by symbolic color name (key type: | |
82 * <code>String</code>, value type: <code>dwt.graphics.RGB</code>). | |
83 */ | |
84 private Map!(String,RGB) stringToRGB; | |
85 | |
86 /** | |
87 * Runnable that cleans up the manager on disposal of the display. | |
88 */ | |
89 protected Runnable displayRunnable; | |
90 private void init_displayRunnable(){ | |
91 displayRunnable = new class Runnable { | |
92 public void run() { | |
93 clearCaches(); | |
94 } | |
95 }; | |
96 } | |
97 | |
98 /** | |
99 * Create a new instance of the receiver that is hooked to the current | |
100 * display. | |
101 * | |
102 * @see dwt.widgets.Display#getCurrent() | |
103 */ | |
104 public this() { | |
105 this(Display.getCurrent(), true); | |
106 } | |
107 | |
108 /** | |
109 * Create a new instance of the receiver. | |
110 * | |
111 * @param display the <code>Display</code> to hook into. | |
112 */ | |
113 public this(Display display) { | |
114 this (display, true); | |
115 } | |
116 | |
117 /** | |
118 * Create a new instance of the receiver. | |
119 * | |
120 * @param display the <code>Display</code> to hook into | |
121 * @param cleanOnDisplayDisposal | |
122 * whether all fonts allocated by this <code>ColorRegistry</code> | |
123 * should be disposed when the display is disposed | |
124 * @since 3.1 | |
125 */ | |
126 public this(Display display, bool cleanOnDisplayDisposal) { | |
127 staleColors = new ArraySeq!(Color); | |
128 stringToColor = new HashMap!(String,Color); | |
129 stringToRGB = new HashMap!(String,RGB); | |
130 init_displayRunnable(); | |
131 Assert.isNotNull(display); | |
132 this.display = display; | |
133 if (cleanOnDisplayDisposal) { | |
134 hookDisplayDispose(); | |
135 } | |
136 } | |
137 | |
138 /** | |
139 * Create a new <code>Color</code> on the receivers <code>Display</code>. | |
140 * | |
141 * @param rgb the <code>RGB</code> data for the color. | |
142 * @return the new <code>Color</code> object. | |
143 * | |
144 * @since 3.1 | |
145 */ | |
146 private Color createColor(RGB rgb) { | |
147 return new Color(display, rgb); | |
148 } | |
149 | |
150 /** | |
151 * Dispose of all of the <code>Color</code>s in this iterator. | |
152 * | |
153 * @param iterator over <code>Collection</code> of <code>Color</code> | |
154 */ | |
155 /+ | |
156 private void disposeColors(Iterator iterator) { | |
157 while (iterator.hasNext()) { | |
158 Object next = iterator.next(); | |
159 ((Color) next).dispose(); | |
160 } | |
161 } | |
162 +/ | |
163 | |
164 /** | |
165 * Returns the <code>color</code> associated with the given symbolic color | |
166 * name, or <code>null</code> if no such definition exists. | |
167 * | |
168 * @param symbolicName symbolic color name | |
169 * @return the <code>Color</code> or <code>null</code> | |
170 */ | |
171 public Color get(String symbolicName) { | |
172 | |
173 Assert.isNotNull(symbolicName); | |
174 auto result1 = stringToColor.get(symbolicName); | |
175 if (result1 !is null) { | |
176 return result1; | |
177 } | |
178 | |
179 Color color = null; | |
180 | |
181 auto result = stringToRGB.get(symbolicName); | |
182 if (result is null) { | |
183 return null; | |
184 } | |
185 | |
186 color = createColor(result); | |
187 | |
188 stringToColor.add(symbolicName, color); | |
189 | |
190 return color; | |
191 } | |
192 | |
193 /* (non-Javadoc) | |
194 * @see dwtx.jface.resource.ResourceRegistry#getKeySet() | |
195 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
9
diff
changeset
|
196 public override SetView!(String) getKeySet() { |
9 | 197 auto res = new HashSet!(String); |
198 foreach( k,v; stringToRGB ){ | |
199 res.add(k); | |
200 } | |
201 return res; | |
202 } | |
203 | |
204 /** | |
205 * Returns the color data associated with the given symbolic color name. | |
206 * | |
207 * @param symbolicName symbolic color name. | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
208 * @return the <code>RGB</code> data, or <code>null</code> if the symbolic name |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
209 * is not valid. |
9 | 210 */ |
211 public RGB getRGB(String symbolicName) { | |
212 Assert.isNotNull(symbolicName); | |
213 return stringToRGB.get(symbolicName); | |
214 } | |
215 | |
216 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
217 * Returns the color descriptor associated with the given symbolic color |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
218 * name. As of 3.4 if this color is not defined then an unspecified color |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
219 * is returned. Users that wish to ensure a reasonable default value should |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
220 * use {@link #getColorDescriptor(String, ColorDescriptor)} instead. |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
221 * |
9 | 222 * @since 3.1 |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
223 * |
9 | 224 * @param symbolicName |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
225 * @return the color descriptor associated with the given symbolic color |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
226 * name or an unspecified sentinel. |
9 | 227 */ |
228 public ColorDescriptor getColorDescriptor(String symbolicName) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
229 return getColorDescriptor(symbolicName, DEFAULT_COLOR); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
230 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
231 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
232 /** |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
233 * Returns the color descriptor associated with the given symbolic color |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
234 * name. If this name does not exist within the registry the supplied |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
235 * default value will be used. |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
236 * |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
237 * @param symbolicName |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
238 * @param defaultValue |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
239 * @return the color descriptor associated with the given symbolic color |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
240 * name or the default |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
241 * @since 3.4 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
242 */ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
243 public ColorDescriptor getColorDescriptor(String symbolicName, |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
244 ColorDescriptor defaultValue) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
245 RGB rgb = getRGB(symbolicName); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
246 if (rgb is null) |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
247 return defaultValue; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
248 return ColorDescriptor.createFrom(rgb); |
9 | 249 } |
250 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
251 /* |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
252 * (non-Javadoc) |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
253 * |
9 | 254 * @see dwtx.jface.resource.ResourceRegistry#clearCaches() |
255 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
9
diff
changeset
|
256 protected override void clearCaches() { |
9 | 257 foreach( k, v; stringToColor ){ |
258 v.dispose(); | |
259 } | |
260 foreach( v; staleColors ){ | |
261 v.dispose(); | |
262 } | |
263 // disposeColors(stringToColor.values().iterator()); | |
264 // disposeColors(staleColors.iterator()); | |
265 stringToColor.clear(); | |
266 staleColors.clear(); | |
267 } | |
268 | |
269 /* (non-Javadoc) | |
270 * @see dwtx.jface.resource.ResourceRegistry#hasValueFor(java.lang.String) | |
271 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
9
diff
changeset
|
272 public override bool hasValueFor(String colorKey) { |
9 | 273 return stringToRGB.containsKey(colorKey); |
274 } | |
275 | |
276 /** | |
277 * Hook a dispose listener on the DWT display. | |
278 */ | |
279 private void hookDisplayDispose() { | |
280 display.disposeExec(displayRunnable); | |
281 } | |
282 | |
283 /** | |
284 * Adds (or replaces) a color to this color registry under the given | |
285 * symbolic name. | |
286 * <p> | |
287 * A property change event is reported whenever the mapping from a symbolic | |
288 * name to a color changes. The source of the event is this registry; the | |
289 * property name is the symbolic color name. | |
290 * </p> | |
291 * | |
292 * @param symbolicName the symbolic color name | |
293 * @param colorData an <code>RGB</code> object | |
294 */ | |
295 public void put(String symbolicName, RGB colorData) { | |
296 put(symbolicName, colorData, true); | |
297 } | |
298 | |
299 /** | |
300 * Adds (or replaces) a color to this color registry under the given | |
301 * symbolic name. | |
302 * <p> | |
303 * A property change event is reported whenever the mapping from a symbolic | |
304 * name to a color changes. The source of the event is this registry; the | |
305 * property name is the symbolic color name. | |
306 * </p> | |
307 * | |
308 * @param symbolicName the symbolic color name | |
309 * @param colorData an <code>RGB</code> object | |
310 * @param update - fire a color mapping changed if true. False if this | |
311 * method is called from the get method as no setting has | |
312 * changed. | |
313 */ | |
314 private void put(String symbolicName, RGB colorData, bool update) { | |
315 | |
316 Assert.isNotNull(symbolicName); | |
317 Assert.isNotNull(colorData); | |
318 | |
319 RGB existing = stringToRGB.get(symbolicName); | |
320 if (colorData.opEquals(existing)) { | |
321 return; | |
322 } | |
323 | |
324 Color oldColor = stringToColor.get(symbolicName); | |
325 stringToColor.removeKey(symbolicName); | |
326 stringToRGB.add(symbolicName, colorData); | |
327 if (update) { | |
328 fireMappingChanged(symbolicName, existing, colorData); | |
329 } | |
330 | |
331 if (oldColor !is null) { | |
332 staleColors.append(oldColor); | |
333 } | |
334 } | |
335 } |