Refactory: Remove UI from core plugin.
[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 public class MultiTextEdit {
15
16         private List fChildren;
17
18         /**
19          * Creates a new composite text edit.
20          */
21         public MultiTextEdit() {
22                 fChildren = new ArrayList(3);
23         }
24
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());
29                 }
30         }
31
32         protected List getChildren() {
33                 return fChildren;
34         }
35
36         /**
37          * Adds all <code>TextEdits</code> managed by the given multt text edit.
38          * 
39          * @param edit
40          *            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
51          *            the text edit to be added
52          */
53         public void add(TextEdit edit) {
54                 Assert.isNotNull(edit);
55                 fChildren.add(edit);
56         }
57
58         /**
59          * Returns the children managed by this text edit collection.
60          * 
61          * @return the children of this composite text edit
62          */
63         public Iterator iterator() {
64                 return fChildren.iterator();
65         }
66
67         /**
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>.
71          * <p>
72          * This default implementation does nothing. Subclasses may override if
73          * needed.
74          * 
75          * @param editor
76          *            the text buffer editor this text edit has been added to
77          */
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);
83                         else
84                                 editor.add((MultiTextEdit) element);
85                 }
86         }
87
88         /**
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
91          * different <code>
92          * TextBuffer</code> without causing any harm to the
93          * object from which the copy has been created.
94          * 
95          * @return a copy of this object.
96          */
97         public MultiTextEdit copy() throws CoreException {
98                 return new MultiTextEdit(fChildren);
99         }
100
101         /**
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>
106          * null</code> to
107          * indicate this situation.
108          * 
109          * @return the <code>TextRange</code>s this <code>TextEdit is going
110          *      to manipulate
111          */
112         public TextRange getTextRange() {
113                 int size = fChildren.size();
114                 if (size == 0)
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());
123                 }
124                 return new TextRange(start, end - start + 1);
125         }
126
127         /**
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.
131          * <p>
132          * This default implementation returns <code>null</code>
133          * 
134          * @return the element modified by this text edit
135          */
136         public Object getModifiedElement() {
137                 return null;
138         }
139 }