KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > dom > ReplaceRewrite


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.jdt.internal.corext.dom;
12
13 import org.eclipse.text.edits.TextEditGroup;
14
15 import org.eclipse.core.runtime.Assert;
16
17 import org.eclipse.jdt.core.dom.ASTNode;
18 import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor;
19 import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
20 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
21 import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
22
23
24
25 public class ReplaceRewrite {
26     
27     protected ASTRewrite fRewrite;
28     protected ASTNode[] fToReplace;
29     protected StructuralPropertyDescriptor fDescriptor;
30     
31     public static ReplaceRewrite create(ASTRewrite rewrite, ASTNode[] nodes) {
32         return new ReplaceRewrite(rewrite, nodes);
33     }
34     
35     protected ReplaceRewrite(ASTRewrite rewrite, ASTNode[] nodes) {
36         Assert.isNotNull(rewrite);
37         Assert.isNotNull(nodes);
38         Assert.isTrue(nodes.length > 0);
39         fRewrite= rewrite;
40         fToReplace= nodes;
41         fDescriptor= fToReplace[0].getLocationInParent();
42         if (nodes.length > 1) {
43             Assert.isTrue(fDescriptor instanceof ChildListPropertyDescriptor);
44         }
45     }
46     
47     public void replace(ASTNode[] replacements, TextEditGroup description) {
48         if (fToReplace.length == 1) {
49             if (replacements.length == 1) {
50                 handleOneOne(replacements, description);
51             } else {
52                 handleOneMany(replacements, description);
53             }
54         } else {
55             handleManyMany(replacements, description);
56         }
57     }
58
59     protected void handleOneOne(ASTNode[] replacements, TextEditGroup description) {
60         fRewrite.replace(fToReplace[0], replacements[0], description);
61     }
62
63     protected void handleOneMany(ASTNode[] replacements, TextEditGroup description) {
64         handleManyMany(replacements, description);
65     }
66     
67     protected void handleManyMany(ASTNode[] replacements, TextEditGroup description) {
68         ListRewrite container= fRewrite.getListRewrite(fToReplace[0].getParent(), (ChildListPropertyDescriptor)fDescriptor);
69         if (fToReplace.length == replacements.length) {
70             for (int i= 0; i < fToReplace.length; i++) {
71                 container.replace(fToReplace[i], replacements[i], description);
72             }
73         } else if (fToReplace.length < replacements.length) {
74             for (int i= 0; i < fToReplace.length; i++) {
75                 container.replace(fToReplace[i], replacements[i], description);
76             }
77             for (int i= fToReplace.length; i < replacements.length; i++) {
78                 container.insertAfter(replacements[i], replacements[i - 1], description);
79             }
80         } else if (fToReplace.length > replacements.length) {
81             int delta= fToReplace.length - replacements.length;
82             for(int i= 0; i < delta; i++) {
83                 container.remove(fToReplace[i], description);
84             }
85             for (int i= delta, r= 0; i < fToReplace.length; i++, r++) {
86                 container.replace(fToReplace[i], replacements[r], description);
87             }
88         }
89     }
90 }
91
Popular Tags