01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.text.edits;
11:
12: import java.util.ArrayList;
13: import java.util.HashMap;
14: import java.util.Iterator;
15: import java.util.List;
16: import java.util.Map;
17:
18: import org.eclipse.core.runtime.Assert;
19:
20: /**
21: * Copies a tree of text edits. A text edit copier keeps a map
22: * between original and new text edits. It can be used to map
23: * a copy back to its original edit.
24: *
25: * @since 3.0
26: */
27: public final class TextEditCopier {
28:
29: private TextEdit fEdit;
30: private Map fCopies;
31:
32: /**
33: * Constructs a new <code>TextEditCopier</code> for the
34: * given edit. The actual copy is done by calling <code>
35: * perform</code>.
36: *
37: * @param edit the edit to copy
38: *
39: * @see #perform()
40: */
41: public TextEditCopier(TextEdit edit) {
42: super ();
43: Assert.isNotNull(edit);
44: fEdit = edit;
45: fCopies = new HashMap();
46: }
47:
48: /**
49: * Performs the actual copying.
50: *
51: * @return the copy
52: */
53: public TextEdit perform() {
54: TextEdit result = doCopy(fEdit);
55: if (result != null) {
56: for (Iterator iter = fCopies.keySet().iterator(); iter
57: .hasNext();) {
58: TextEdit edit = (TextEdit) iter.next();
59: edit.postProcessCopy(this );
60: }
61: }
62: return result;
63: }
64:
65: /**
66: * Returns the copy for the original text edit.
67: *
68: * @param original the original for which the copy
69: * is requested
70: * @return the copy of the original edit or <code>null</code>
71: * if the original isn't managed by this copier
72: */
73: public TextEdit getCopy(TextEdit original) {
74: Assert.isNotNull(original);
75: return (TextEdit) fCopies.get(original);
76: }
77:
78: //---- helper methods --------------------------------------------
79:
80: private TextEdit doCopy(TextEdit edit) {
81: TextEdit result = edit.doCopy();
82: List children = edit.internalGetChildren();
83: if (children != null) {
84: List newChildren = new ArrayList(children.size());
85: for (Iterator iter = children.iterator(); iter.hasNext();) {
86: TextEdit childCopy = doCopy((TextEdit) iter.next());
87: childCopy.internalSetParent(result);
88: newChildren.add(childCopy);
89: }
90: result.internalSetChildren(newChildren);
91: }
92: addCopy(edit, result);
93: return result;
94: }
95:
96: private void addCopy(TextEdit original, TextEdit copy) {
97: fCopies.put(original, copy);
98: }
99: }
|