1 11 package org.eclipse.text.edits; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.Assert; 18 19 import org.eclipse.jface.text.BadLocationException; 20 import org.eclipse.jface.text.IDocument; 21 22 32 public class TextEditProcessor { 33 34 private IDocument fDocument; 35 private TextEdit fRoot; 36 private int fStyle; 37 38 private boolean fChecked; 39 private MalformedTreeException fException; 40 41 private List fSourceEdits; 42 43 56 public TextEditProcessor(IDocument document, TextEdit root, int style) { 57 this(document, root, style, false); 58 } 59 60 private TextEditProcessor(IDocument document, TextEdit root, int style, boolean secondary) { 61 Assert.isNotNull(document); 62 Assert.isNotNull(root); 63 fDocument= document; 64 fRoot= root; 65 if (fRoot instanceof MultiTextEdit) 66 ((MultiTextEdit)fRoot).defineRegion(0); 67 fStyle= style; 68 if (secondary) { 69 fChecked= true; 70 fSourceEdits= new ArrayList (); 71 } 72 } 73 74 84 static TextEditProcessor createSourceComputationProcessor(IDocument document, TextEdit root, int style) { 85 return new TextEditProcessor(document, root, style, true); 86 } 87 88 93 public IDocument getDocument() { 94 return fDocument; 95 } 96 97 102 public TextEdit getRoot() { 103 return fRoot; 104 } 105 106 113 public int getStyle() { 114 return fStyle; 115 } 116 117 125 public boolean canPerformEdits() { 126 try { 127 fRoot.dispatchCheckIntegrity(this); 128 fChecked= true; 129 } catch (MalformedTreeException e) { 130 fException= e; 131 return false; 132 } 133 return true; 134 } 135 136 147 public UndoEdit performEdits() throws MalformedTreeException, BadLocationException { 148 if (!fChecked) { 149 fRoot.dispatchCheckIntegrity(this); 150 } else { 151 if (fException != null) 152 throw fException; 153 } 154 return fRoot.dispatchPerformEdits(this); 155 } 156 157 160 protected boolean considerEdit(TextEdit edit) { 161 return true; 162 } 163 164 166 void checkIntegrityDo() throws MalformedTreeException { 167 fSourceEdits= new ArrayList (); 168 fRoot.traverseConsistencyCheck(this, fDocument, fSourceEdits); 169 if (fRoot.getExclusiveEnd() > fDocument.getLength()) 170 throw new MalformedTreeException(null, fRoot, TextEditMessages.getString("TextEditProcessor.invalid_length")); } 172 173 void checkIntegrityUndo() { 174 if (fRoot.getExclusiveEnd() > fDocument.getLength()) 175 throw new MalformedTreeException(null, fRoot, TextEditMessages.getString("TextEditProcessor.invalid_length")); } 177 178 180 UndoEdit executeDo() throws BadLocationException { 181 UndoCollector collector= new UndoCollector(fRoot); 182 try { 183 if (createUndo()) 184 collector.connect(fDocument); 185 computeSources(); 186 fRoot.traverseDocumentUpdating(this, fDocument); 187 if (updateRegions()) { 188 fRoot.traverseRegionUpdating(this, fDocument, 0, false); 189 } 190 } finally { 191 collector.disconnect(fDocument); 192 } 193 return collector.undo; 194 } 195 196 private void computeSources() { 197 for (Iterator iter= fSourceEdits.iterator(); iter.hasNext();) { 198 List list= (List )iter.next(); 199 if (list != null) { 200 for (Iterator edits= list.iterator(); edits.hasNext();) { 201 TextEdit edit= (TextEdit)edits.next(); 202 edit.traverseSourceComputation(this, fDocument); 203 } 204 } 205 } 206 } 207 208 UndoEdit executeUndo() throws BadLocationException { 209 UndoCollector collector= new UndoCollector(fRoot); 210 try { 211 if (createUndo()) 212 collector.connect(fDocument); 213 TextEdit[] edits= fRoot.getChildren(); 214 for (int i= edits.length - 1; i >= 0; i--) { 215 edits[i].performDocumentUpdating(fDocument); 216 } 217 } finally { 218 collector.disconnect(fDocument); 219 } 220 return collector.undo; 221 } 222 223 private boolean createUndo() { 224 return (fStyle & TextEdit.CREATE_UNDO) != 0; 225 } 226 227 private boolean updateRegions() { 228 return (fStyle & TextEdit.UPDATE_REGIONS) != 0; 229 } 230 } 231 | Popular Tags |