KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > changes > TextChangeCompatibility


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.refactoring.changes;
12
13 import org.eclipse.text.edits.MalformedTreeException;
14 import org.eclipse.text.edits.MultiTextEdit;
15 import org.eclipse.text.edits.TextEdit;
16 import org.eclipse.text.edits.TextEditGroup;
17
18 import org.eclipse.core.runtime.Assert;
19
20 import org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup;
21 import org.eclipse.ltk.core.refactoring.GroupCategorySet;
22 import org.eclipse.ltk.core.refactoring.TextChange;
23 import org.eclipse.ltk.core.refactoring.TextEditChangeGroup;
24
25
26 /**
27  * A utility class to provide compatibility with the old
28  * text change API of adding text edits directly and auto
29  * inserting them into the tree.
30  */

31 public class TextChangeCompatibility {
32
33     public static void addTextEdit(TextChange change, String JavaDoc name, TextEdit edit) throws MalformedTreeException {
34         Assert.isNotNull(change);
35         Assert.isNotNull(name);
36         Assert.isNotNull(edit);
37         TextEdit root= change.getEdit();
38         if (root == null) {
39             root= new MultiTextEdit();
40             change.setEdit(root);
41         }
42         insert(root, edit);
43         change.addTextEditGroup(new TextEditGroup(name, edit));
44     }
45     
46     public static void addTextEdit(TextChange change, String JavaDoc name, TextEdit edit, GroupCategorySet groupCategories) throws MalformedTreeException {
47         Assert.isNotNull(change);
48         Assert.isNotNull(name);
49         Assert.isNotNull(edit);
50         TextEdit root= change.getEdit();
51         if (root == null) {
52             root= new MultiTextEdit();
53             change.setEdit(root);
54         }
55         insert(root, edit);
56         change.addTextEditChangeGroup(new TextEditChangeGroup(
57             change,
58             new CategorizedTextEditGroup(name, edit, groupCategories)));
59     }
60     
61     public static void insert(TextEdit parent, TextEdit edit) throws MalformedTreeException {
62         if (!parent.hasChildren()) {
63             parent.addChild(edit);
64             return;
65         }
66         TextEdit[] children= parent.getChildren();
67         // First dive down to find the right parent.
68
for (int i= 0; i < children.length; i++) {
69             TextEdit child= children[i];
70             if (covers(child, edit)) {
71                 insert(child, edit);
72                 return;
73             }
74         }
75         // We have the right parent. Now check if some of the children have to
76
// be moved under the new edit since it is covering it.
77
int removed= 0;
78         for (int i= 0; i < children.length; i++) {
79             TextEdit child= children[i];
80             if (covers(edit, child)) {
81                 parent.removeChild(i - removed++);
82                 edit.addChild(child);
83             }
84         }
85         parent.addChild(edit);
86     }
87     
88     private static boolean covers(TextEdit thisEdit, TextEdit otherEdit) {
89         if (thisEdit.getLength() == 0) // an insertion point can't cover anything
90
return false;
91         
92         int thisOffset= thisEdit.getOffset();
93         int thisEnd= thisEdit.getExclusiveEnd();
94         if (otherEdit.getLength() == 0) {
95             int otherOffset= otherEdit.getOffset();
96             return thisOffset < otherOffset && otherOffset < thisEnd;
97         } else {
98             int otherOffset= otherEdit.getOffset();
99             int otherEnd= otherEdit.getExclusiveEnd();
100             return thisOffset <= otherOffset && otherEnd <= thisEnd;
101         }
102     }
103     
104 }
105
Popular Tags