2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.corext.textmanipulation;
7 import java.util.ArrayList;
8 import java.util.Iterator;
11 import org.eclipse.core.runtime.CoreException;
12 import org.eclipse.jface.util.Assert;
14 public class MultiTextEdit {
16 private List fChildren;
19 * Creates a new composite text edit.
21 public MultiTextEdit() {
22 fChildren = new ArrayList(3);
25 protected MultiTextEdit(List children) throws CoreException {
26 fChildren = new ArrayList(children.size());
27 for (Iterator iter = children.iterator(); iter.hasNext();) {
28 fChildren.add(((TextEdit) iter.next()).copy());
32 protected List getChildren() {
37 * Adds all <code>TextEdits</code> managed by the given multt text edit.
40 * the multi text edit to be added.
42 public void add(MultiTextEdit edit) {
43 Assert.isNotNull(edit);
51 * the text edit to be added
53 public void add(TextEdit edit) {
54 Assert.isNotNull(edit);
59 * Returns the children managed by this text edit collection.
61 * @return the children of this composite text edit
63 public Iterator iterator() {
64 return fChildren.iterator();
68 * Connects this text edit to the given <code>TextBufferEditor</code>.
69 * Note that this method <b>should only be called</b> by a <code>
70 * TextBufferEditor</code>.
72 * This default implementation does nothing. Subclasses may override if
76 * the text buffer editor this text edit has been added to
78 public void connect(TextBufferEditor editor) throws CoreException {
79 for (Iterator iter = fChildren.iterator(); iter.hasNext();) {
80 Object element = iter.next();
81 if (element instanceof TextEdit)
82 editor.add((TextEdit) element);
84 editor.add((MultiTextEdit) element);
89 * Creates and returns a copy of this text edit collection. The copy method
90 * should be implemented in a way so that the copy can be added to a
92 * TextBuffer</code> without causing any harm to the
93 * object from which the copy has been created.
95 * @return a copy of this object.
97 public MultiTextEdit copy() throws CoreException {
98 return new MultiTextEdit(fChildren);
102 * Returns the <code>TextRange</code> that this text edit is going to
103 * manipulate. If this method is called before the
104 * <code>MultiTextEdit</code> has been added to a
105 * <code>TextBufferEditor</code> it may return <code>
107 * indicate this situation.
109 * @return the <code>TextRange</code>s this <code>TextEdit is going
112 public TextRange getTextRange() {
113 int size = fChildren.size();
115 return new TextRange(0, 0);
116 TextRange range = ((TextEdit) fChildren.get(0)).getTextRange();
117 int start = range.getOffset();
118 int end = range.getInclusiveEnd();
119 for (int i = 1; i < size; i++) {
120 range = ((TextEdit) fChildren.get(i)).getTextRange();
121 start = Math.min(start, range.getOffset());
122 end = Math.max(end, range.getInclusiveEnd());
124 return new TextRange(start, end - start + 1);
128 * Returns the element modified by this text edit. The method may return
129 * <code>null</code> if the modification isn't related to a element or if
130 * the content of the modified text buffer doesn't follow any syntax.
132 * This default implementation returns <code>null</code>
134 * @return the element modified by this text edit
136 public Object getModifiedElement() {