1 11 package org.eclipse.text.edits; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.jface.text.BadLocationException; 17 import org.eclipse.jface.text.IDocument; 18 19 36 public final class MoveTargetEdit extends TextEdit { 37 38 private MoveSourceEdit fSource; 39 40 45 public MoveTargetEdit(int offset) { 46 super(offset, 0); 47 } 48 49 55 public MoveTargetEdit(int offset, MoveSourceEdit source) { 56 this(offset); 57 setSourceEdit(source); 58 } 59 60 63 private MoveTargetEdit(MoveTargetEdit other) { 64 super(other); 65 } 66 67 73 public MoveSourceEdit getSourceEdit() { 74 return fSource; 75 } 76 77 85 public void setSourceEdit(MoveSourceEdit edit) { 86 if (fSource != edit) { 87 fSource= edit; 88 fSource.setTargetEdit(this); 89 TextEdit parent= getParent(); 90 while (parent != null) { 91 if (parent == fSource) 92 throw new MalformedTreeException(parent, this, TextEditMessages.getString("MoveTargetEdit.wrong_parent")); parent= parent.getParent(); 94 } 95 } 96 } 97 98 101 protected TextEdit doCopy() { 102 return new MoveTargetEdit(this); 103 } 104 105 108 protected void postProcessCopy(TextEditCopier copier) { 109 if (fSource != null) { 110 MoveTargetEdit target= (MoveTargetEdit)copier.getCopy(this); 111 MoveSourceEdit source= (MoveSourceEdit)copier.getCopy(fSource); 112 if (target != null && source != null) 113 target.setSourceEdit(source); 114 } 115 } 116 117 120 protected void accept0(TextEditVisitor visitor) { 121 boolean visitChildren= visitor.visit(this); 122 if (visitChildren) { 123 acceptChildren(visitor); 124 } 125 } 126 127 129 132 int traverseConsistencyCheck(TextEditProcessor processor, IDocument document, List sourceEdits) { 133 return super.traverseConsistencyCheck(processor, document, sourceEdits) + 1; 134 } 135 136 139 void performConsistencyCheck(TextEditProcessor processor, IDocument document) throws MalformedTreeException { 140 if (fSource == null) 141 throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("MoveTargetEdit.no_source")); if (fSource.getTargetEdit() != this) 143 throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("MoveTargetEdit.different_target")); } 145 146 148 151 int performDocumentUpdating(IDocument document) throws BadLocationException { 152 String source= fSource.getContent(); 153 document.replace(getOffset(), getLength(), source); 154 fDelta= source.length() - getLength(); 155 156 MultiTextEdit sourceRoot= fSource.getSourceRoot(); 157 if (sourceRoot != null) { 158 sourceRoot.internalMoveTree(getOffset()); 159 TextEdit[] sourceChildren= sourceRoot.removeChildren(); 160 List children= new ArrayList (sourceChildren.length); 161 for (int i= 0; i < sourceChildren.length; i++) { 162 TextEdit child= sourceChildren[i]; 163 child.internalSetParent(this); 164 children.add(child); 165 } 166 internalSetChildren(children); 167 } 168 fSource.clearContent(); 169 return fDelta; 170 } 171 172 174 177 int traverseRegionUpdating(TextEditProcessor processor, IDocument document, int accumulatedDelta, boolean delete) { 178 if (delete) { 182 deleteTree(); 183 } else { 184 internalMoveTree(accumulatedDelta); 185 } 186 return accumulatedDelta + fDelta; 187 } 188 189 boolean deleteChildren() { 190 return false; 191 } 192 } 193 | Popular Tags |