1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
13 import java.util.ArrayList;
15 import net.sourceforge.phpdt.core.IJavaElement;
16 import net.sourceforge.phpdt.core.IJavaElementDelta;
18 import org.eclipse.core.resources.IResourceDelta;
22 * @see IJavaElementDelta
24 public class JavaElementDelta extends SimpleDelta implements IJavaElementDelta {
26 * The element that this delta describes the change to.
29 protected IJavaElement fChangedElement;
33 private int fKind = 0;
37 private int fChangeFlags = 0;
39 * @see #getAffectedChildren()
41 protected IJavaElementDelta[] fAffectedChildren = fgEmptyDelta;
44 * Collection of resource deltas that correspond to non java resources deltas.
46 protected IResourceDelta[] resourceDeltas = null;
49 * Counter of resource deltas
51 protected int resourceDeltasCounter;
53 * @see #getMovedFromHandle()
55 protected IJavaElement fMovedFromHandle = null;
57 * @see #getMovedToHandle()
59 protected IJavaElement fMovedToHandle = null;
61 * Empty array of IJavaElementDelta
63 protected static IJavaElementDelta[] fgEmptyDelta= new IJavaElementDelta[] {};
65 * Creates the root delta. To create the nested delta
66 * hierarchies use the following convenience methods. The root
67 * delta can be created at any level (for example: project, package root,
68 * package fragment...).
70 * <li><code>added(IJavaElement)</code>
71 * <li><code>changed(IJavaElement)</code>
72 * <li><code>moved(IJavaElement, IJavaElement)</code>
73 * <li><code>removed(IJavaElement)</code>
74 * <li><code>renamed(IJavaElement, IJavaElement)</code>
77 public JavaElementDelta(IJavaElement element) {
79 fChangedElement = element;
82 * Adds the child delta to the collection of affected children. If the
83 * child is already in the collection, walk down the hierarchy.
85 protected void addAffectedChild(JavaElementDelta child) {
89 // no need to add a child if this parent is added or removed
92 fChangeFlags |= F_CHILDREN;
96 fChangeFlags |= F_CHILDREN;
99 // if a child delta is added to a compilation unit delta or below,
100 // it's a fine grained delta
101 if (fChangedElement.getElementType() >= IJavaElement.COMPILATION_UNIT) {
105 if (fAffectedChildren.length == 0) {
106 fAffectedChildren = new IJavaElementDelta[] {child};
109 IJavaElementDelta existingChild = null;
110 int existingChildIndex = -1;
111 if (fAffectedChildren != null) {
112 for (int i = 0; i < fAffectedChildren.length; i++) {
113 if (this.equalsAndSameParent(fAffectedChildren[i].getElement(), child.getElement())) { // handle case of two jars that can be equals but not in the same project
114 existingChild = fAffectedChildren[i];
115 existingChildIndex = i;
120 if (existingChild == null) { //new affected child
121 fAffectedChildren= growAndAddToArray(fAffectedChildren, child);
123 switch (existingChild.getKind()) {
125 switch (child.getKind()) {
126 case ADDED: // child was added then added -> it is added
127 case CHANGED: // child was added then changed -> it is added
129 case REMOVED: // child was added then removed -> noop
130 fAffectedChildren = this.removeAndShrinkArray(fAffectedChildren, existingChildIndex);
135 switch (child.getKind()) {
136 case ADDED: // child was removed then added -> it is changed
137 child.fKind = CHANGED;
138 fAffectedChildren[existingChildIndex] = child;
140 case CHANGED: // child was removed then changed -> it is removed
141 case REMOVED: // child was removed then removed -> it is removed
146 switch (child.getKind()) {
147 case ADDED: // child was changed then added -> it is added
148 case REMOVED: // child was changed then removed -> it is removed
149 fAffectedChildren[existingChildIndex] = child;
151 case CHANGED: // child was changed then changed -> it is changed
152 IJavaElementDelta[] children = child.getAffectedChildren();
153 for (int i = 0; i < children.length; i++) {
154 JavaElementDelta childsChild = (JavaElementDelta) children[i];
155 ((JavaElementDelta) existingChild).addAffectedChild(childsChild);
158 // update flags if needed
159 switch (((JavaElementDelta) existingChild).fChangeFlags) {
160 case F_ADDED_TO_CLASSPATH:
161 case F_REMOVED_FROM_CLASSPATH:
162 case F_SOURCEATTACHED:
163 case F_SOURCEDETACHED:
164 ((JavaElementDelta) existingChild).fChangeFlags |= ((JavaElementDelta) child).fChangeFlags;
168 // add the non-java resource deltas if needed
169 // note that the child delta always takes precedence over this existing child delta
170 // as non-java resource deltas are always created last (by the DeltaProcessor)
171 IResourceDelta[] resDeltas = child.getResourceDeltas();
172 if (resDeltas != null) {
173 ((JavaElementDelta)existingChild).resourceDeltas = resDeltas;
174 ((JavaElementDelta)existingChild).resourceDeltasCounter = child.resourceDeltasCounter;
180 // unknown -> existing child becomes the child with the existing child's flags
181 int flags = existingChild.getFlags();
182 fAffectedChildren[existingChildIndex] = child;
183 child.fChangeFlags |= flags;
188 // * Creates the nested deltas resulting from an add operation.
189 // * Convenience method for creating add deltas.
190 // * The constructor should be used to create the root delta
191 // * and then an add operation should call this method.
193 //public void added(IJavaElement element) {
194 // JavaElementDelta addedDelta = new JavaElementDelta(element);
195 // addedDelta.fKind = ADDED;
196 // insertDeltaTree(element, addedDelta);
199 * Creates the nested deltas resulting from an add operation.
200 * Convenience method for creating add deltas.
201 * The constructor should be used to create the root delta
202 * and then an add operation should call this method.
204 public void added(IJavaElement element) {
207 public void added(IJavaElement element, int flags) {
208 JavaElementDelta addedDelta = new JavaElementDelta(element);
210 addedDelta.changeFlags |= flags;
211 insertDeltaTree(element, addedDelta);
214 * Adds the child delta to the collection of affected children. If the
215 * child is already in the collection, walk down the hierarchy.
217 protected void addResourceDelta(IResourceDelta child) {
221 // no need to add a child if this parent is added or removed
224 fChangeFlags |= F_CONTENT;
228 fChangeFlags |= F_CONTENT;
230 if (resourceDeltas == null) {
231 resourceDeltas = new IResourceDelta[5];
232 resourceDeltas[resourceDeltasCounter++] = child;
235 if (resourceDeltas.length == resourceDeltasCounter) {
237 System.arraycopy(resourceDeltas, 0, (resourceDeltas = new IResourceDelta[resourceDeltasCounter * 2]), 0, resourceDeltasCounter);
239 resourceDeltas[resourceDeltasCounter++] = child;
243 * Creates the nested deltas resulting from a change operation.
244 * Convenience method for creating change deltas.
245 * The constructor should be used to create the root delta
246 * and then a change operation should call this method.
248 public JavaElementDelta changed(IJavaElement element, int changeFlag) {
249 JavaElementDelta changedDelta = new JavaElementDelta(element);
250 changedDelta.changed(changeFlag);
251 insertDeltaTree(element, changedDelta);
255 * Mark this delta as a content changed delta.
257 public void contentChanged() {
258 fChangeFlags |= F_CONTENT;
261 // * Clone this delta so that its elements are rooted at the given project.
263 //public IJavaElementDelta clone(IJavaProject project) {
264 // JavaElementDelta clone =
265 // new JavaElementDelta(((JavaElement)fChangedElement).rootedAt(project));
266 // if (fAffectedChildren != fgEmptyDelta) {
267 // int length = fAffectedChildren.length;
268 // IJavaElementDelta[] cloneChildren = new IJavaElementDelta[length];
269 // for (int i= 0; i < length; i++) {
270 // cloneChildren[i] = ((JavaElementDelta)fAffectedChildren[i]).clone(project);
272 // clone.fAffectedChildren = cloneChildren;
274 // clone.fChangeFlags = fChangeFlags;
275 // clone.fKind = fKind;
276 // if (fMovedFromHandle != null) {
277 // clone.fMovedFromHandle = ((JavaElement)fMovedFromHandle).rootedAt(project);
279 // if (fMovedToHandle != null) {
280 // clone.fMovedToHandle = ((JavaElement)fMovedToHandle).rootedAt(project);
282 // clone.resourceDeltas = this.resourceDeltas;
283 // clone.resourceDeltasCounter = this.resourceDeltasCounter;
288 * Creates the nested deltas for a closed element.
290 public void closed(IJavaElement element) {
291 JavaElementDelta delta = new JavaElementDelta(element);
292 delta.fKind = CHANGED;
293 delta.fChangeFlags |= F_CLOSED;
294 insertDeltaTree(element, delta);
297 * Creates the nested delta deltas based on the affected element
298 * its delta, and the root of this delta tree. Returns the root
299 * of the created delta tree.
301 protected JavaElementDelta createDeltaTree(IJavaElement element, JavaElementDelta delta) {
302 JavaElementDelta childDelta = delta;
303 ArrayList ancestors= getAncestors(element);
304 if (ancestors == null) {
305 if (this.equalsAndSameParent(delta.getElement(), getElement())) { // handle case of two jars that can be equals but not in the same project
306 // the element being changed is the root element
308 fChangeFlags = delta.fChangeFlags;
309 fMovedToHandle = delta.fMovedToHandle;
310 fMovedFromHandle = delta.fMovedFromHandle;
313 for (int i = 0, size = ancestors.size(); i < size; i++) {
314 IJavaElement ancestor = (IJavaElement) ancestors.get(i);
315 JavaElementDelta ancestorDelta = new JavaElementDelta(ancestor);
316 ancestorDelta.addAffectedChild(childDelta);
317 childDelta = ancestorDelta;
323 * Returns whether the two java elements are equals and have the same parent.
325 protected boolean equalsAndSameParent(IJavaElement e1, IJavaElement e2) {
326 IJavaElement parent1;
327 return e1.equals(e2) && ((parent1 = e1.getParent()) != null) && parent1.equals(e2.getParent());
330 * Returns the <code>JavaElementDelta</code> for the given element
331 * in the delta tree, or null, if no delta for the given element is found.
333 protected JavaElementDelta find(IJavaElement e) {
334 if (this.equalsAndSameParent(fChangedElement, e)) { // handle case of two jars that can be equals but not in the same project
337 for (int i = 0; i < fAffectedChildren.length; i++) {
338 JavaElementDelta delta = ((JavaElementDelta)fAffectedChildren[i]).find(e);
347 * Mark this delta as a fine-grained delta.
349 public void fineGrained() {
350 if (fKind == 0) { // if not set yet
353 fChangeFlags |= F_FINE_GRAINED;
356 * @see IJavaElementDelta
358 public IJavaElementDelta[] getAddedChildren() {
359 return getChildrenOfType(ADDED);
362 * @see IJavaElementDelta
364 public IJavaElementDelta[] getAffectedChildren() {
365 return fAffectedChildren;
368 * Returns a collection of all the parents of this element up to (but
369 * not including) the root of this tree in bottom-up order. If the given
370 * element is not a descendant of the root of this tree, <code>null</code>
373 private ArrayList getAncestors(IJavaElement element) {
374 IJavaElement parent = element.getParent();
375 if (parent == null) {
378 ArrayList parents = new ArrayList();
379 while (!parent.equals(fChangedElement)) {
381 parent = parent.getParent();
382 if (parent == null) {
386 parents.trimToSize();
390 * @see IJavaElementDelta
392 public IJavaElementDelta[] getChangedChildren() {
393 return getChildrenOfType(CHANGED);
396 * @see IJavaElementDelta
398 protected IJavaElementDelta[] getChildrenOfType(int type) {
399 int length = fAffectedChildren.length;
401 return new IJavaElementDelta[] {};
403 ArrayList children= new ArrayList(length);
404 for (int i = 0; i < length; i++) {
405 if (fAffectedChildren[i].getKind() == type) {
406 children.add(fAffectedChildren[i]);
410 IJavaElementDelta[] childrenOfType = new IJavaElementDelta[children.size()];
411 children.toArray(childrenOfType);
413 return childrenOfType;
416 * Returns the delta for a given element. Only looks below this
419 protected JavaElementDelta getDeltaFor(IJavaElement element) {
420 if (this.equalsAndSameParent(getElement(), element)) // handle case of two jars that can be equals but not in the same project
422 if (fAffectedChildren.length == 0)
424 int childrenCount = fAffectedChildren.length;
425 for (int i = 0; i < childrenCount; i++) {
426 JavaElementDelta delta = (JavaElementDelta)fAffectedChildren[i];
427 if (this.equalsAndSameParent(delta.getElement(), element)) { // handle case of two jars that can be equals but not in the same project
430 delta = ((JavaElementDelta)delta).getDeltaFor(element);
438 * @see IJavaElementDelta
440 public IJavaElement getElement() {
441 return fChangedElement;
444 * @see IJavaElementDelta
446 public int getFlags() {
450 * @see IJavaElementDelta
452 public int getKind() {
456 * @see IJavaElementDelta
458 public IJavaElement getMovedFromElement() {
459 return fMovedFromHandle;
462 * @see IJavaElementDelta
464 public IJavaElement getMovedToElement() {
465 return fMovedToHandle;
468 * @see IJavaElementDelta
470 public IJavaElementDelta[] getRemovedChildren() {
471 return getChildrenOfType(REMOVED);
474 * Return the collection of resource deltas. Return null if none.
476 public IResourceDelta[] getResourceDeltas() {
477 if (resourceDeltas == null) return null;
478 if (resourceDeltas.length != resourceDeltasCounter) {
479 System.arraycopy(resourceDeltas, 0, resourceDeltas = new IResourceDelta[resourceDeltasCounter], 0, resourceDeltasCounter);
481 return resourceDeltas;
484 * Adds the new element to a new array that contains all of the elements of the old array.
485 * Returns the new array.
487 protected IJavaElementDelta[] growAndAddToArray(IJavaElementDelta[] array, IJavaElementDelta addition) {
488 IJavaElementDelta[] old = array;
489 array = new IJavaElementDelta[old.length + 1];
490 System.arraycopy(old, 0, array, 0, old.length);
491 array[old.length] = addition;
495 * Creates the delta tree for the given element and delta, and then
496 * inserts the tree as an affected child of this node.
498 protected void insertDeltaTree(IJavaElement element, JavaElementDelta delta) {
499 JavaElementDelta childDelta= createDeltaTree(element, delta);
500 if (!this.equalsAndSameParent(element, getElement())) { // handle case of two jars that can be equals but not in the same project
501 addAffectedChild(childDelta);
505 * Creates the nested deltas resulting from an move operation.
506 * Convenience method for creating the "move from" delta.
507 * The constructor should be used to create the root delta
508 * and then the move operation should call this method.
510 public void movedFrom(IJavaElement movedFromElement, IJavaElement movedToElement) {
511 JavaElementDelta removedDelta = new JavaElementDelta(movedFromElement);
512 removedDelta.fKind = REMOVED;
513 removedDelta.fChangeFlags |= F_MOVED_TO;
514 removedDelta.fMovedToHandle = movedToElement;
515 insertDeltaTree(movedFromElement, removedDelta);
518 * Creates the nested deltas resulting from an move operation.
519 * Convenience method for creating the "move to" delta.
520 * The constructor should be used to create the root delta
521 * and then the move operation should call this method.
523 public void movedTo(IJavaElement movedToElement, IJavaElement movedFromElement) {
524 JavaElementDelta addedDelta = new JavaElementDelta(movedToElement);
525 addedDelta.fKind = ADDED;
526 addedDelta.fChangeFlags |= F_MOVED_FROM;
527 addedDelta.fMovedFromHandle = movedFromElement;
528 insertDeltaTree(movedToElement, addedDelta);
531 * Creates the nested deltas for an opened element.
533 public void opened(IJavaElement element) {
534 JavaElementDelta delta = new JavaElementDelta(element);
535 delta.fKind = CHANGED;
536 delta.fChangeFlags |= F_OPENED;
537 insertDeltaTree(element, delta);
540 * Removes the child delta from the collection of affected children.
542 protected void removeAffectedChild(JavaElementDelta child) {
544 if (fAffectedChildren != null) {
545 for (int i = 0; i < fAffectedChildren.length; i++) {
546 if (this.equalsAndSameParent(fAffectedChildren[i].getElement(), child.getElement())) { // handle case of two jars that can be equals but not in the same project
553 fAffectedChildren= removeAndShrinkArray(fAffectedChildren, index);
557 * Removes the element from the array.
558 * Returns the a new array which has shrunk.
560 protected IJavaElementDelta[] removeAndShrinkArray(IJavaElementDelta[] old, int index) {
561 IJavaElementDelta[] array = new IJavaElementDelta[old.length - 1];
563 System.arraycopy(old, 0, array, 0, index);
564 int rest = old.length - index - 1;
566 System.arraycopy(old, index + 1, array, index, rest);
570 * Creates the nested deltas resulting from an delete operation.
571 * Convenience method for creating removed deltas.
572 * The constructor should be used to create the root delta
573 * and then the delete operation should call this method.
575 public void removed(IJavaElement element) {
578 public void removed(IJavaElement element, int flags) {
579 JavaElementDelta removedDelta= new JavaElementDelta(element);
580 insertDeltaTree(element, removedDelta);
581 JavaElementDelta actualDelta = getDeltaFor(element);
582 if (actualDelta != null) {
583 actualDelta.removed();
584 actualDelta.changeFlags |= flags;
585 actualDelta.fAffectedChildren = fgEmptyDelta;
590 * Creates the nested deltas resulting from a change operation.
591 * Convenience method for creating change deltas.
592 * The constructor should be used to create the root delta
593 * and then a change operation should call this method.
595 public void sourceAttached(IJavaElement element) {
596 JavaElementDelta attachedDelta = new JavaElementDelta(element);
597 attachedDelta.fKind = CHANGED;
598 attachedDelta.fChangeFlags |= F_SOURCEATTACHED;
599 insertDeltaTree(element, attachedDelta);
602 * Creates the nested deltas resulting from a change operation.
603 * Convenience method for creating change deltas.
604 * The constructor should be used to create the root delta
605 * and then a change operation should call this method.
607 public void sourceDetached(IJavaElement element) {
608 JavaElementDelta detachedDelta = new JavaElementDelta(element);
609 detachedDelta.fKind = CHANGED;
610 detachedDelta.fChangeFlags |= F_SOURCEDETACHED;
611 insertDeltaTree(element, detachedDelta);
614 * Returns a string representation of this delta's
615 * structure suitable for debug purposes.
619 public String toDebugString(int depth) {
620 StringBuffer buffer = new StringBuffer();
621 for (int i= 0; i < depth; i++) {
624 buffer.append(((JavaElement)getElement()).toDebugString());
625 buffer.append("["); //$NON-NLS-1$
627 case IJavaElementDelta.ADDED :
630 case IJavaElementDelta.REMOVED :
633 case IJavaElementDelta.CHANGED :
640 buffer.append("]: {"); //$NON-NLS-1$
641 int changeFlags = getFlags();
642 boolean prev = false;
643 if ((changeFlags & IJavaElementDelta.F_CHILDREN) != 0) {
645 buffer.append(" | "); //$NON-NLS-1$
646 buffer.append("CHILDREN"); //$NON-NLS-1$
649 if ((changeFlags & IJavaElementDelta.F_CONTENT) != 0) {
651 buffer.append(" | "); //$NON-NLS-1$
652 buffer.append("CONTENT"); //$NON-NLS-1$
655 if ((changeFlags & IJavaElementDelta.F_MOVED_FROM) != 0) {
657 buffer.append(" | "); //$NON-NLS-1$
658 buffer.append("MOVED_FROM(" + ((JavaElement)getMovedFromElement()).toStringWithAncestors() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
661 if ((changeFlags & IJavaElementDelta.F_MOVED_TO) != 0) {
663 buffer.append(" | "); //$NON-NLS-1$
664 buffer.append("MOVED_TO(" + ((JavaElement)getMovedToElement()).toStringWithAncestors() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
667 if ((changeFlags & IJavaElementDelta.F_ADDED_TO_CLASSPATH) != 0) {
669 buffer.append(" | "); //$NON-NLS-1$
670 buffer.append("ADDED TO CLASSPATH"); //$NON-NLS-1$
673 if ((changeFlags & IJavaElementDelta.F_REMOVED_FROM_CLASSPATH) != 0) {
675 buffer.append(" | "); //$NON-NLS-1$
676 buffer.append("REMOVED FROM CLASSPATH"); //$NON-NLS-1$
679 if ((changeFlags & IJavaElementDelta.F_REORDER) != 0) {
681 buffer.append(" | "); //$NON-NLS-1$
682 buffer.append("REORDERED"); //$NON-NLS-1$
685 if ((changeFlags & IJavaElementDelta.F_ARCHIVE_CONTENT_CHANGED) != 0) {
687 buffer.append(" | "); //$NON-NLS-1$
688 buffer.append("ARCHIVE CONTENT CHANGED"); //$NON-NLS-1$
691 if ((changeFlags & IJavaElementDelta.F_SOURCEATTACHED) != 0) {
693 buffer.append(" | "); //$NON-NLS-1$
694 buffer.append("SOURCE ATTACHED"); //$NON-NLS-1$
697 if ((changeFlags & IJavaElementDelta.F_SOURCEDETACHED) != 0) {
699 buffer.append(" | "); //$NON-NLS-1$
700 buffer.append("SOURCE DETACHED"); //$NON-NLS-1$
703 if ((changeFlags & IJavaElementDelta.F_MODIFIERS) != 0) {
705 buffer.append(" | "); //$NON-NLS-1$
706 buffer.append("MODIFIERS CHANGED"); //$NON-NLS-1$
709 if ((changeFlags & IJavaElementDelta.F_SUPER_TYPES) != 0) {
711 buffer.append(" | "); //$NON-NLS-1$
712 buffer.append("SUPER TYPES CHANGED"); //$NON-NLS-1$
715 if ((changeFlags & IJavaElementDelta.F_FINE_GRAINED) != 0) {
717 buffer.append(" | "); //$NON-NLS-1$
718 buffer.append("FINE GRAINED"); //$NON-NLS-1$
721 buffer.append("}"); //$NON-NLS-1$
722 IJavaElementDelta[] children = getAffectedChildren();
723 if (children != null) {
724 for (int i = 0; i < children.length; ++i) {
725 buffer.append("\n"); //$NON-NLS-1$
726 buffer.append(((JavaElementDelta) children[i]).toDebugString(depth + 1));
729 for (int i = 0; i < resourceDeltasCounter; i++) {
730 buffer.append("\n");//$NON-NLS-1$
731 for (int j = 0; j < depth+1; j++) {
734 IResourceDelta resourceDelta = resourceDeltas[i];
735 buffer.append(resourceDelta.toString());
736 buffer.append("["); //$NON-NLS-1$
737 switch (resourceDelta.getKind()) {
738 case IResourceDelta.ADDED :
741 case IResourceDelta.REMOVED :
744 case IResourceDelta.CHANGED :
751 buffer.append("]"); //$NON-NLS-1$
753 return buffer.toString();
756 * Returns a string representation of this delta's
757 * structure suitable for debug purposes.
759 public String toString() {
760 return toDebugString(0);