129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 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.TextEditProcessor;
|
|
14
|
131
|
15 import dwtx.text.edits.MultiTextEdit; // packageimport
|
|
16 import dwtx.text.edits.CopySourceEdit; // packageimport
|
|
17 import dwtx.text.edits.MoveSourceEdit; // packageimport
|
|
18 import dwtx.text.edits.CopyingRangeMarker; // packageimport
|
|
19 import dwtx.text.edits.ReplaceEdit; // packageimport
|
|
20 import dwtx.text.edits.EditDocument; // packageimport
|
|
21 import dwtx.text.edits.UndoCollector; // packageimport
|
|
22 import dwtx.text.edits.DeleteEdit; // packageimport
|
|
23 import dwtx.text.edits.MoveTargetEdit; // packageimport
|
|
24 import dwtx.text.edits.CopyTargetEdit; // packageimport
|
|
25 import dwtx.text.edits.TextEditCopier; // packageimport
|
|
26 import dwtx.text.edits.ISourceModifier; // packageimport
|
|
27 import dwtx.text.edits.TextEditMessages; // packageimport
|
|
28 import dwtx.text.edits.MalformedTreeException; // packageimport
|
|
29 import dwtx.text.edits.TreeIterationInfo; // packageimport
|
|
30 import dwtx.text.edits.TextEditVisitor; // packageimport
|
|
31 import dwtx.text.edits.TextEditGroup; // packageimport
|
|
32 import dwtx.text.edits.TextEdit; // packageimport
|
|
33 import dwtx.text.edits.RangeMarker; // packageimport
|
|
34 import dwtx.text.edits.UndoEdit; // packageimport
|
|
35 import dwtx.text.edits.InsertEdit; // packageimport
|
|
36
|
|
37
|
129
|
38 import dwt.dwthelper.utils;
|
|
39
|
|
40 import java.util.ArrayList;
|
|
41 import java.util.Iterator;
|
|
42 import java.util.List;
|
|
43
|
|
44 import dwtx.core.runtime.Assert;
|
|
45 import dwtx.jface.text.BadLocationException;
|
|
46 import dwtx.jface.text.IDocument;
|
|
47
|
|
48
|
|
49 /**
|
|
50 * A <code>TextEditProcessor</code> manages a set of edits and applies
|
|
51 * them as a whole to an <code>IDocument</code>.
|
|
52 * <p>
|
|
53 * This class isn't intended to be subclassed.</p>
|
|
54 *
|
|
55 * @see dwtx.text.edits.TextEdit#apply(IDocument)
|
|
56 *
|
|
57 * @since 3.0
|
|
58 * @noextend This class is not intended to be subclassed by clients.
|
|
59 */
|
|
60 public class TextEditProcessor {
|
|
61
|
|
62 private IDocument fDocument;
|
|
63 private TextEdit fRoot;
|
|
64 private int fStyle;
|
|
65
|
|
66 private bool fChecked;
|
|
67 private MalformedTreeException fException;
|
|
68
|
|
69 private List fSourceEdits;
|
|
70
|
|
71 /**
|
|
72 * Constructs a new edit processor for the given
|
|
73 * document.
|
|
74 *
|
|
75 * @param document the document to manipulate
|
|
76 * @param root the root of the text edit tree describing
|
|
77 * the modifications. By passing a text edit a a text edit
|
|
78 * processor the ownership of the edit is transfered to the
|
|
79 * text edit processors. Clients must not modify the edit
|
|
80 * (e.g adding new children) any longer.
|
|
81 *
|
|
82 * @param style {@link TextEdit#NONE}, {@link TextEdit#CREATE_UNDO} or {@link TextEdit#UPDATE_REGIONS})
|
|
83 */
|
130
|
84 public this(IDocument document, TextEdit root, int style) {
|
129
|
85 this(document, root, style, false);
|
|
86 }
|
|
87
|
130
|
88 private this(IDocument document, TextEdit root, int style, bool secondary) {
|
129
|
89 Assert.isNotNull(document);
|
|
90 Assert.isNotNull(root);
|
|
91 fDocument= document;
|
|
92 fRoot= root;
|
|
93 if (fRoot instanceof MultiTextEdit)
|
134
|
94 (cast(MultiTextEdit)fRoot).defineRegion(0);
|
129
|
95 fStyle= style;
|
|
96 if (secondary) {
|
|
97 fChecked= true;
|
|
98 fSourceEdits= new ArrayList();
|
|
99 }
|
|
100 }
|
|
101
|
|
102 /**
|
|
103 * Creates a special internal processor used to during source computation inside
|
|
104 * move source and copy source edits
|
|
105 *
|
|
106 * @param document the document to be manipulated
|
|
107 * @param root the edit tree
|
|
108 * @param style {@link TextEdit#NONE}, {@link TextEdit#CREATE_UNDO} or {@link TextEdit#UPDATE_REGIONS})
|
|
109 * @return a secondary text edit processor
|
|
110 * @since 3.1
|
|
111 */
|
|
112 static TextEditProcessor createSourceComputationProcessor(IDocument document, TextEdit root, int style) {
|
|
113 return new TextEditProcessor(document, root, style, true);
|
|
114 }
|
|
115
|
|
116 /**
|
|
117 * Returns the document to be manipulated.
|
|
118 *
|
|
119 * @return the document
|
|
120 */
|
|
121 public IDocument getDocument() {
|
|
122 return fDocument;
|
|
123 }
|
|
124
|
|
125 /**
|
|
126 * Returns the edit processor's root edit.
|
|
127 *
|
|
128 * @return the processor's root edit
|
|
129 */
|
|
130 public TextEdit getRoot() {
|
|
131 return fRoot;
|
|
132 }
|
|
133
|
|
134 /**
|
|
135 * Returns the style bits of the text edit processor
|
|
136 *
|
|
137 * @return the style bits
|
|
138 * @see TextEdit#CREATE_UNDO
|
|
139 * @see TextEdit#UPDATE_REGIONS
|
|
140 */
|
|
141 public int getStyle() {
|
|
142 return fStyle;
|
|
143 }
|
|
144
|
|
145 /**
|
|
146 * Checks if the processor can execute all its edits.
|
|
147 *
|
|
148 * @return <code>true</code> if the edits can be executed. Return <code>false
|
|
149 * </code>otherwise. One major reason why edits cannot be executed are wrong
|
|
150 * offset or length values of edits. Calling perform in this case will very
|
|
151 * likely end in a <code>BadLocationException</code>.
|
|
152 */
|
|
153 public bool canPerformEdits() {
|
|
154 try {
|
|
155 fRoot.dispatchCheckIntegrity(this);
|
|
156 fChecked= true;
|
|
157 } catch (MalformedTreeException e) {
|
|
158 fException= e;
|
|
159 return false;
|
|
160 }
|
|
161 return true;
|
|
162 }
|
|
163
|
|
164 /**
|
|
165 * Executes the text edits.
|
|
166 *
|
|
167 * @return an object representing the undo of the executed edits
|
|
168 * @exception MalformedTreeException is thrown if the edit tree isn't
|
|
169 * in a valid state. This exception is thrown before any edit is executed.
|
|
170 * So the document is still in its original state.
|
|
171 * @exception BadLocationException is thrown if one of the edits in the
|
|
172 * tree can't be executed. The state of the document is undefined if this
|
|
173 * exception is thrown.
|
|
174 */
|
|
175 public UndoEdit performEdits() throws MalformedTreeException, BadLocationException {
|
|
176 if (!fChecked) {
|
|
177 fRoot.dispatchCheckIntegrity(this);
|
|
178 } else {
|
|
179 if (fException !is null)
|
|
180 throw fException;
|
|
181 }
|
|
182 return fRoot.dispatchPerformEdits(this);
|
|
183 }
|
|
184
|
|
185 /*
|
|
186 * Class isn't intended to be sub-lcassed
|
|
187 */
|
|
188 protected bool considerEdit(TextEdit edit) {
|
|
189 return true;
|
|
190 }
|
|
191
|
|
192 //---- checking --------------------------------------------------------------------
|
|
193
|
|
194 void checkIntegrityDo() throws MalformedTreeException {
|
|
195 fSourceEdits= new ArrayList();
|
|
196 fRoot.traverseConsistencyCheck(this, fDocument, fSourceEdits);
|
|
197 if (fRoot.getExclusiveEnd() > fDocument.getLength())
|
|
198 throw new MalformedTreeException(null, fRoot, TextEditMessages.getString("TextEditProcessor.invalid_length")); //$NON-NLS-1$
|
|
199 }
|
|
200
|
|
201 void checkIntegrityUndo() {
|
|
202 if (fRoot.getExclusiveEnd() > fDocument.getLength())
|
|
203 throw new MalformedTreeException(null, fRoot, TextEditMessages.getString("TextEditProcessor.invalid_length")); //$NON-NLS-1$
|
|
204 }
|
|
205
|
|
206 //---- execution --------------------------------------------------------------------
|
|
207
|
|
208 UndoEdit executeDo() throws BadLocationException {
|
|
209 UndoCollector collector= new UndoCollector(fRoot);
|
|
210 try {
|
|
211 if (createUndo())
|
|
212 collector.connect(fDocument);
|
|
213 computeSources();
|
|
214 fRoot.traverseDocumentUpdating(this, fDocument);
|
|
215 if (updateRegions()) {
|
|
216 fRoot.traverseRegionUpdating(this, fDocument, 0, false);
|
|
217 }
|
|
218 } finally {
|
|
219 collector.disconnect(fDocument);
|
|
220 }
|
|
221 return collector.undo;
|
|
222 }
|
|
223
|
|
224 private void computeSources() {
|
|
225 for (Iterator iter= fSourceEdits.iterator(); iter.hasNext();) {
|
134
|
226 List list= cast(List)iter.next();
|
129
|
227 if (list !is null) {
|
|
228 for (Iterator edits= list.iterator(); edits.hasNext();) {
|
134
|
229 TextEdit edit= cast(TextEdit)edits.next();
|
129
|
230 edit.traverseSourceComputation(this, fDocument);
|
|
231 }
|
|
232 }
|
|
233 }
|
|
234 }
|
|
235
|
|
236 UndoEdit executeUndo() throws BadLocationException {
|
|
237 UndoCollector collector= new UndoCollector(fRoot);
|
|
238 try {
|
|
239 if (createUndo())
|
|
240 collector.connect(fDocument);
|
|
241 TextEdit[] edits= fRoot.getChildren();
|
|
242 for (int i= edits.length - 1; i >= 0; i--) {
|
|
243 edits[i].performDocumentUpdating(fDocument);
|
|
244 }
|
|
245 } finally {
|
|
246 collector.disconnect(fDocument);
|
|
247 }
|
|
248 return collector.undo;
|
|
249 }
|
|
250
|
|
251 private bool createUndo() {
|
|
252 return (fStyle & TextEdit.CREATE_UNDO) !is 0;
|
|
253 }
|
|
254
|
|
255 private bool updateRegions() {
|
|
256 return (fStyle & TextEdit.UPDATE_REGIONS) !is 0;
|
|
257 }
|
|
258 }
|