001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring.changes;
011:
012: import org.eclipse.text.edits.MalformedTreeException;
013: import org.eclipse.text.edits.MultiTextEdit;
014: import org.eclipse.text.edits.TextEdit;
015: import org.eclipse.text.edits.TextEditGroup;
016:
017: import org.eclipse.core.runtime.Assert;
018:
019: import org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup;
020: import org.eclipse.ltk.core.refactoring.GroupCategorySet;
021: import org.eclipse.ltk.core.refactoring.TextChange;
022: import org.eclipse.ltk.core.refactoring.TextEditChangeGroup;
023:
024: /**
025: * A utility class to provide compatibility with the old
026: * text change API of adding text edits directly and auto
027: * inserting them into the tree.
028: */
029: public class TextChangeCompatibility {
030:
031: public static void addTextEdit(TextChange change, String name,
032: TextEdit edit) throws MalformedTreeException {
033: Assert.isNotNull(change);
034: Assert.isNotNull(name);
035: Assert.isNotNull(edit);
036: TextEdit root = change.getEdit();
037: if (root == null) {
038: root = new MultiTextEdit();
039: change.setEdit(root);
040: }
041: insert(root, edit);
042: change.addTextEditGroup(new TextEditGroup(name, edit));
043: }
044:
045: public static void addTextEdit(TextChange change, String name,
046: TextEdit edit, GroupCategorySet groupCategories)
047: throws MalformedTreeException {
048: Assert.isNotNull(change);
049: Assert.isNotNull(name);
050: Assert.isNotNull(edit);
051: TextEdit root = change.getEdit();
052: if (root == null) {
053: root = new MultiTextEdit();
054: change.setEdit(root);
055: }
056: insert(root, edit);
057: change.addTextEditChangeGroup(new TextEditChangeGroup(change,
058: new CategorizedTextEditGroup(name, edit,
059: groupCategories)));
060: }
061:
062: public static void insert(TextEdit parent, TextEdit edit)
063: throws MalformedTreeException {
064: if (!parent.hasChildren()) {
065: parent.addChild(edit);
066: return;
067: }
068: TextEdit[] children = parent.getChildren();
069: // First dive down to find the right parent.
070: for (int i = 0; i < children.length; i++) {
071: TextEdit child = children[i];
072: if (covers(child, edit)) {
073: insert(child, edit);
074: return;
075: }
076: }
077: // We have the right parent. Now check if some of the children have to
078: // be moved under the new edit since it is covering it.
079: int removed = 0;
080: for (int i = 0; i < children.length; i++) {
081: TextEdit child = children[i];
082: if (covers(edit, child)) {
083: parent.removeChild(i - removed++);
084: edit.addChild(child);
085: }
086: }
087: parent.addChild(edit);
088: }
089:
090: private static boolean covers(TextEdit this Edit, TextEdit otherEdit) {
091: if (this Edit.getLength() == 0) // an insertion point can't cover anything
092: return false;
093:
094: int this Offset = this Edit.getOffset();
095: int this End = this Edit.getExclusiveEnd();
096: if (otherEdit.getLength() == 0) {
097: int otherOffset = otherEdit.getOffset();
098: return this Offset < otherOffset && otherOffset < this End;
099: } else {
100: int otherOffset = otherEdit.getOffset();
101: int otherEnd = otherEdit.getExclusiveEnd();
102: return thisOffset <= otherOffset && otherEnd <= thisEnd;
103: }
104: }
105:
106: }
|