KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > text > edits > TextEditCopier


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  *******************************************************************************/

11 package org.eclipse.text.edits;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20
21
22 /**
23  * Copies a tree of text edits. A text edit copier keeps a map
24  * between original and new text edits. It can be used to map
25  * a copy back to its original edit.
26  *
27  * @since 3.0
28  */

29 public final class TextEditCopier {
30
31     private TextEdit fEdit;
32     private Map JavaDoc fCopies;
33
34     /**
35      * Constructs a new <code>TextEditCopier</code> for the
36      * given edit. The actual copy is done by calling <code>
37      * perform</code>.
38      *
39      * @param edit the edit to copy
40      *
41      * @see #perform()
42      */

43     public TextEditCopier(TextEdit edit) {
44         super();
45         Assert.isNotNull(edit);
46         fEdit= edit;
47         fCopies= new HashMap JavaDoc();
48     }
49
50     /**
51      * Performs the actual copying.
52      *
53      * @return the copy
54      */

55     public TextEdit perform() {
56         TextEdit result= doCopy(fEdit);
57         if (result != null) {
58             for (Iterator JavaDoc iter= fCopies.keySet().iterator(); iter.hasNext();) {
59                 TextEdit edit= (TextEdit)iter.next();
60                 edit.postProcessCopy(this);
61             }
62         }
63         return result;
64     }
65
66     /**
67      * Returns the copy for the original text edit.
68      *
69      * @param original the original for which the copy
70      * is requested
71      * @return the copy of the original edit or <code>null</code>
72      * if the original isn't managed by this copier
73      */

74     public TextEdit getCopy(TextEdit original) {
75         Assert.isNotNull(original);
76         return (TextEdit)fCopies.get(original);
77     }
78
79     //---- helper methods --------------------------------------------
80

81     private TextEdit doCopy(TextEdit edit) {
82         TextEdit result= edit.doCopy();
83         List JavaDoc children= edit.internalGetChildren();
84         if (children != null) {
85             List JavaDoc newChildren= new ArrayList JavaDoc(children.size());
86             for (Iterator JavaDoc iter= children.iterator(); iter.hasNext();) {
87                 TextEdit childCopy= doCopy((TextEdit)iter.next());
88                 childCopy.internalSetParent(result);
89                 newChildren.add(childCopy);
90             }
91             result.internalSetChildren(newChildren);
92         }
93         addCopy(edit, result);
94         return result;
95     }
96
97     private void addCopy(TextEdit original, TextEdit copy) {
98         fCopies.put(original, copy);
99     }
100 }
101
Popular Tags