Mercurial > projects > dwt-addons
comparison dwtx/core/commands/operations/TriggeredOperations.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 | ea8ff534f622 |
children |
comparison
equal
deleted
inserted
replaced
103:2d6540440fe6 | 104:04b47443bb01 |
---|---|
10 * Port to the D programming language: | 10 * Port to the D programming language: |
11 * Frank Benoit <benoit@tionex.de> | 11 * Frank Benoit <benoit@tionex.de> |
12 *******************************************************************************/ | 12 *******************************************************************************/ |
13 module dwtx.core.commands.operations.TriggeredOperations; | 13 module dwtx.core.commands.operations.TriggeredOperations; |
14 | 14 |
15 import tango.util.collection.ArraySeq; | |
16 import tango.util.collection.model.Seq; | |
17 | |
18 import dwtx.core.commands.ExecutionException; | 15 import dwtx.core.commands.ExecutionException; |
19 import dwtx.core.runtime.IAdaptable; | 16 import dwtx.core.runtime.IAdaptable; |
20 import dwtx.core.runtime.IProgressMonitor; | 17 import dwtx.core.runtime.IProgressMonitor; |
21 import dwtx.core.runtime.IStatus; | 18 import dwtx.core.runtime.IStatus; |
22 import dwtx.core.runtime.OperationCanceledException; | 19 import dwtx.core.runtime.OperationCanceledException; |
30 import dwtx.core.commands.operations.IOperationHistory; | 27 import dwtx.core.commands.operations.IOperationHistory; |
31 import dwtx.core.commands.operations.IUndoContext; | 28 import dwtx.core.commands.operations.IUndoContext; |
32 import dwtx.core.commands.operations.OperationHistoryEvent; | 29 import dwtx.core.commands.operations.OperationHistoryEvent; |
33 | 30 |
34 import dwt.dwthelper.utils; | 31 import dwt.dwthelper.utils; |
32 import dwtx.dwtxhelper.Collection; | |
35 | 33 |
36 /** | 34 /** |
37 * Triggered operations are a specialized implementation of a composite | 35 * Triggered operations are a specialized implementation of a composite |
38 * operation that keeps track of operations triggered by the execution of some | 36 * operation that keeps track of operations triggered by the execution of some |
39 * primary operation. The composite knows which operation was the trigger for | 37 * primary operation. The composite knows which operation was the trigger for |
54 | 52 |
55 private IUndoableOperation triggeringOperation; | 53 private IUndoableOperation triggeringOperation; |
56 | 54 |
57 private IOperationHistory history; | 55 private IOperationHistory history; |
58 | 56 |
59 private Seq!(IUndoableOperation) children; | 57 private List children; |
60 | 58 |
61 /** | 59 /** |
62 * Construct a composite triggered operations using the specified undoable | 60 * Construct a composite triggered operations using the specified undoable |
63 * operation as the trigger. Use the label of this trigger as the label of | 61 * operation as the trigger. Use the label of this trigger as the label of |
64 * the operation. | 62 * the operation. |
69 * the operation history containing the triggered operations. | 67 * the operation history containing the triggered operations. |
70 */ | 68 */ |
71 public this(IUndoableOperation operation, | 69 public this(IUndoableOperation operation, |
72 IOperationHistory history) { | 70 IOperationHistory history) { |
73 super(operation.getLabel()); | 71 super(operation.getLabel()); |
74 children = new ArraySeq!(IUndoableOperation); | 72 children = new ArrayList(); |
75 triggeringOperation = operation; | 73 triggeringOperation = operation; |
76 recomputeContexts(); | 74 recomputeContexts(); |
77 this.history = history; | 75 this.history = history; |
78 } | 76 } |
79 | 77 |
81 * (non-Javadoc) | 79 * (non-Javadoc) |
82 * | 80 * |
83 * @see dwtx.core.commands.operations.IUndoableOperation#add(dwtx.core.commands.operations.IUndoableOperation) | 81 * @see dwtx.core.commands.operations.IUndoableOperation#add(dwtx.core.commands.operations.IUndoableOperation) |
84 */ | 82 */ |
85 public void add(IUndoableOperation operation) { | 83 public void add(IUndoableOperation operation) { |
86 children.append(operation); | 84 children.add(cast(Object)operation); |
87 recomputeContexts(); | 85 recomputeContexts(); |
88 } | 86 } |
89 | 87 |
90 /* | 88 /* |
91 * (non-Javadoc) | 89 * (non-Javadoc) |
99 triggeringOperation = null; | 97 triggeringOperation = null; |
100 // save the children before replacing the operation, since this | 98 // save the children before replacing the operation, since this |
101 // operation will be disposed as part of replacing it. We don't want | 99 // operation will be disposed as part of replacing it. We don't want |
102 // the children to be disposed since they are to replace this | 100 // the children to be disposed since they are to replace this |
103 // operation. | 101 // operation. |
104 Seq!(IUndoableOperation) childrenToRestore = children.dup; | 102 List childrenToRestore = new ArrayList(children); |
105 children = new ArraySeq!(IUndoableOperation); | 103 children = new ArrayList(0); |
106 recomputeContexts(); | 104 recomputeContexts(); |
107 operation.dispose(); | 105 operation.dispose(); |
108 // now replace the triggering operation | 106 // now replace the triggering operation |
109 history.replaceOperation(this, childrenToRestore.toArray()); | 107 history.replaceOperation(this, arraycast!(IUndoableOperation)(childrenToRestore.toArray())); |
110 } else { | 108 } else { |
111 children.remove(operation); | 109 children.remove(cast(Object)operation); |
112 operation.dispose(); | 110 operation.dispose(); |
113 recomputeContexts(); | 111 recomputeContexts(); |
114 } | 112 } |
115 } | 113 } |
116 | 114 |
140 } | 138 } |
141 triggeringOperation.removeContext(context); | 139 triggeringOperation.removeContext(context); |
142 recompute = true; | 140 recompute = true; |
143 } | 141 } |
144 // the triggering operation remains, check all the children | 142 // the triggering operation remains, check all the children |
145 auto toBeRemoved = new ArraySeq!(IUndoableOperation); | 143 auto toBeRemoved = new ArrayList(); |
146 for (int i = 0; i < children.size(); i++) { | 144 for (int i = 0; i < children.size(); i++) { |
147 IUndoableOperation child = cast(IUndoableOperation) children.get(i); | 145 IUndoableOperation child = cast(IUndoableOperation) children.get(i); |
148 if (child.hasContext(context)) { | 146 if (child.hasContext(context)) { |
149 if (child.getContexts().length is 1) { | 147 if (child.getContexts().length is 1) { |
150 toBeRemoved.append(child); | 148 toBeRemoved.add(cast(Object)child); |
151 } else { | 149 } else { |
152 child.removeContext(context); | 150 child.removeContext(context); |
153 } | 151 } |
154 recompute = true; | 152 recompute = true; |
155 } | 153 } |
195 * dwtx.core.runtime.IAdaptable) | 193 * dwtx.core.runtime.IAdaptable) |
196 */ | 194 */ |
197 public override IStatus redo(IProgressMonitor monitor, IAdaptable info) { | 195 public override IStatus redo(IProgressMonitor monitor, IAdaptable info) { |
198 if (triggeringOperation !is null) { | 196 if (triggeringOperation !is null) { |
199 history.openOperation(this, IOperationHistory.REDO); | 197 history.openOperation(this, IOperationHistory.REDO); |
200 Seq!(IUndoableOperation) childrenToRestore = children.dup; | 198 List childrenToRestore = new ArrayList(children); |
201 try { | 199 try { |
202 removeAllChildren(); | 200 removeAllChildren(); |
203 IStatus status = triggeringOperation.redo(monitor, info); | 201 IStatus status = triggeringOperation.redo(monitor, info); |
204 if (!status.isOK()) { | 202 if (!status.isOK()) { |
205 children = childrenToRestore; | 203 children = childrenToRestore; |
227 * dwtx.core.runtime.IAdaptable) | 225 * dwtx.core.runtime.IAdaptable) |
228 */ | 226 */ |
229 public override IStatus undo(IProgressMonitor monitor, IAdaptable info) { | 227 public override IStatus undo(IProgressMonitor monitor, IAdaptable info) { |
230 if (triggeringOperation !is null) { | 228 if (triggeringOperation !is null) { |
231 history.openOperation(this, IOperationHistory.UNDO); | 229 history.openOperation(this, IOperationHistory.UNDO); |
232 auto childrenToRestore = children.dup; | 230 List childrenToRestore = new ArrayList(children); |
233 try { | 231 try { |
234 removeAllChildren(); | 232 removeAllChildren(); |
235 IStatus status = triggeringOperation.undo(monitor, info); | 233 IStatus status = triggeringOperation.undo(monitor, info); |
236 if (!status.isOK()) { | 234 if (!status.isOK()) { |
237 children = childrenToRestore; | 235 children = childrenToRestore; |
291 /* | 289 /* |
292 * Dispose all operations in the receiver. | 290 * Dispose all operations in the receiver. |
293 */ | 291 */ |
294 public override void dispose() { | 292 public override void dispose() { |
295 for (int i = 0; i < children.size(); i++) { | 293 for (int i = 0; i < children.size(); i++) { |
296 children.get(i).dispose(); | 294 (cast(IUndoableOperation)children.get(i)).dispose(); |
297 } | 295 } |
298 if (triggeringOperation !is null) { | 296 if (triggeringOperation !is null) { |
299 triggeringOperation.dispose(); | 297 triggeringOperation.dispose(); |
300 } | 298 } |
301 } | 299 } |
302 | 300 |
303 /* | 301 /* |
304 * Recompute contexts in light of some change in the children | 302 * Recompute contexts in light of some change in the children |
305 */ | 303 */ |
306 private void recomputeContexts() { | 304 private void recomputeContexts() { |
307 auto allContexts = new ArraySeq!(IUndoContext); | 305 ArrayList allContexts = new ArrayList(); |
308 if (triggeringOperation !is null) { | 306 if (triggeringOperation !is null) { |
309 IUndoContext[] contexts = triggeringOperation.getContexts(); | 307 IUndoContext[] contexts = triggeringOperation.getContexts(); |
310 for (int i = 0; i < contexts.length; i++) { | 308 for (int i = 0; i < contexts.length; i++) { |
311 allContexts.append(contexts[i]); | 309 allContexts.add(cast(Object)contexts[i]); |
312 } | 310 } |
313 } | 311 } |
314 for (int i = 0; i < children.size(); i++) { | 312 for (int i = 0; i < children.size(); i++) { |
315 IUndoContext[] contexts = children.get(i).getContexts(); | 313 IUndoContext[] contexts = (cast(IUndoableOperation)children.get(i)) |
314 .getContexts(); | |
316 for (int j = 0; j < contexts.length; j++) { | 315 for (int j = 0; j < contexts.length; j++) { |
317 if (!allContexts.contains(contexts[j])) { | 316 if (!allContexts.contains(cast(Object)contexts[j])) { |
318 allContexts.append(contexts[j]); | 317 allContexts.add(cast(Object)contexts[j]); |
319 } | 318 } |
320 } | 319 } |
321 } | 320 } |
322 contexts = allContexts; | 321 contexts = allContexts; |
323 | 322 |
325 | 324 |
326 /* | 325 /* |
327 * Remove all non-triggering children | 326 * Remove all non-triggering children |
328 */ | 327 */ |
329 private void removeAllChildren() { | 328 private void removeAllChildren() { |
330 IUndoableOperation[] nonTriggers = children.toArray(); | 329 IUndoableOperation[] nonTriggers = arraycast!(IUndoableOperation)(children |
330 .toArray()); | |
331 for (int i = 0; i < nonTriggers.length; i++) { | 331 for (int i = 0; i < nonTriggers.length; i++) { |
332 children.remove(nonTriggers[i]); | 332 children.remove(cast(Object)nonTriggers[i]); |
333 nonTriggers[i].dispose(); | 333 nonTriggers[i].dispose(); |
334 } | 334 } |
335 } | 335 } |
336 | 336 |
337 /** | 337 /** |
429 triggeringOperation.addContext(replacement); | 429 triggeringOperation.addContext(replacement); |
430 } | 430 } |
431 } | 431 } |
432 // Now check all the children | 432 // Now check all the children |
433 for (int i = 0; i < children.size(); i++) { | 433 for (int i = 0; i < children.size(); i++) { |
434 IUndoableOperation child = children.get(i); | 434 IUndoableOperation child = cast(IUndoableOperation)children.get(i); |
435 if (child.hasContext(original)) { | 435 if (child.hasContext(original)) { |
436 if ( auto c = cast(IContextReplacingOperation)child ) { | 436 if ( auto c = cast(IContextReplacingOperation)child ) { |
437 c.replaceContext( | 437 c.replaceContext( |
438 original, replacement); | 438 original, replacement); |
439 } else { | 439 } else { |