KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > text > edits > MultiTextEdit


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.text.edits;
12
13 import java.util.List JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.IRegion;
20
21 /**
22  * A multi-text edit can be used to aggregate several edits into
23  * one edit. The edit itself doesn't modify a document.
24  * <p>
25  * Clients are allowed to implement subclasses of a multi-text
26  * edit.Subclasses must implement <code>doCopy()</code> to ensure
27  * the a copy of the right type is created. Not implementing
28  * <code>doCopy()</code> in subclasses will result in an assertion
29  * failure during copying.
30  *
31  * @since 3.0
32  */

33 public class MultiTextEdit extends TextEdit {
34
35     private boolean fDefined;
36
37     /**
38      * Creates a new <code>MultiTextEdit</code>. The range
39      * of the edit is determined by the range of its children.
40      *
41      * Adding this edit to a parent edit sets its range to the
42      * range covered by its children. If the edit doesn't have
43      * any children its offset is set to the parent's offset
44      * and its length is set to 0.
45      */

46     public MultiTextEdit() {
47         super(0, Integer.MAX_VALUE);
48         fDefined= false;
49     }
50
51     /**
52      * Creates a new </code>MultiTextEdit</code> for the given
53      * range. Adding a child to this edit which isn't covered
54      * by the given range will result in an exception.
55      *
56      * @param offset the edit's offset
57      * @param length the edit's length.
58      * @see TextEdit#addChild(TextEdit)
59      * @see TextEdit#addChildren(TextEdit[])
60      */

61     public MultiTextEdit(int offset, int length) {
62         super(offset, length);
63         fDefined= true;
64     }
65
66     /*
67      * Copy constructor.
68      */

69     protected MultiTextEdit(MultiTextEdit other) {
70         super(other);
71     }
72
73     /**
74      * Checks the edit's integrity.
75      * <p>
76      * Note that this method <b>should only be called</b> by the edit
77      * framework and not by normal clients.</p>
78      *<p>
79      * This default implementation does nothing. Subclasses may override
80      * if needed.</p>
81      *
82      * @exception MalformedTreeException if the edit isn't in a valid state
83      * and can therefore not be executed
84      */

85     protected void checkIntegrity() throws MalformedTreeException {
86         // does nothing
87
}
88
89     /**
90      * {@inheritDoc}
91      */

92     final boolean isDefined() {
93         if (fDefined)
94             return true;
95         return hasChildren();
96     }
97
98     /**
99      * {@inheritDoc}
100      */

101     public final int getOffset() {
102         if (fDefined)
103             return super.getOffset();
104
105         List JavaDoc/*<TextEdit>*/ children= internalGetChildren();
106         if (children == null || children.size() == 0)
107             return 0;
108         // the children are already sorted
109
return ((TextEdit)children.get(0)).getOffset();
110     }
111
112     /**
113      * {@inheritDoc}
114      */

115     public final int getLength() {
116         if (fDefined)
117             return super.getLength();
118
119         List JavaDoc/*<TextEdit>*/ children= internalGetChildren();
120         if (children == null || children.size() == 0)
121             return 0;
122         // the children are already sorted
123
TextEdit first= (TextEdit)children.get(0);
124         TextEdit last= (TextEdit)children.get(children.size() - 1);
125         return last.getOffset() - first.getOffset() + last.getLength();
126     }
127
128     /**
129      * {@inheritDoc}
130      */

131     public final boolean covers(TextEdit other) {
132         if (fDefined)
133             return super.covers(other);
134         // an undefined multiple text edit covers everything
135
return true;
136     }
137
138     /*
139      * @see org.eclipse.text.edits.TextEdit#canZeroLengthCover()
140      */

141     protected boolean canZeroLengthCover() {
142         return true;
143     }
144
145     /*
146      * @see TextEdit#copy
147      */

148     protected TextEdit doCopy() {
149         Assert.isTrue(MultiTextEdit.class == getClass(), "Subclasses must reimplement copy0"); //$NON-NLS-1$
150
return new MultiTextEdit(this);
151     }
152
153     /*
154      * @see TextEdit#accept0
155      */

156     protected void accept0(TextEditVisitor visitor) {
157         boolean visitChildren= visitor.visit(this);
158         if (visitChildren) {
159             acceptChildren(visitor);
160         }
161     }
162
163     /*
164      * @see org.eclipse.text.edits.TextEdit#adjustOffset(int)
165      * @since 3.1
166      */

167     void adjustOffset(int delta) {
168         if (fDefined)
169             super.adjustOffset(delta);
170     }
171
172     /*
173      * @see org.eclipse.text.edits.TextEdit#adjustLength(int)
174      * @since 3.1
175      */

176     void adjustLength(int delta) {
177         if (fDefined)
178             super.adjustLength(delta);
179     }
180
181     /*
182      * @see TextEdit#performConsistencyCheck
183      */

184     void performConsistencyCheck(TextEditProcessor processor, IDocument document) throws MalformedTreeException {
185         checkIntegrity();
186     }
187
188     /*
189      * @see TextEdit#performDocumentUpdating
190      */

191     int performDocumentUpdating(IDocument document) throws BadLocationException {
192         fDelta= 0;
193         return fDelta;
194     }
195
196     /*
197      * @see TextEdit#deleteChildren
198      */

199     boolean deleteChildren() {
200         return false;
201     }
202
203     void aboutToBeAdded(TextEdit parent) {
204         defineRegion(parent.getOffset());
205     }
206
207     void defineRegion(int parentOffset) {
208         if (fDefined)
209             return;
210         if (hasChildren()) {
211             IRegion region= getCoverage(getChildren());
212             internalSetOffset(region.getOffset());
213             internalSetLength(region.getLength());
214         } else {
215             internalSetOffset(parentOffset);
216             internalSetLength(0);
217         }
218         fDefined= true;
219     }
220
221     /*
222      * @see org.eclipse.text.edits.TextEdit#internalToString(java.lang.StringBuffer, int)
223      * @since 3.3
224      */

225     void internalToString(StringBuffer JavaDoc buffer, int indent) {
226         super.internalToString(buffer, indent);
227         if (! fDefined)
228             buffer.append(" [undefined]"); //$NON-NLS-1$
229
}
230 }
231
Popular Tags