Mercurial > projects > dwt-addons
comparison dwtx/jface/resource/ColorRegistry.d @ 9:6c14e54dfc11
completed /jface/resource/
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 29 Mar 2008 02:25:12 +0100 |
parents | |
children | ea8ff534f622 |
comparison
equal
deleted
inserted
replaced
8:a3ff22a98bef | 9:6c14e54dfc11 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2003, 2006 IBM Corporation and others. | |
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 /** | |
57 * This registries <code>Display</code>. All colors will be allocated using | |
58 * it. | |
59 */ | |
60 protected Display display; | |
61 | |
62 /** | |
63 * Collection of <code>Color</code> that are now stale to be disposed when | |
64 * it is safe to do so (i.e. on shutdown). | |
65 */ | |
66 private Seq!(Color) staleColors; | |
67 | |
68 /** | |
69 * Table of known colors, keyed by symbolic color name (key type: <code>String</code>, | |
70 * value type: <code>dwt.graphics.Color</code>. | |
71 */ | |
72 private Map!(String,Color) stringToColor; | |
73 | |
74 /** | |
75 * Table of known color data, keyed by symbolic color name (key type: | |
76 * <code>String</code>, value type: <code>dwt.graphics.RGB</code>). | |
77 */ | |
78 private Map!(String,RGB) stringToRGB; | |
79 | |
80 /** | |
81 * Runnable that cleans up the manager on disposal of the display. | |
82 */ | |
83 protected Runnable displayRunnable; | |
84 private void init_displayRunnable(){ | |
85 displayRunnable = new class Runnable { | |
86 public void run() { | |
87 clearCaches(); | |
88 } | |
89 }; | |
90 } | |
91 | |
92 /** | |
93 * Create a new instance of the receiver that is hooked to the current | |
94 * display. | |
95 * | |
96 * @see dwt.widgets.Display#getCurrent() | |
97 */ | |
98 public this() { | |
99 this(Display.getCurrent(), true); | |
100 } | |
101 | |
102 /** | |
103 * Create a new instance of the receiver. | |
104 * | |
105 * @param display the <code>Display</code> to hook into. | |
106 */ | |
107 public this(Display display) { | |
108 this (display, true); | |
109 } | |
110 | |
111 /** | |
112 * Create a new instance of the receiver. | |
113 * | |
114 * @param display the <code>Display</code> to hook into | |
115 * @param cleanOnDisplayDisposal | |
116 * whether all fonts allocated by this <code>ColorRegistry</code> | |
117 * should be disposed when the display is disposed | |
118 * @since 3.1 | |
119 */ | |
120 public this(Display display, bool cleanOnDisplayDisposal) { | |
121 staleColors = new ArraySeq!(Color); | |
122 stringToColor = new HashMap!(String,Color); | |
123 stringToRGB = new HashMap!(String,RGB); | |
124 init_displayRunnable(); | |
125 Assert.isNotNull(display); | |
126 this.display = display; | |
127 if (cleanOnDisplayDisposal) { | |
128 hookDisplayDispose(); | |
129 } | |
130 } | |
131 | |
132 /** | |
133 * Create a new <code>Color</code> on the receivers <code>Display</code>. | |
134 * | |
135 * @param rgb the <code>RGB</code> data for the color. | |
136 * @return the new <code>Color</code> object. | |
137 * | |
138 * @since 3.1 | |
139 */ | |
140 private Color createColor(RGB rgb) { | |
141 return new Color(display, rgb); | |
142 } | |
143 | |
144 /** | |
145 * Dispose of all of the <code>Color</code>s in this iterator. | |
146 * | |
147 * @param iterator over <code>Collection</code> of <code>Color</code> | |
148 */ | |
149 /+ | |
150 private void disposeColors(Iterator iterator) { | |
151 while (iterator.hasNext()) { | |
152 Object next = iterator.next(); | |
153 ((Color) next).dispose(); | |
154 } | |
155 } | |
156 +/ | |
157 | |
158 /** | |
159 * Returns the <code>color</code> associated with the given symbolic color | |
160 * name, or <code>null</code> if no such definition exists. | |
161 * | |
162 * @param symbolicName symbolic color name | |
163 * @return the <code>Color</code> or <code>null</code> | |
164 */ | |
165 public Color get(String symbolicName) { | |
166 | |
167 Assert.isNotNull(symbolicName); | |
168 auto result1 = stringToColor.get(symbolicName); | |
169 if (result1 !is null) { | |
170 return result1; | |
171 } | |
172 | |
173 Color color = null; | |
174 | |
175 auto result = stringToRGB.get(symbolicName); | |
176 if (result is null) { | |
177 return null; | |
178 } | |
179 | |
180 color = createColor(result); | |
181 | |
182 stringToColor.add(symbolicName, color); | |
183 | |
184 return color; | |
185 } | |
186 | |
187 /* (non-Javadoc) | |
188 * @see dwtx.jface.resource.ResourceRegistry#getKeySet() | |
189 */ | |
190 public SetView!(String) getKeySet() { | |
191 auto res = new HashSet!(String); | |
192 foreach( k,v; stringToRGB ){ | |
193 res.add(k); | |
194 } | |
195 return res; | |
196 } | |
197 | |
198 /** | |
199 * Returns the color data associated with the given symbolic color name. | |
200 * | |
201 * @param symbolicName symbolic color name. | |
202 * @return the <code>RGB</code> data. | |
203 */ | |
204 public RGB getRGB(String symbolicName) { | |
205 Assert.isNotNull(symbolicName); | |
206 return stringToRGB.get(symbolicName); | |
207 } | |
208 | |
209 /** | |
210 * Returns the color descriptor associated with the given symbolic color name. | |
211 * @since 3.1 | |
212 * | |
213 * @param symbolicName | |
214 * @return the color descriptor associated with the given symbolic color name. | |
215 */ | |
216 public ColorDescriptor getColorDescriptor(String symbolicName) { | |
217 return ColorDescriptor.createFrom(getRGB(symbolicName)); | |
218 } | |
219 | |
220 /* (non-Javadoc) | |
221 * @see dwtx.jface.resource.ResourceRegistry#clearCaches() | |
222 */ | |
223 protected void clearCaches() { | |
224 foreach( k, v; stringToColor ){ | |
225 v.dispose(); | |
226 } | |
227 foreach( v; staleColors ){ | |
228 v.dispose(); | |
229 } | |
230 // disposeColors(stringToColor.values().iterator()); | |
231 // disposeColors(staleColors.iterator()); | |
232 stringToColor.clear(); | |
233 staleColors.clear(); | |
234 } | |
235 | |
236 /* (non-Javadoc) | |
237 * @see dwtx.jface.resource.ResourceRegistry#hasValueFor(java.lang.String) | |
238 */ | |
239 public bool hasValueFor(String colorKey) { | |
240 return stringToRGB.containsKey(colorKey); | |
241 } | |
242 | |
243 /** | |
244 * Hook a dispose listener on the DWT display. | |
245 */ | |
246 private void hookDisplayDispose() { | |
247 display.disposeExec(displayRunnable); | |
248 } | |
249 | |
250 /** | |
251 * Adds (or replaces) a color to this color registry under the given | |
252 * symbolic name. | |
253 * <p> | |
254 * A property change event is reported whenever the mapping from a symbolic | |
255 * name to a color changes. The source of the event is this registry; the | |
256 * property name is the symbolic color name. | |
257 * </p> | |
258 * | |
259 * @param symbolicName the symbolic color name | |
260 * @param colorData an <code>RGB</code> object | |
261 */ | |
262 public void put(String symbolicName, RGB colorData) { | |
263 put(symbolicName, colorData, true); | |
264 } | |
265 | |
266 /** | |
267 * Adds (or replaces) a color to this color registry under the given | |
268 * symbolic name. | |
269 * <p> | |
270 * A property change event is reported whenever the mapping from a symbolic | |
271 * name to a color changes. The source of the event is this registry; the | |
272 * property name is the symbolic color name. | |
273 * </p> | |
274 * | |
275 * @param symbolicName the symbolic color name | |
276 * @param colorData an <code>RGB</code> object | |
277 * @param update - fire a color mapping changed if true. False if this | |
278 * method is called from the get method as no setting has | |
279 * changed. | |
280 */ | |
281 private void put(String symbolicName, RGB colorData, bool update) { | |
282 | |
283 Assert.isNotNull(symbolicName); | |
284 Assert.isNotNull(colorData); | |
285 | |
286 RGB existing = stringToRGB.get(symbolicName); | |
287 if (colorData.opEquals(existing)) { | |
288 return; | |
289 } | |
290 | |
291 Color oldColor = stringToColor.get(symbolicName); | |
292 stringToColor.removeKey(symbolicName); | |
293 stringToRGB.add(symbolicName, colorData); | |
294 if (update) { | |
295 fireMappingChanged(symbolicName, existing, colorData); | |
296 } | |
297 | |
298 if (oldColor !is null) { | |
299 staleColors.append(oldColor); | |
300 } | |
301 } | |
302 } |