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