129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 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.text.edits.TextEditCopier;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17 import java.util.ArrayList;
|
|
18 import java.util.HashMap;
|
|
19 import java.util.Iterator;
|
|
20 import java.util.List;
|
|
21 import java.util.Map;
|
|
22
|
|
23 import dwtx.core.runtime.Assert;
|
|
24
|
|
25
|
|
26 /**
|
|
27 * Copies a tree of text edits. A text edit copier keeps a map
|
|
28 * between original and new text edits. It can be used to map
|
|
29 * a copy back to its original edit.
|
|
30 *
|
|
31 * @since 3.0
|
|
32 */
|
|
33 public final class TextEditCopier {
|
|
34
|
|
35 private TextEdit fEdit;
|
|
36 private Map fCopies;
|
|
37
|
|
38 /**
|
|
39 * Constructs a new <code>TextEditCopier</code> for the
|
|
40 * given edit. The actual copy is done by calling <code>
|
|
41 * perform</code>.
|
|
42 *
|
|
43 * @param edit the edit to copy
|
|
44 *
|
|
45 * @see #perform()
|
|
46 */
|
|
47 public TextEditCopier(TextEdit edit) {
|
|
48 super();
|
|
49 Assert.isNotNull(edit);
|
|
50 fEdit= edit;
|
|
51 fCopies= new HashMap();
|
|
52 }
|
|
53
|
|
54 /**
|
|
55 * Performs the actual copying.
|
|
56 *
|
|
57 * @return the copy
|
|
58 */
|
|
59 public TextEdit perform() {
|
|
60 TextEdit result= doCopy(fEdit);
|
|
61 if (result !is null) {
|
|
62 for (Iterator iter= fCopies.keySet().iterator(); iter.hasNext();) {
|
|
63 TextEdit edit= (TextEdit)iter.next();
|
|
64 edit.postProcessCopy(this);
|
|
65 }
|
|
66 }
|
|
67 return result;
|
|
68 }
|
|
69
|
|
70 /**
|
|
71 * Returns the copy for the original text edit.
|
|
72 *
|
|
73 * @param original the original for which the copy
|
|
74 * is requested
|
|
75 * @return the copy of the original edit or <code>null</code>
|
|
76 * if the original isn't managed by this copier
|
|
77 */
|
|
78 public TextEdit getCopy(TextEdit original) {
|
|
79 Assert.isNotNull(original);
|
|
80 return (TextEdit)fCopies.get(original);
|
|
81 }
|
|
82
|
|
83 //---- helper methods --------------------------------------------
|
|
84
|
|
85 private TextEdit doCopy(TextEdit edit) {
|
|
86 TextEdit result= edit.doCopy();
|
|
87 List children= edit.internalGetChildren();
|
|
88 if (children !is null) {
|
|
89 List newChildren= new ArrayList(children.size());
|
|
90 for (Iterator iter= children.iterator(); iter.hasNext();) {
|
|
91 TextEdit childCopy= doCopy((TextEdit)iter.next());
|
|
92 childCopy.internalSetParent(result);
|
|
93 newChildren.add(childCopy);
|
|
94 }
|
|
95 result.internalSetChildren(newChildren);
|
|
96 }
|
|
97 addCopy(edit, result);
|
|
98 return result;
|
|
99 }
|
|
100
|
|
101 private void addCopy(TextEdit original, TextEdit copy) {
|
|
102 fCopies.put(original, copy);
|
|
103 }
|
|
104 }
|