Mercurial > projects > dwt-addons
annotate dwtx/jface/resource/ColorRegistry.d @ 71:4878bef4a38e
Some fixing
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 22 May 2008 04:03:58 +0200 |
parents | 46a6e0e6ccd4 |
children | 5df4896124c7 |
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 /** | |
71 | 57 * Default color value. This is cyan (very unappetizing). |
70
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 */ |
71 | 60 private static ColorDescriptor DEFAULT_COLOR; |
61 private static void init_DEFAULT_COLOR () { | |
62 if( DEFAULT_COLOR is null ){ | |
63 synchronized( ColorRegistry.classinfo ){ | |
64 if( DEFAULT_COLOR is null ){ | |
65 DEFAULT_COLOR = new RGBColorDescriptor(new RGB(0, 255, 255)); | |
66 } | |
67 } | |
68 } | |
69 } | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
70 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
71 /** |
9 | 72 * This registries <code>Display</code>. All colors will be allocated using |
73 * it. | |
74 */ | |
75 protected Display display; | |
76 | |
77 /** | |
78 * Collection of <code>Color</code> that are now stale to be disposed when | |
79 * it is safe to do so (i.e. on shutdown). | |
80 */ | |
81 private Seq!(Color) staleColors; | |
82 | |
83 /** | |
84 * Table of known colors, keyed by symbolic color name (key type: <code>String</code>, | |
85 * value type: <code>dwt.graphics.Color</code>. | |
86 */ | |
87 private Map!(String,Color) stringToColor; | |
88 | |
89 /** | |
90 * Table of known color data, keyed by symbolic color name (key type: | |
91 * <code>String</code>, value type: <code>dwt.graphics.RGB</code>). | |
92 */ | |
93 private Map!(String,RGB) stringToRGB; | |
94 | |
95 /** | |
96 * Runnable that cleans up the manager on disposal of the display. | |
97 */ | |
98 protected Runnable displayRunnable; | |
99 private void init_displayRunnable(){ | |
100 displayRunnable = new class Runnable { | |
101 public void run() { | |
102 clearCaches(); | |
103 } | |
104 }; | |
105 } | |
106 | |
107 /** | |
108 * Create a new instance of the receiver that is hooked to the current | |
109 * display. | |
110 * | |
111 * @see dwt.widgets.Display#getCurrent() | |
112 */ | |
113 public this() { | |
114 this(Display.getCurrent(), true); | |
115 } | |
116 | |
117 /** | |
118 * Create a new instance of the receiver. | |
119 * | |
120 * @param display the <code>Display</code> to hook into. | |
121 */ | |
122 public this(Display display) { | |
123 this (display, true); | |
124 } | |
125 | |
126 /** | |
127 * Create a new instance of the receiver. | |
128 * | |
129 * @param display the <code>Display</code> to hook into | |
130 * @param cleanOnDisplayDisposal | |
131 * whether all fonts allocated by this <code>ColorRegistry</code> | |
132 * should be disposed when the display is disposed | |
133 * @since 3.1 | |
134 */ | |
135 public this(Display display, bool cleanOnDisplayDisposal) { | |
71 | 136 init_DEFAULT_COLOR(); |
9 | 137 staleColors = new ArraySeq!(Color); |
138 stringToColor = new HashMap!(String,Color); | |
139 stringToRGB = new HashMap!(String,RGB); | |
140 init_displayRunnable(); | |
141 Assert.isNotNull(display); | |
142 this.display = display; | |
143 if (cleanOnDisplayDisposal) { | |
144 hookDisplayDispose(); | |
145 } | |
146 } | |
147 | |
148 /** | |
149 * Create a new <code>Color</code> on the receivers <code>Display</code>. | |
150 * | |
151 * @param rgb the <code>RGB</code> data for the color. | |
152 * @return the new <code>Color</code> object. | |
153 * | |
154 * @since 3.1 | |
155 */ | |
156 private Color createColor(RGB rgb) { | |
157 return new Color(display, rgb); | |
158 } | |
159 | |
160 /** | |
161 * Dispose of all of the <code>Color</code>s in this iterator. | |
162 * | |
163 * @param iterator over <code>Collection</code> of <code>Color</code> | |
164 */ | |
165 /+ | |
166 private void disposeColors(Iterator iterator) { | |
167 while (iterator.hasNext()) { | |
168 Object next = iterator.next(); | |
169 ((Color) next).dispose(); | |
170 } | |
171 } | |
172 +/ | |
173 | |
174 /** | |
175 * Returns the <code>color</code> associated with the given symbolic color | |
176 * name, or <code>null</code> if no such definition exists. | |
177 * | |
178 * @param symbolicName symbolic color name | |
179 * @return the <code>Color</code> or <code>null</code> | |
180 */ | |
181 public Color get(String symbolicName) { | |
182 | |
183 Assert.isNotNull(symbolicName); | |
184 auto result1 = stringToColor.get(symbolicName); | |
185 if (result1 !is null) { | |
186 return result1; | |
187 } | |
188 | |
189 Color color = null; | |
190 | |
191 auto result = stringToRGB.get(symbolicName); | |
192 if (result is null) { | |
193 return null; | |
194 } | |
195 | |
196 color = createColor(result); | |
197 | |
198 stringToColor.add(symbolicName, color); | |
199 | |
200 return color; | |
201 } | |
202 | |
203 /* (non-Javadoc) | |
204 * @see dwtx.jface.resource.ResourceRegistry#getKeySet() | |
205 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
9
diff
changeset
|
206 public override SetView!(String) getKeySet() { |
9 | 207 auto res = new HashSet!(String); |
208 foreach( k,v; stringToRGB ){ | |
209 res.add(k); | |
210 } | |
211 return res; | |
212 } | |
213 | |
214 /** | |
215 * Returns the color data associated with the given symbolic color name. | |
216 * | |
217 * @param symbolicName symbolic color name. | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
218 * @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
|
219 * is not valid. |
9 | 220 */ |
221 public RGB getRGB(String symbolicName) { | |
222 Assert.isNotNull(symbolicName); | |
223 return stringToRGB.get(symbolicName); | |
224 } | |
225 | |
226 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
227 * 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
|
228 * 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
|
229 * 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
|
230 * use {@link #getColorDescriptor(String, ColorDescriptor)} instead. |
71 | 231 * |
9 | 232 * @since 3.1 |
71 | 233 * |
9 | 234 * @param symbolicName |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
235 * @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
|
236 * name or an unspecified sentinel. |
9 | 237 */ |
238 public ColorDescriptor getColorDescriptor(String symbolicName) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
239 return getColorDescriptor(symbolicName, DEFAULT_COLOR); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
240 } |
71 | 241 |
70
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 * 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
|
244 * 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
|
245 * default value will be used. |
71 | 246 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
247 * @param symbolicName |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
248 * @param defaultValue |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
249 * @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
|
250 * name or the default |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
251 * @since 3.4 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
252 */ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
253 public ColorDescriptor getColorDescriptor(String symbolicName, |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
254 ColorDescriptor defaultValue) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
255 RGB rgb = getRGB(symbolicName); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
256 if (rgb is null) |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
257 return defaultValue; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
258 return ColorDescriptor.createFrom(rgb); |
9 | 259 } |
260 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
261 /* |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
262 * (non-Javadoc) |
71 | 263 * |
9 | 264 * @see dwtx.jface.resource.ResourceRegistry#clearCaches() |
265 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
9
diff
changeset
|
266 protected override void clearCaches() { |
9 | 267 foreach( k, v; stringToColor ){ |
268 v.dispose(); | |
269 } | |
270 foreach( v; staleColors ){ | |
271 v.dispose(); | |
272 } | |
273 // disposeColors(stringToColor.values().iterator()); | |
274 // disposeColors(staleColors.iterator()); | |
275 stringToColor.clear(); | |
276 staleColors.clear(); | |
277 } | |
278 | |
279 /* (non-Javadoc) | |
280 * @see dwtx.jface.resource.ResourceRegistry#hasValueFor(java.lang.String) | |
281 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
9
diff
changeset
|
282 public override bool hasValueFor(String colorKey) { |
9 | 283 return stringToRGB.containsKey(colorKey); |
284 } | |
285 | |
286 /** | |
287 * Hook a dispose listener on the DWT display. | |
288 */ | |
289 private void hookDisplayDispose() { | |
290 display.disposeExec(displayRunnable); | |
291 } | |
292 | |
293 /** | |
294 * Adds (or replaces) a color to this color registry under the given | |
295 * symbolic name. | |
296 * <p> | |
297 * A property change event is reported whenever the mapping from a symbolic | |
298 * name to a color changes. The source of the event is this registry; the | |
299 * property name is the symbolic color name. | |
300 * </p> | |
301 * | |
302 * @param symbolicName the symbolic color name | |
303 * @param colorData an <code>RGB</code> object | |
304 */ | |
305 public void put(String symbolicName, RGB colorData) { | |
306 put(symbolicName, colorData, true); | |
307 } | |
308 | |
309 /** | |
310 * Adds (or replaces) a color to this color registry under the given | |
311 * symbolic name. | |
312 * <p> | |
313 * A property change event is reported whenever the mapping from a symbolic | |
314 * name to a color changes. The source of the event is this registry; the | |
315 * property name is the symbolic color name. | |
316 * </p> | |
317 * | |
318 * @param symbolicName the symbolic color name | |
319 * @param colorData an <code>RGB</code> object | |
320 * @param update - fire a color mapping changed if true. False if this | |
321 * method is called from the get method as no setting has | |
322 * changed. | |
323 */ | |
324 private void put(String symbolicName, RGB colorData, bool update) { | |
325 | |
326 Assert.isNotNull(symbolicName); | |
327 Assert.isNotNull(colorData); | |
328 | |
329 RGB existing = stringToRGB.get(symbolicName); | |
330 if (colorData.opEquals(existing)) { | |
331 return; | |
332 } | |
333 | |
334 Color oldColor = stringToColor.get(symbolicName); | |
335 stringToColor.removeKey(symbolicName); | |
336 stringToRGB.add(symbolicName, colorData); | |
337 if (update) { | |
338 fireMappingChanged(symbolicName, existing, colorData); | |
339 } | |
340 | |
341 if (oldColor !is null) { | |
342 staleColors.append(oldColor); | |
343 } | |
344 } | |
345 } |