129
|
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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.text.edits.MultiTextEdit;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17 import java.util.List;
|
|
18
|
|
19 import dwtx.core.runtime.Assert;
|
|
20 import dwtx.jface.text.BadLocationException;
|
|
21 import dwtx.jface.text.IDocument;
|
|
22 import dwtx.jface.text.IRegion;
|
|
23
|
|
24 /**
|
|
25 * A multi-text edit can be used to aggregate several edits into
|
|
26 * one edit. The edit itself doesn't modify a document.
|
|
27 * <p>
|
|
28 * Clients are allowed to implement subclasses of a multi-text
|
|
29 * edit.Subclasses must implement <code>doCopy()</code> to ensure
|
|
30 * the a copy of the right type is created. Not implementing
|
|
31 * <code>doCopy()</code> in subclasses will result in an assertion
|
|
32 * failure during copying.
|
|
33 *
|
|
34 * @since 3.0
|
|
35 */
|
|
36 public class MultiTextEdit : TextEdit {
|
|
37
|
|
38 private bool fDefined;
|
|
39
|
|
40 /**
|
|
41 * Creates a new <code>MultiTextEdit</code>. The range
|
|
42 * of the edit is determined by the range of its children.
|
|
43 *
|
|
44 * Adding this edit to a parent edit sets its range to the
|
|
45 * range covered by its children. If the edit doesn't have
|
|
46 * any children its offset is set to the parent's offset
|
|
47 * and its length is set to 0.
|
|
48 */
|
130
|
49 public this() {
|
129
|
50 super(0, Integer.MAX_VALUE);
|
|
51 fDefined= false;
|
|
52 }
|
|
53
|
|
54 /**
|
|
55 * Creates a new </code>MultiTextEdit</code> for the given
|
|
56 * range. Adding a child to this edit which isn't covered
|
|
57 * by the given range will result in an exception.
|
|
58 *
|
|
59 * @param offset the edit's offset
|
|
60 * @param length the edit's length.
|
|
61 * @see TextEdit#addChild(TextEdit)
|
|
62 * @see TextEdit#addChildren(TextEdit[])
|
|
63 */
|
130
|
64 public this(int offset, int length) {
|
129
|
65 super(offset, length);
|
|
66 fDefined= true;
|
|
67 }
|
|
68
|
|
69 /*
|
|
70 * Copy constructor.
|
|
71 */
|
130
|
72 protected this(MultiTextEdit other) {
|
129
|
73 super(other);
|
|
74 }
|
|
75
|
|
76 /**
|
|
77 * Checks the edit's integrity.
|
|
78 * <p>
|
|
79 * Note that this method <b>should only be called</b> by the edit
|
|
80 * framework and not by normal clients.</p>
|
|
81 *<p>
|
|
82 * This default implementation does nothing. Subclasses may override
|
|
83 * if needed.</p>
|
|
84 *
|
|
85 * @exception MalformedTreeException if the edit isn't in a valid state
|
|
86 * and can therefore not be executed
|
|
87 */
|
|
88 protected void checkIntegrity() throws MalformedTreeException {
|
|
89 // does nothing
|
|
90 }
|
|
91
|
|
92 /**
|
|
93 * {@inheritDoc}
|
|
94 */
|
|
95 final bool isDefined() {
|
|
96 if (fDefined)
|
|
97 return true;
|
|
98 return hasChildren();
|
|
99 }
|
|
100
|
|
101 /**
|
|
102 * {@inheritDoc}
|
|
103 */
|
|
104 public final int getOffset() {
|
|
105 if (fDefined)
|
|
106 return super.getOffset();
|
|
107
|
|
108 List/*<TextEdit>*/ children= internalGetChildren();
|
|
109 if (children is null || children.size() is 0)
|
|
110 return 0;
|
|
111 // the children are already sorted
|
|
112 return ((TextEdit)children.get(0)).getOffset();
|
|
113 }
|
|
114
|
|
115 /**
|
|
116 * {@inheritDoc}
|
|
117 */
|
|
118 public final int getLength() {
|
|
119 if (fDefined)
|
|
120 return super.getLength();
|
|
121
|
|
122 List/*<TextEdit>*/ children= internalGetChildren();
|
|
123 if (children is null || children.size() is 0)
|
|
124 return 0;
|
|
125 // the children are already sorted
|
|
126 TextEdit first= (TextEdit)children.get(0);
|
|
127 TextEdit last= (TextEdit)children.get(children.size() - 1);
|
|
128 return last.getOffset() - first.getOffset() + last.getLength();
|
|
129 }
|
|
130
|
|
131 /**
|
|
132 * {@inheritDoc}
|
|
133 */
|
|
134 public final bool covers(TextEdit other) {
|
|
135 if (fDefined)
|
|
136 return super.covers(other);
|
|
137 // an undefined multiple text edit covers everything
|
|
138 return true;
|
|
139 }
|
|
140
|
|
141 /*
|
|
142 * @see dwtx.text.edits.TextEdit#canZeroLengthCover()
|
|
143 */
|
|
144 protected bool canZeroLengthCover() {
|
|
145 return true;
|
|
146 }
|
|
147
|
|
148 /*
|
|
149 * @see TextEdit#copy
|
|
150 */
|
|
151 protected TextEdit doCopy() {
|
|
152 Assert.isTrue(MultiTextEdit.class is getClass(), "Subclasses must reimplement copy0"); //$NON-NLS-1$
|
|
153 return new MultiTextEdit(this);
|
|
154 }
|
|
155
|
|
156 /*
|
|
157 * @see TextEdit#accept0
|
|
158 */
|
|
159 protected void accept0(TextEditVisitor visitor) {
|
|
160 bool visitChildren= visitor.visit(this);
|
|
161 if (visitChildren) {
|
|
162 acceptChildren(visitor);
|
|
163 }
|
|
164 }
|
|
165
|
|
166 /*
|
|
167 * @see dwtx.text.edits.TextEdit#adjustOffset(int)
|
|
168 * @since 3.1
|
|
169 */
|
|
170 void adjustOffset(int delta) {
|
|
171 if (fDefined)
|
|
172 super.adjustOffset(delta);
|
|
173 }
|
|
174
|
|
175 /*
|
|
176 * @see dwtx.text.edits.TextEdit#adjustLength(int)
|
|
177 * @since 3.1
|
|
178 */
|
|
179 void adjustLength(int delta) {
|
|
180 if (fDefined)
|
|
181 super.adjustLength(delta);
|
|
182 }
|
|
183
|
|
184 /*
|
|
185 * @see TextEdit#performConsistencyCheck
|
|
186 */
|
|
187 void performConsistencyCheck(TextEditProcessor processor, IDocument document) throws MalformedTreeException {
|
|
188 checkIntegrity();
|
|
189 }
|
|
190
|
|
191 /*
|
|
192 * @see TextEdit#performDocumentUpdating
|
|
193 */
|
|
194 int performDocumentUpdating(IDocument document) throws BadLocationException {
|
|
195 fDelta= 0;
|
|
196 return fDelta;
|
|
197 }
|
|
198
|
|
199 /*
|
|
200 * @see TextEdit#deleteChildren
|
|
201 */
|
|
202 bool deleteChildren() {
|
|
203 return false;
|
|
204 }
|
|
205
|
|
206 void aboutToBeAdded(TextEdit parent) {
|
|
207 defineRegion(parent.getOffset());
|
|
208 }
|
|
209
|
|
210 void defineRegion(int parentOffset) {
|
|
211 if (fDefined)
|
|
212 return;
|
|
213 if (hasChildren()) {
|
|
214 IRegion region= getCoverage(getChildren());
|
|
215 internalSetOffset(region.getOffset());
|
|
216 internalSetLength(region.getLength());
|
|
217 } else {
|
|
218 internalSetOffset(parentOffset);
|
|
219 internalSetLength(0);
|
|
220 }
|
|
221 fDefined= true;
|
|
222 }
|
|
223
|
|
224 /*
|
|
225 * @see dwtx.text.edits.TextEdit#internalToString(java.lang.StringBuffer, int)
|
|
226 * @since 3.3
|
|
227 */
|
|
228 void internalToString(StringBuffer buffer, int indent) {
|
|
229 super.internalToString(buffer, indent);
|
|
230 if (! fDefined)
|
|
231 buffer.append(" [undefined]"); //$NON-NLS-1$
|
|
232 }
|
|
233 }
|