1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.dom;
13 import org.eclipse.core.runtime.Assert;
15 import org.eclipse.text.edits.TextEditGroup;
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;
25 public class ReplaceRewrite {
27 protected ASTRewrite fRewrite;
28 protected ASTNode[] fToReplace;
29 protected StructuralPropertyDescriptor fDescriptor;
31 public static ReplaceRewrite create(ASTRewrite rewrite, ASTNode[] nodes) {
32 return new ReplaceRewrite(rewrite, nodes);
35 protected ReplaceRewrite(ASTRewrite rewrite, ASTNode[] nodes) {
36 Assert.isNotNull(rewrite);
37 Assert.isNotNull(nodes);
38 Assert.isTrue(nodes.length > 0);
41 fDescriptor= fToReplace[0].getLocationInParent();
42 if (nodes.length > 1) {
43 Assert.isTrue(fDescriptor instanceof ChildListPropertyDescriptor);
47 public void replace(ASTNode[] replacements, TextEditGroup description) {
48 if (fToReplace.length == 1) {
49 if (replacements.length == 1) {
50 handleOneOne(replacements, description);
52 handleOneMany(replacements, description);
55 handleManyMany(replacements, description);
59 protected void handleOneOne(ASTNode[] replacements, TextEditGroup description) {
60 fRewrite.replace(fToReplace[0], replacements[0], description);
63 protected void handleOneMany(ASTNode[] replacements, TextEditGroup description) {
64 handleManyMany(replacements, description);
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);
73 } else if (fToReplace.length < replacements.length) {
74 for (int i= 0; i < fToReplace.length; i++) {
75 container.replace(fToReplace[i], replacements[i], description);
77 for (int i= fToReplace.length; i < replacements.length; i++) {
78 container.insertAfter(replacements[i], replacements[i - 1], description);
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);
85 for (int i= delta, r= 0; i < fToReplace.length; i++, r++) {
86 container.replace(fToReplace[i], replacements[r], description);