1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.core.resources.IResourceDelta; 16 import org.eclipse.jdt.core.IJavaElement; 17 import org.eclipse.jdt.core.IJavaElementDelta; 18 import org.eclipse.jdt.core.dom.CompilationUnit; 19 20 23 public class JavaElementDelta extends SimpleDelta implements IJavaElementDelta { 24 27 protected IJavaElementDelta[] affectedChildren = EMPTY_DELTA; 28 29 36 protected CompilationUnit ast = null; 37 38 41 protected IJavaElement changedElement; 42 43 46 protected IResourceDelta[] resourceDeltas = null; 47 48 51 protected int resourceDeltasCounter; 52 55 protected IJavaElement movedFromHandle = null; 56 59 protected IJavaElement movedToHandle = null; 60 63 protected static IJavaElementDelta[] EMPTY_DELTA= new IJavaElementDelta[] {}; 64 77 public JavaElementDelta(IJavaElement element) { 78 this.changedElement = element; 79 } 80 84 protected void addAffectedChild(JavaElementDelta child) { 85 switch (this.kind) { 86 case ADDED: 87 case REMOVED: 88 return; 90 case CHANGED: 91 this.changeFlags |= F_CHILDREN; 92 break; 93 default: 94 this.kind = CHANGED; 95 this.changeFlags |= F_CHILDREN; 96 } 97 98 if (this.changedElement.getElementType() >= IJavaElement.COMPILATION_UNIT) { 101 this.fineGrained(); 102 } 103 104 if (this.affectedChildren.length == 0) { 105 this.affectedChildren = new IJavaElementDelta[] {child}; 106 return; 107 } 108 JavaElementDelta existingChild = null; 109 int existingChildIndex = -1; 110 if (this.affectedChildren != null) { 111 for (int i = 0; i < this.affectedChildren.length; i++) { 112 if (this.equalsAndSameParent(this.affectedChildren[i].getElement(), child.getElement())) { existingChild = (JavaElementDelta)this.affectedChildren[i]; 114 existingChildIndex = i; 115 break; 116 } 117 } 118 } 119 if (existingChild == null) { this.affectedChildren= growAndAddToArray(this.affectedChildren, child); 121 } else { 122 switch (existingChild.getKind()) { 123 case ADDED: 124 switch (child.getKind()) { 125 case ADDED: case CHANGED: return; 128 case REMOVED: this.affectedChildren = this.removeAndShrinkArray(this.affectedChildren, existingChildIndex); 130 return; 131 } 132 break; 133 case REMOVED: 134 switch (child.getKind()) { 135 case ADDED: child.kind = CHANGED; 137 this.affectedChildren[existingChildIndex] = child; 138 return; 139 case CHANGED: case REMOVED: return; 142 } 143 break; 144 case CHANGED: 145 switch (child.getKind()) { 146 case ADDED: case REMOVED: this.affectedChildren[existingChildIndex] = child; 149 return; 150 case CHANGED: IJavaElementDelta[] children = child.getAffectedChildren(); 152 for (int i = 0; i < children.length; i++) { 153 JavaElementDelta childsChild = (JavaElementDelta) children[i]; 154 existingChild.addAffectedChild(childsChild); 155 } 156 157 boolean childHadContentFlag = (child.changeFlags & F_CONTENT) != 0; 159 boolean existingChildHadChildrenFlag = (existingChild.changeFlags & F_CHILDREN) != 0; 160 existingChild.changeFlags |= child.changeFlags; 161 162 if (childHadContentFlag && existingChildHadChildrenFlag) { 166 existingChild.changeFlags &= ~F_CONTENT; 167 } 168 169 IResourceDelta[] resDeltas = child.getResourceDeltas(); 173 if (resDeltas != null) { 174 existingChild.resourceDeltas = resDeltas; 175 existingChild.resourceDeltasCounter = child.resourceDeltasCounter; 176 } 177 178 return; 179 } 180 break; 181 default: 182 int flags = existingChild.getFlags(); 184 this.affectedChildren[existingChildIndex] = child; 185 child.changeFlags |= flags; 186 } 187 } 188 } 189 195 public void added(IJavaElement element) { 196 added(element, 0); 197 } 198 public void added(IJavaElement element, int flags) { 199 JavaElementDelta addedDelta = new JavaElementDelta(element); 200 addedDelta.added(); 201 addedDelta.changeFlags |= flags; 202 insertDeltaTree(element, addedDelta); 203 } 204 208 protected void addResourceDelta(IResourceDelta child) { 209 switch (this.kind) { 210 case ADDED: 211 case REMOVED: 212 return; 214 case CHANGED: 215 this.changeFlags |= F_CONTENT; 216 break; 217 default: 218 this.kind = CHANGED; 219 this.changeFlags |= F_CONTENT; 220 } 221 if (resourceDeltas == null) { 222 resourceDeltas = new IResourceDelta[5]; 223 resourceDeltas[resourceDeltasCounter++] = child; 224 return; 225 } 226 if (resourceDeltas.length == resourceDeltasCounter) { 227 System.arraycopy(resourceDeltas, 0, (resourceDeltas = new IResourceDelta[resourceDeltasCounter * 2]), 0, resourceDeltasCounter); 229 } 230 resourceDeltas[resourceDeltasCounter++] = child; 231 } 232 238 public JavaElementDelta changed(IJavaElement element, int changeFlag) { 239 JavaElementDelta changedDelta = new JavaElementDelta(element); 240 changedDelta.changed(changeFlag); 241 insertDeltaTree(element, changedDelta); 242 return changedDelta; 243 } 244 247 public void changedAST(CompilationUnit changedAST) { 248 this.ast = changedAST; 249 changed(F_AST_AFFECTED); 250 } 251 254 public void contentChanged() { 255 this.changeFlags |= F_CONTENT; 256 } 257 260 public void closed(IJavaElement element) { 261 JavaElementDelta delta = new JavaElementDelta(element); 262 delta.changed(F_CLOSED); 263 insertDeltaTree(element, delta); 264 } 265 270 protected JavaElementDelta createDeltaTree(IJavaElement element, JavaElementDelta delta) { 271 JavaElementDelta childDelta = delta; 272 ArrayList ancestors= getAncestors(element); 273 if (ancestors == null) { 274 if (this.equalsAndSameParent(delta.getElement(), getElement())) { this.kind= delta.kind; 277 this.changeFlags = delta.changeFlags; 278 this.movedToHandle = delta.movedToHandle; 279 this.movedFromHandle = delta.movedFromHandle; 280 } 281 } else { 282 for (int i = 0, size = ancestors.size(); i < size; i++) { 283 IJavaElement ancestor = (IJavaElement) ancestors.get(i); 284 JavaElementDelta ancestorDelta = new JavaElementDelta(ancestor); 285 ancestorDelta.addAffectedChild(childDelta); 286 childDelta = ancestorDelta; 287 } 288 } 289 return childDelta; 290 } 291 294 protected boolean equalsAndSameParent(IJavaElement e1, IJavaElement e2) { 295 IJavaElement parent1; 296 return e1.equals(e2) && ((parent1 = e1.getParent()) != null) && parent1.equals(e2.getParent()); 297 } 298 302 protected JavaElementDelta find(IJavaElement e) { 303 if (this.equalsAndSameParent(this.changedElement, e)) { return this; 305 } else { 306 for (int i = 0; i < this.affectedChildren.length; i++) { 307 JavaElementDelta delta = ((JavaElementDelta)this.affectedChildren[i]).find(e); 308 if (delta != null) { 309 return delta; 310 } 311 } 312 } 313 return null; 314 } 315 318 public void fineGrained() { 319 changed(F_FINE_GRAINED); 320 } 321 324 public IJavaElementDelta[] getAddedChildren() { 325 return getChildrenOfType(ADDED); 326 } 327 330 public IJavaElementDelta[] getAffectedChildren() { 331 return this.affectedChildren; 332 } 333 339 private ArrayList getAncestors(IJavaElement element) { 340 IJavaElement parent = element.getParent(); 341 if (parent == null) { 342 return null; 343 } 344 ArrayList parents = new ArrayList (); 345 while (!parent.equals(this.changedElement)) { 346 parents.add(parent); 347 parent = parent.getParent(); 348 if (parent == null) { 349 return null; 350 } 351 } 352 parents.trimToSize(); 353 return parents; 354 } 355 public CompilationUnit getCompilationUnitAST() { 356 return this.ast; 357 } 358 361 public IJavaElementDelta[] getChangedChildren() { 362 return getChildrenOfType(CHANGED); 363 } 364 367 protected IJavaElementDelta[] getChildrenOfType(int type) { 368 int length = this.affectedChildren.length; 369 if (length == 0) { 370 return new IJavaElementDelta[] {}; 371 } 372 ArrayList children= new ArrayList (length); 373 for (int i = 0; i < length; i++) { 374 if (this.affectedChildren[i].getKind() == type) { 375 children.add(this.affectedChildren[i]); 376 } 377 } 378 379 IJavaElementDelta[] childrenOfType = new IJavaElementDelta[children.size()]; 380 children.toArray(childrenOfType); 381 382 return childrenOfType; 383 } 384 388 protected JavaElementDelta getDeltaFor(IJavaElement element) { 389 if (this.equalsAndSameParent(getElement(), element)) return this; 391 if (this.affectedChildren.length == 0) 392 return null; 393 int childrenCount = this.affectedChildren.length; 394 for (int i = 0; i < childrenCount; i++) { 395 JavaElementDelta delta = (JavaElementDelta)this.affectedChildren[i]; 396 if (this.equalsAndSameParent(delta.getElement(), element)) { return delta; 398 } else { 399 delta = delta.getDeltaFor(element); 400 if (delta != null) 401 return delta; 402 } 403 } 404 return null; 405 } 406 409 public IJavaElement getElement() { 410 return this.changedElement; 411 } 412 415 public IJavaElement getMovedFromElement() { 416 return this.movedFromHandle; 417 } 418 421 public IJavaElement getMovedToElement() { 422 return movedToHandle; 423 } 424 427 public IJavaElementDelta[] getRemovedChildren() { 428 return getChildrenOfType(REMOVED); 429 } 430 433 public IResourceDelta[] getResourceDeltas() { 434 if (resourceDeltas == null) return null; 435 if (resourceDeltas.length != resourceDeltasCounter) { 436 System.arraycopy(resourceDeltas, 0, resourceDeltas = new IResourceDelta[resourceDeltasCounter], 0, resourceDeltasCounter); 437 } 438 return resourceDeltas; 439 } 440 444 protected IJavaElementDelta[] growAndAddToArray(IJavaElementDelta[] array, IJavaElementDelta addition) { 445 IJavaElementDelta[] old = array; 446 array = new IJavaElementDelta[old.length + 1]; 447 System.arraycopy(old, 0, array, 0, old.length); 448 array[old.length] = addition; 449 return array; 450 } 451 455 protected void insertDeltaTree(IJavaElement element, JavaElementDelta delta) { 456 JavaElementDelta childDelta= createDeltaTree(element, delta); 457 if (!this.equalsAndSameParent(element, getElement())) { addAffectedChild(childDelta); 459 } 460 } 461 467 public void movedFrom(IJavaElement movedFromElement, IJavaElement movedToElement) { 468 JavaElementDelta removedDelta = new JavaElementDelta(movedFromElement); 469 removedDelta.kind = REMOVED; 470 removedDelta.changeFlags |= F_MOVED_TO; 471 removedDelta.movedToHandle = movedToElement; 472 insertDeltaTree(movedFromElement, removedDelta); 473 } 474 480 public void movedTo(IJavaElement movedToElement, IJavaElement movedFromElement) { 481 JavaElementDelta addedDelta = new JavaElementDelta(movedToElement); 482 addedDelta.kind = ADDED; 483 addedDelta.changeFlags |= F_MOVED_FROM; 484 addedDelta.movedFromHandle = movedFromElement; 485 insertDeltaTree(movedToElement, addedDelta); 486 } 487 490 public void opened(IJavaElement element) { 491 JavaElementDelta delta = new JavaElementDelta(element); 492 delta.changed(F_OPENED); 493 insertDeltaTree(element, delta); 494 } 495 498 protected void removeAffectedChild(JavaElementDelta child) { 499 int index = -1; 500 if (this.affectedChildren != null) { 501 for (int i = 0; i < this.affectedChildren.length; i++) { 502 if (this.equalsAndSameParent(this.affectedChildren[i].getElement(), child.getElement())) { index = i; 504 break; 505 } 506 } 507 } 508 if (index >= 0) { 509 this.affectedChildren= removeAndShrinkArray(this.affectedChildren, index); 510 } 511 } 512 516 protected IJavaElementDelta[] removeAndShrinkArray(IJavaElementDelta[] old, int index) { 517 IJavaElementDelta[] array = new IJavaElementDelta[old.length - 1]; 518 if (index > 0) 519 System.arraycopy(old, 0, array, 0, index); 520 int rest = old.length - index - 1; 521 if (rest > 0) 522 System.arraycopy(old, index + 1, array, index, rest); 523 return array; 524 } 525 531 public void removed(IJavaElement element) { 532 removed(element, 0); 533 } 534 public void removed(IJavaElement element, int flags) { 535 JavaElementDelta removedDelta= new JavaElementDelta(element); 536 insertDeltaTree(element, removedDelta); 537 JavaElementDelta actualDelta = getDeltaFor(element); 538 if (actualDelta != null) { 539 actualDelta.removed(); 540 actualDelta.changeFlags |= flags; 541 actualDelta.affectedChildren = EMPTY_DELTA; 542 } 543 } 544 550 public void sourceAttached(IJavaElement element) { 551 JavaElementDelta attachedDelta = new JavaElementDelta(element); 552 attachedDelta.changed(F_SOURCEATTACHED); 553 insertDeltaTree(element, attachedDelta); 554 } 555 561 public void sourceDetached(IJavaElement element) { 562 JavaElementDelta detachedDelta = new JavaElementDelta(element); 563 detachedDelta.changed(F_SOURCEDETACHED); 564 insertDeltaTree(element, detachedDelta); 565 } 566 572 public String toDebugString(int depth) { 573 StringBuffer buffer = new StringBuffer (); 574 for (int i= 0; i < depth; i++) { 575 buffer.append('\t'); 576 } 577 buffer.append(((JavaElement)getElement()).toDebugString()); 578 toDebugString(buffer); 579 IJavaElementDelta[] children = getAffectedChildren(); 580 if (children != null) { 581 for (int i = 0; i < children.length; ++i) { 582 buffer.append("\n"); buffer.append(((JavaElementDelta) children[i]).toDebugString(depth + 1)); 584 } 585 } 586 for (int i = 0; i < resourceDeltasCounter; i++) { 587 buffer.append("\n"); for (int j = 0; j < depth+1; j++) { 589 buffer.append('\t'); 590 } 591 IResourceDelta resourceDelta = resourceDeltas[i]; 592 buffer.append(resourceDelta.toString()); 593 buffer.append("["); switch (resourceDelta.getKind()) { 595 case IResourceDelta.ADDED : 596 buffer.append('+'); 597 break; 598 case IResourceDelta.REMOVED : 599 buffer.append('-'); 600 break; 601 case IResourceDelta.CHANGED : 602 buffer.append('*'); 603 break; 604 default : 605 buffer.append('?'); 606 break; 607 } 608 buffer.append("]"); } 610 return buffer.toString(); 611 } 612 protected boolean toDebugString(StringBuffer buffer, int flags) { 613 boolean prev = super.toDebugString(buffer, flags); 614 615 if ((flags & IJavaElementDelta.F_CHILDREN) != 0) { 616 if (prev) 617 buffer.append(" | "); buffer.append("CHILDREN"); prev = true; 620 } 621 if ((flags & IJavaElementDelta.F_CONTENT) != 0) { 622 if (prev) 623 buffer.append(" | "); buffer.append("CONTENT"); prev = true; 626 } 627 if ((flags & IJavaElementDelta.F_MOVED_FROM) != 0) { 628 if (prev) 629 buffer.append(" | "); buffer.append("MOVED_FROM(" + ((JavaElement)getMovedFromElement()).toStringWithAncestors() + ")"); prev = true; 632 } 633 if ((flags & IJavaElementDelta.F_MOVED_TO) != 0) { 634 if (prev) 635 buffer.append(" | "); buffer.append("MOVED_TO(" + ((JavaElement)getMovedToElement()).toStringWithAncestors() + ")"); prev = true; 638 } 639 if ((flags & IJavaElementDelta.F_ADDED_TO_CLASSPATH) != 0) { 640 if (prev) 641 buffer.append(" | "); buffer.append("ADDED TO CLASSPATH"); prev = true; 644 } 645 if ((flags & IJavaElementDelta.F_REMOVED_FROM_CLASSPATH) != 0) { 646 if (prev) 647 buffer.append(" | "); buffer.append("REMOVED FROM CLASSPATH"); prev = true; 650 } 651 if ((flags & IJavaElementDelta.F_REORDER) != 0) { 652 if (prev) 653 buffer.append(" | "); buffer.append("REORDERED"); prev = true; 656 } 657 if ((flags & IJavaElementDelta.F_ARCHIVE_CONTENT_CHANGED) != 0) { 658 if (prev) 659 buffer.append(" | "); buffer.append("ARCHIVE CONTENT CHANGED"); prev = true; 662 } 663 if ((flags & IJavaElementDelta.F_SOURCEATTACHED) != 0) { 664 if (prev) 665 buffer.append(" | "); buffer.append("SOURCE ATTACHED"); prev = true; 668 } 669 if ((flags & IJavaElementDelta.F_SOURCEDETACHED) != 0) { 670 if (prev) 671 buffer.append(" | "); buffer.append("SOURCE DETACHED"); prev = true; 674 } 675 if ((flags & IJavaElementDelta.F_FINE_GRAINED) != 0) { 676 if (prev) 677 buffer.append(" | "); buffer.append("FINE GRAINED"); prev = true; 680 } 681 if ((flags & IJavaElementDelta.F_PRIMARY_WORKING_COPY) != 0) { 682 if (prev) 683 buffer.append(" | "); buffer.append("PRIMARY WORKING COPY"); prev = true; 686 } 687 if ((flags & IJavaElementDelta.F_CLASSPATH_CHANGED) != 0) { 688 if (prev) 689 buffer.append(" | "); buffer.append("CLASSPATH CHANGED"); prev = true; 692 } 693 if ((flags & IJavaElementDelta.F_PRIMARY_RESOURCE) != 0) { 694 if (prev) 695 buffer.append(" | "); buffer.append("PRIMARY RESOURCE"); prev = true; 698 } 699 if ((flags & IJavaElementDelta.F_OPENED) != 0) { 700 if (prev) 701 buffer.append(" | "); buffer.append("OPENED"); prev = true; 704 } 705 if ((flags & IJavaElementDelta.F_CLOSED) != 0) { 706 if (prev) 707 buffer.append(" | "); buffer.append("CLOSED"); prev = true; 710 } 711 if ((flags & IJavaElementDelta.F_AST_AFFECTED) != 0) { 712 if (prev) 713 buffer.append(" | "); buffer.append("AST AFFECTED"); prev = true; 716 } 717 if ((flags & IJavaElementDelta.F_CATEGORIES) != 0) { 718 if (prev) 719 buffer.append(" | "); buffer.append("CATEGORIES"); prev = true; 722 } 723 return prev; 724 } 725 729 public String toString() { 730 return toDebugString(0); 731 } 732 } 733 | Popular Tags |