make compile in 3.2 by removing internal dependency
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / textmanipulation / MultiTextEdit.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.corext.textmanipulation;
6
7 import java.util.ArrayList;
8 import java.util.Iterator;
9 import java.util.List;
10
11 import org.eclipse.core.runtime.CoreException;
12 import org.eclipse.jface.util.Assert;
13
14
15 public class MultiTextEdit {
16
17         private List fChildren;
18
19         /**
20          * Creates a new composite text edit.
21          */
22         public MultiTextEdit() {
23                 fChildren= new ArrayList(3);
24         }
25
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());
30                 }
31         }
32
33         protected List getChildren() {
34                 return fChildren;
35         }
36
37         /**
38          * Adds all <code>TextEdits</code> managed by the given multt text edit.
39          * 
40          * @param edit the multi text edit to be added.
41          */
42         public void add(MultiTextEdit edit) {
43                 Assert.isNotNull(edit);
44                 fChildren.add(edit);
45         }
46
47         /**
48          * Adds a text edit.
49          * 
50          * @param edit the text edit to be added
51          */
52         public void add(TextEdit edit) {
53                 Assert.isNotNull(edit);
54                 fChildren.add(edit);
55         }
56
57         /**
58          * Returns the children managed by this text edit collection.
59          * 
60          * @return the children of this composite text edit
61          */
62         public Iterator iterator() {
63                 return fChildren.iterator();
64         }
65
66         /**
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>.
70          *<p>
71          * This default implementation does nothing. Subclasses may override
72          * if needed.
73          *  
74          * @param editor the text buffer editor this text edit has been added to
75          */
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);
81                         else
82                                 editor.add((MultiTextEdit)element);
83                 }
84         }
85
86         /**
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
90          * has been created.
91          *
92          * @return a copy of this object.
93          */
94         public MultiTextEdit copy() throws CoreException {
95                 return new MultiTextEdit(fChildren);
96         }
97
98         /**
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.
103          * 
104          * @return the <code>TextRange</code>s this <code>TextEdit is going
105          *      to manipulate
106          */
107         public TextRange getTextRange() {
108                 int size= fChildren.size();
109                 if (size == 0)
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());
118                 }
119                 return new TextRange(start, end - start + 1);
120         }
121
122         /**
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
126          * follow any syntax.
127          * <p>
128          * This default implementation returns <code>null</code>
129          * 
130          * @return the element modified by this text edit
131          */
132         public Object getModifiedElement() {
133                 return null;
134         }
135 }
136