Mercurial > projects > dwt-addons
comparison dwtx/core/commands/contexts/ContextManager.d @ 104:04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
These new wrappers now use the tango.util.containers instead of the tango.util.collections.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 07 Aug 2008 15:01:33 +0200 |
parents | 6518c18a01f7 |
children |
comparison
equal
deleted
inserted
replaced
103:2d6540440fe6 | 104:04b47443bb01 |
---|---|
11 * Frank Benoit <benoit@tionex.de> | 11 * Frank Benoit <benoit@tionex.de> |
12 *******************************************************************************/ | 12 *******************************************************************************/ |
13 | 13 |
14 module dwtx.core.commands.contexts.ContextManager; | 14 module dwtx.core.commands.contexts.ContextManager; |
15 | 15 |
16 import tango.util.collection.HashSet; | |
17 import tango.util.collection.model.Set; | |
18 import tango.util.collection.model.SetView; | |
19 | |
20 import dwtx.core.commands.contexts.IContextListener; | 16 import dwtx.core.commands.contexts.IContextListener; |
21 import dwtx.core.commands.contexts.IContextManagerListener; | 17 import dwtx.core.commands.contexts.IContextManagerListener; |
22 import dwtx.core.commands.contexts.ContextEvent; | 18 import dwtx.core.commands.contexts.ContextEvent; |
23 import dwtx.core.commands.contexts.ContextManagerEvent; | 19 import dwtx.core.commands.contexts.ContextManagerEvent; |
24 import dwtx.core.commands.contexts.Context; | 20 import dwtx.core.commands.contexts.Context; |
25 import dwtx.core.commands.common.HandleObjectManager; | 21 import dwtx.core.commands.common.HandleObjectManager; |
26 import dwtx.core.commands.util.Tracing; | 22 import dwtx.core.commands.util.Tracing; |
27 import dwtx.core.internal.commands.util.Util; | 23 import dwtx.core.internal.commands.util.Util; |
28 | 24 |
29 import dwt.dwthelper.utils; | 25 import dwt.dwthelper.utils; |
26 import dwtx.dwtxhelper.Collection; | |
30 | 27 |
31 /** | 28 /** |
32 * <p> | 29 * <p> |
33 * A context manager tracks the sets of defined and enabled contexts within the | 30 * A context manager tracks the sets of defined and enabled contexts within the |
34 * application. The manager sends notification events to listeners when these | 31 * application. The manager sends notification events to listeners when these |
56 | 53 |
57 /** | 54 /** |
58 * The set of active context identifiers. This value may be empty, but it is | 55 * The set of active context identifiers. This value may be empty, but it is |
59 * never <code>null</code>. | 56 * never <code>null</code>. |
60 */ | 57 */ |
61 private Set!(String) activeContextIds; | 58 private Set activeContextIds; |
62 private static Set!(String) EMPTY_SET; | 59 private static Set EMPTY_SET; |
63 | 60 |
64 // allow the ContextManager to send one event for a larger delta | 61 // allow the ContextManager to send one event for a larger delta |
65 private bool caching = false; | 62 private bool caching = false; |
66 | 63 |
67 private int cachingRef = 0; | 64 private int cachingRef = 0; |
68 | 65 |
69 private bool activeContextsChange = false; | 66 private bool activeContextsChange = false; |
70 | 67 |
71 private Set!(String) oldIds = null; | 68 private Set oldIds = null; |
72 | 69 |
73 public this(){ | 70 public this(){ |
74 activeContextIds = new HashSet!(String); | 71 activeContextIds = new HashSet(); |
75 if( EMPTY_SET is null ){ | 72 if( EMPTY_SET is null ){ |
76 EMPTY_SET = new HashSet!(String); | 73 EMPTY_SET = new HashSet(); |
77 } | 74 } |
78 } | 75 } |
79 | 76 |
80 /** | 77 /** |
81 * Activates a context in this context manager. | 78 * Activates a context in this context manager. |
105 activeContextsChange = true; | 102 activeContextsChange = true; |
106 | 103 |
107 if (caching) { | 104 if (caching) { |
108 activeContextIds.add(contextId); | 105 activeContextIds.add(contextId); |
109 } else { | 106 } else { |
110 Set!(String) previouslyActiveContextIds = activeContextIds.duplicate(); | 107 Set previouslyActiveContextIds = new HashSet(activeContextIds); |
111 activeContextIds.add(contextId); | 108 activeContextIds.add(contextId); |
112 | 109 |
113 fireContextManagerChanged(new ContextManagerEvent(this, null, | 110 fireContextManagerChanged(new ContextManagerEvent(this, null, |
114 false, true, previouslyActiveContextIds)); | 111 false, true, previouslyActiveContextIds)); |
115 } | 112 } |
116 | 113 |
117 if (DEBUG) { | 114 if (DEBUG) { |
118 Tracing.printTrace("CONTEXTS", SetToString(activeContextIds)); //$NON-NLS-1$ | 115 Tracing.printTrace("CONTEXTS", activeContextIds.toString()); //$NON-NLS-1$ |
119 } | 116 } |
120 | 117 |
121 } | 118 } |
122 | 119 |
123 /** | 120 /** |
176 * @return The set of active context identifiers; this value may be | 173 * @return The set of active context identifiers; this value may be |
177 * <code>null</code> if no active contexts have been set yet. If | 174 * <code>null</code> if no active contexts have been set yet. If |
178 * the set is not <code>null</code>, then it contains only | 175 * the set is not <code>null</code>, then it contains only |
179 * instances of <code>String</code>. | 176 * instances of <code>String</code>. |
180 */ | 177 */ |
181 public final SetView!(String) getActiveContextIds() { | 178 public final Set getActiveContextIds() { |
182 return /+Collections.unmodifiableSet(+/activeContextIds/+)+/; | 179 return Collections.unmodifiableSet(activeContextIds); |
183 } | 180 } |
184 | 181 |
185 /** | 182 /** |
186 * Gets the context with the given identifier. If no such context currently | 183 * Gets the context with the given identifier. If no such context currently |
187 * exists, then the context will be created (but be undefined). | 184 * exists, then the context will be created (but be undefined). |
196 checkId(contextId); | 193 checkId(contextId); |
197 | 194 |
198 Context context = cast(Context) handleObjectsById.get(contextId); | 195 Context context = cast(Context) handleObjectsById.get(contextId); |
199 if (context is null) { | 196 if (context is null) { |
200 context = new Context(contextId); | 197 context = new Context(contextId); |
201 handleObjectsById.add(contextId, context); | 198 handleObjectsById.put(contextId, context); |
202 context.addContextListener(this); | 199 context.addContextListener(this); |
203 } | 200 } |
204 | 201 |
205 return context; | 202 return context; |
206 } | 203 } |
209 * Returns the set of identifiers for those contexts that are defined. | 206 * Returns the set of identifiers for those contexts that are defined. |
210 * | 207 * |
211 * @return The set of defined context identifiers; this value may be empty, | 208 * @return The set of defined context identifiers; this value may be empty, |
212 * but it is never <code>null</code>. | 209 * but it is never <code>null</code>. |
213 */ | 210 */ |
214 public final Set!(String) getDefinedContextIds() { | 211 public final Set getDefinedContextIds() { |
215 return getDefinedHandleObjectIds(); | 212 return getDefinedHandleObjectIds(); |
216 } | 213 } |
217 | 214 |
218 /** | 215 /** |
219 * Returns the those contexts that are defined. | 216 * Returns the those contexts that are defined. |
221 * @return The defined contexts; this value may be empty, but it is never | 218 * @return The defined contexts; this value may be empty, but it is never |
222 * <code>null</code>. | 219 * <code>null</code>. |
223 * @since 3.2 | 220 * @since 3.2 |
224 */ | 221 */ |
225 public final Context[] getDefinedContexts() { | 222 public final Context[] getDefinedContexts() { |
226 return cast(Context[]) definedHandleObjects | 223 return arraycast!(Context)( definedHandleObjects |
227 .toArray(/+new Context[definedHandleObjects.size()]+/); | 224 .toArray(/+new Context[definedHandleObjects.size()]+/)); |
228 } | 225 } |
229 | 226 |
230 /** | 227 /** |
231 * Deactivates a context in this context manager. | 228 * Deactivates a context in this context manager. |
232 * | 229 * |
241 | 238 |
242 activeContextsChange = true; | 239 activeContextsChange = true; |
243 if (caching) { | 240 if (caching) { |
244 activeContextIds.remove(contextId); | 241 activeContextIds.remove(contextId); |
245 } else { | 242 } else { |
246 auto previouslyActiveContextIds = activeContextIds.dup; | 243 Set previouslyActiveContextIds = new HashSet(activeContextIds); |
247 activeContextIds.remove(contextId); | 244 activeContextIds.remove(contextId); |
248 | 245 |
249 fireContextManagerChanged(new ContextManagerEvent(this, null, | 246 fireContextManagerChanged(new ContextManagerEvent(this, null, |
250 false, true, previouslyActiveContextIds)); | 247 false, true, previouslyActiveContextIds)); |
251 } | 248 } |
252 | 249 |
253 if (DEBUG) { | 250 if (DEBUG) { |
254 Tracing.printTrace("CONTEXTS", SetToString(activeContextIds)); //$NON-NLS-1$ | 251 Tracing.printTrace("CONTEXTS", activeContextIds.toString()); //$NON-NLS-1$ |
255 } | 252 } |
256 } | |
257 private String SetToString( Set!(String) set ){ | |
258 String s = "["; | |
259 foreach( id; set ){ | |
260 if( s.length > 1 ) s ~= ", "; | |
261 s ~= id; | |
262 } | |
263 s ~= "]"; | |
264 return s; | |
265 } | 253 } |
266 /** | 254 /** |
267 * Removes a listener from this context manager. | 255 * Removes a listener from this context manager. |
268 * | 256 * |
269 * @param listener | 257 * @param listener |
281 * | 269 * |
282 * @param activeContextIds | 270 * @param activeContextIds |
283 * The new set of active context identifiers; may be | 271 * The new set of active context identifiers; may be |
284 * <code>null</code>. | 272 * <code>null</code>. |
285 */ | 273 */ |
286 public final void setActiveContextIds(Set!(String) activeContextIds) { | 274 public final void setActiveContextIds(Set activeContextIds) { |
287 if (Util.equals(this.activeContextIds.toArray, activeContextIds.toArray)) { | 275 if (Util.equals(cast(Object)this.activeContextIds, cast(Object)activeContextIds)) { |
288 return; | 276 return; |
289 } | 277 } |
290 | 278 |
291 activeContextsChange = true; | 279 activeContextsChange = true; |
292 | 280 |
293 Set!(String) previouslyActiveContextIds = this.activeContextIds; | 281 Set previouslyActiveContextIds = this.activeContextIds; |
294 if (activeContextIds !is null) { | 282 if (activeContextIds !is null) { |
295 this.activeContextIds = activeContextIds.duplicate; | 283 this.activeContextIds = new HashSet(); |
284 this.activeContextIds.addAll(activeContextIds); | |
296 } else { | 285 } else { |
297 this.activeContextIds = null; | 286 this.activeContextIds = null; |
298 } | 287 } |
299 | 288 |
300 if (DEBUG) { | 289 if (DEBUG) { |
301 Tracing.printTrace("CONTEXTS", (activeContextIds is null) ? "none" //$NON-NLS-1$ //$NON-NLS-2$ | 290 Tracing.printTrace("CONTEXTS", (activeContextIds is null) ? "none" //$NON-NLS-1$ //$NON-NLS-2$ |
302 : SetToString(activeContextIds)); | 291 : activeContextIds.toString()); |
303 } | 292 } |
304 | 293 |
305 if (!caching) { | 294 if (!caching) { |
306 fireContextManagerChanged(new ContextManagerEvent(this, null, | 295 fireContextManagerChanged(new ContextManagerEvent(this, null, |
307 false, true, previouslyActiveContextIds)); | 296 false, true, previouslyActiveContextIds)); |
320 if (caching is cache) { | 309 if (caching is cache) { |
321 return; | 310 return; |
322 } | 311 } |
323 caching = cache; | 312 caching = cache; |
324 bool fireChange = activeContextsChange; | 313 bool fireChange = activeContextsChange; |
325 Set!(String) holdOldIds = (oldIds is null? EMPTY_SET : oldIds ); | 314 Set holdOldIds = (oldIds is null?Collections.EMPTY_SET:oldIds); |
326 | 315 |
327 if (caching) { | 316 if (caching) { |
328 oldIds = activeContextIds.duplicate; | 317 oldIds = new HashSet(activeContextIds); |
329 } else { | 318 } else { |
330 oldIds = null; | 319 oldIds = null; |
331 } | 320 } |
332 activeContextsChange = false; | 321 activeContextsChange = false; |
333 | 322 |