1 11 package org.eclipse.jdt.core.dom.rewrite; 12 13 import java.util.Collections ; 14 import java.util.List ; 15 16 import org.eclipse.jdt.core.dom.ASTNode; 17 import org.eclipse.jdt.core.dom.Block; 18 import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor; 19 import org.eclipse.jdt.core.dom.FieldDeclaration; 20 import org.eclipse.jdt.core.dom.Statement; 21 import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; 22 import org.eclipse.jdt.internal.core.dom.rewrite.ListRewriteEvent; 23 import org.eclipse.jdt.internal.core.dom.rewrite.NodeInfoStore; 24 import org.eclipse.jdt.internal.core.dom.rewrite.RewriteEvent; 25 import org.eclipse.jdt.internal.core.dom.rewrite.RewriteEventStore; 26 import org.eclipse.jdt.internal.core.dom.rewrite.RewriteEventStore.CopySourceInfo; 27 import org.eclipse.text.edits.TextEditGroup; 28 29 37 public final class ListRewrite { 38 39 private ASTNode parent; 40 private StructuralPropertyDescriptor childProperty; 41 private ASTRewrite rewriter; 42 43 44 ListRewrite(ASTRewrite rewriter, ASTNode parent, StructuralPropertyDescriptor childProperty) { 45 this.rewriter= rewriter; 46 this.parent= parent; 47 this.childProperty= childProperty; 48 } 49 50 private RewriteEventStore getRewriteStore() { 51 return this.rewriter.getRewriteEventStore(); 52 } 53 54 private ListRewriteEvent getEvent() { 55 return getRewriteStore().getListEvent(this.parent, this.childProperty, true); 56 } 57 58 65 public ASTNode getParent() { 66 return this.parent; 67 } 68 69 76 public StructuralPropertyDescriptor getLocationInParent() { 77 return this.childProperty; 78 } 79 80 93 public void remove(ASTNode node, TextEditGroup editGroup) { 94 if (node == null) { 95 throw new IllegalArgumentException (); 96 } 97 RewriteEvent event= getEvent().removeEntry(node); 98 if (editGroup != null) { 99 getRewriteStore().setEventEditGroup(event, editGroup); 100 } 101 } 102 103 108 public ASTRewrite getASTRewrite() { 109 return this.rewriter; 110 } 111 112 113 134 public void replace(ASTNode node, ASTNode replacement, TextEditGroup editGroup) { 135 if (node == null) { 136 throw new IllegalArgumentException (); 137 } 138 RewriteEvent event= getEvent().replaceEntry(node, replacement); 139 if (editGroup != null) { 140 getRewriteStore().setEventEditGroup(event, editGroup); 141 } 142 } 143 144 166 public void insertAfter(ASTNode node, ASTNode element, TextEditGroup editGroup) { 167 if (node == null || element == null) { 168 throw new IllegalArgumentException (); 169 } 170 int index= getEvent().getIndex(element, ListRewriteEvent.BOTH); 171 if (index == -1) { 172 throw new IllegalArgumentException ("Node does not exist"); } 174 internalInsertAt(node, index + 1, true, editGroup); 175 } 176 177 199 public void insertBefore(ASTNode node, ASTNode element, TextEditGroup editGroup) { 200 if (node == null || element == null) { 201 throw new IllegalArgumentException (); 202 } 203 int index= getEvent().getIndex(element, ListRewriteEvent.BOTH); 204 if (index == -1) { 205 throw new IllegalArgumentException ("Node does not exist"); } 207 internalInsertAt(node, index, false, editGroup); 208 } 209 210 223 public void insertFirst(ASTNode node, TextEditGroup editGroup) { 224 if (node == null) { 225 throw new IllegalArgumentException (); 226 } 227 internalInsertAt(node, 0, false, editGroup); 228 } 229 230 243 public void insertLast(ASTNode node, TextEditGroup editGroup) { 244 if (node == null) { 245 throw new IllegalArgumentException (); 246 } 247 internalInsertAt(node, -1, true, editGroup); 248 } 249 250 274 public void insertAt(ASTNode node, int index, TextEditGroup editGroup) { 275 if (node == null) { 276 throw new IllegalArgumentException (); 277 } 278 internalInsertAt(node, index, isInsertBoundToPreviousByDefault(node), editGroup); 279 } 280 281 private void internalInsertAt(ASTNode node, int index, boolean boundToPrevious, TextEditGroup editGroup) { 282 RewriteEvent event= getEvent().insert(node, index); 283 if (boundToPrevious) { 284 getRewriteStore().setInsertBoundToPrevious(node); 285 } 286 if (editGroup != null) { 287 getRewriteStore().setEventEditGroup(event, editGroup); 288 } 289 } 290 291 292 private ASTNode createTargetNode(ASTNode first, ASTNode last, boolean isMove, ASTNode replacingNode, TextEditGroup editGroup) { 293 if (first == null || last == null) { 294 throw new IllegalArgumentException (); 295 } 296 297 NodeInfoStore nodeStore= this.rewriter.getNodeStore(); 298 ASTNode placeholder= nodeStore.newPlaceholderNode(first.getNodeType()); if (placeholder == null) { 300 throw new IllegalArgumentException ("Creating a target node is not supported for nodes of type" + first.getClass().getName()); } 302 303 Block internalPlaceHolder= nodeStore.createCollapsePlaceholder(); 304 CopySourceInfo info= getRewriteStore().createRangeCopy(this.parent, this.childProperty, first, last, isMove, internalPlaceHolder, replacingNode, editGroup); 305 nodeStore.markAsCopyTarget(placeholder, info); 306 307 return placeholder; 308 } 309 310 326 public final ASTNode createCopyTarget(ASTNode first, ASTNode last) { 327 if (first == last) { 328 return this.rewriter.createCopyTarget(first); 329 } else { 330 return createTargetNode(first, last, false, null, null); 331 } 332 } 333 334 352 public final ASTNode createMoveTarget(ASTNode first, ASTNode last) { 353 return createMoveTarget(first, last, null, null); 354 } 355 356 380 public final ASTNode createMoveTarget(ASTNode first, ASTNode last, ASTNode replacingNode, TextEditGroup editGroup) { 381 if (first == last) { 382 replace(first, replacingNode, editGroup); 383 return this.rewriter.createMoveTarget(first); 384 } else { 385 return createTargetNode(first, last, true, replacingNode, editGroup); 386 } 387 } 388 389 392 private boolean isInsertBoundToPreviousByDefault(ASTNode node) { 393 return (node instanceof Statement || node instanceof FieldDeclaration); 394 } 395 396 402 public List getOriginalList() { 403 List list= (List ) getEvent().getOriginalValue(); 404 return Collections.unmodifiableList(list); 405 } 406 407 414 public List getRewrittenList() { 415 List list= (List ) getEvent().getNewValue(); 416 return Collections.unmodifiableList(list); 417 } 418 419 } 420 | Popular Tags |