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.internal.utils.Assert;
12 import org.eclipse.core.runtime.CoreException;
15 public class MultiTextEdit {
17 private List fChildren;
20 * Creates a new composite text edit.
22 public MultiTextEdit() {
23 fChildren= new ArrayList(3);
26 protected MultiTextEdit(List children) throws CoreException {
27 fChildren= new ArrayList(children.size());
28 for (Iterator iter= children.iterator(); iter.hasNext();) {
29 fChildren.add(((TextEdit)iter.next()).copy());
33 protected List getChildren() {
38 * Adds all <code>TextEdits</code> managed by the given multt text edit.
40 * @param edit the multi text edit to be added.
42 public void add(MultiTextEdit edit) {
43 Assert.isNotNull(edit);
50 * @param edit the text edit to be added
52 public void add(TextEdit edit) {
53 Assert.isNotNull(edit);
58 * Returns the children managed by this text edit collection.
60 * @return the children of this composite text edit
62 public Iterator iterator() {
63 return fChildren.iterator();
67 * Connects this text edit to the given <code>TextBufferEditor</code>.
68 * Note that this method <b>should only be called</b> by a <code>
69 * TextBufferEditor</code>.
71 * This default implementation does nothing. Subclasses may override
74 * @param editor the text buffer editor this text edit has been added to
76 public void connect(TextBufferEditor editor) throws CoreException {
77 for (Iterator iter= fChildren.iterator(); iter.hasNext();) {
78 Object element= iter.next();
79 if (element instanceof TextEdit)
80 editor.add((TextEdit)element);
82 editor.add((MultiTextEdit)element);
87 * Creates and returns a copy of this text edit collection. The copy method should
88 * be implemented in a way so that the copy can be added to a different <code>
89 * TextBuffer</code> without causing any harm to the object from which the copy
92 * @return a copy of this object.
94 public MultiTextEdit copy() throws CoreException {
95 return new MultiTextEdit(fChildren);
99 * Returns the <code>TextRange</code> that this text edit is going to
100 * manipulate. If this method is called before the <code>MultiTextEdit</code>
101 * has been added to a <code>TextBufferEditor</code> it may return <code>
102 * null</code> to indicate this situation.
104 * @return the <code>TextRange</code>s this <code>TextEdit is going
107 public TextRange getTextRange() {
108 int size= fChildren.size();
110 return new TextRange(0,0);
111 TextRange range= ((TextEdit)fChildren.get(0)).getTextRange();
112 int start= range.getOffset();
113 int end= range.getInclusiveEnd();
114 for (int i= 1; i < size; i++) {
115 range= ((TextEdit)fChildren.get(i)).getTextRange();
116 start= Math.min(start, range.getOffset());
117 end= Math.max(end, range.getInclusiveEnd());
119 return new TextRange(start, end - start + 1);
123 * Returns the element modified by this text edit. The method
124 * may return <code>null</code> if the modification isn't related to a
125 * element or if the content of the modified text buffer doesn't
128 * This default implementation returns <code>null</code>
130 * @return the element modified by this text edit
132 public Object getModifiedElement() {