1 11 package org.eclipse.text.edits; 12 13 import java.util.List ; 14 15 import org.eclipse.core.runtime.Assert; 16 17 import org.eclipse.jface.text.BadLocationException; 18 import org.eclipse.jface.text.IDocument; 19 20 36 public final class CopyTargetEdit extends TextEdit { 37 38 private CopySourceEdit fSource; 39 40 45 public CopyTargetEdit(int offset) { 46 super(offset, 0); 47 } 48 49 55 public CopyTargetEdit(int offset, CopySourceEdit source) { 56 this(offset); 57 setSourceEdit(source); 58 } 59 60 63 private CopyTargetEdit(CopyTargetEdit other) { 64 super(other); 65 } 66 67 73 public CopySourceEdit getSourceEdit() { 74 return fSource; 75 } 76 77 85 public void setSourceEdit(CopySourceEdit edit) throws MalformedTreeException { 86 Assert.isNotNull(edit); 87 if (fSource != edit) { 88 fSource= edit; 89 fSource.setTargetEdit(this); 90 TextEdit parent= getParent(); 91 while (parent != null) { 92 if (parent == fSource) 93 throw new MalformedTreeException(parent, this, TextEditMessages.getString("CopyTargetEdit.wrong_parent")); parent= parent.getParent(); 95 } 96 } 97 } 98 99 102 protected TextEdit doCopy() { 103 return new CopyTargetEdit(this); 104 } 105 106 109 protected void postProcessCopy(TextEditCopier copier) { 110 if (fSource != null) { 111 CopyTargetEdit target= (CopyTargetEdit)copier.getCopy(this); 112 CopySourceEdit source= (CopySourceEdit)copier.getCopy(fSource); 113 if (target != null && source != null) 114 target.setSourceEdit(source); 115 } 116 } 117 118 121 protected void accept0(TextEditVisitor visitor) { 122 boolean visitChildren= visitor.visit(this); 123 if (visitChildren) { 124 acceptChildren(visitor); 125 } 126 } 127 128 131 int traverseConsistencyCheck(TextEditProcessor processor, IDocument document, List sourceEdits) { 132 return super.traverseConsistencyCheck(processor, document, sourceEdits) + 1; 133 } 134 135 138 void performConsistencyCheck(TextEditProcessor processor, IDocument document) throws MalformedTreeException { 139 if (fSource == null) 140 throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("CopyTargetEdit.no_source")); if (fSource.getTargetEdit() != this) 142 throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("CopyTargetEdit.different_target")); } 144 145 148 int performDocumentUpdating(IDocument document) throws BadLocationException { 149 String source= fSource.getContent(); 150 document.replace(getOffset(), getLength(), source); 151 fDelta= source.length() - getLength(); 152 fSource.clearContent(); 153 return fDelta; 154 } 155 156 159 boolean deleteChildren() { 160 return false; 161 } 162 } 163 | Popular Tags |