KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > watson > ElementDelta


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.watson;
12
13 import org.eclipse.core.internal.dtree.NodeComparison;
14 import org.eclipse.core.runtime.IPath;
15
16 /**
17  * An element delta describes how an element has been affected
18  * in an element tree delta.
19  */

20 public class ElementDelta {
21     protected ElementTreeDelta treeDelta;
22     protected IPath pathInDelta;
23     protected IPath pathInTrees;
24     protected NodeComparison comparison;
25
26     /** Constructs an element delta.
27      */

28     ElementDelta(ElementTreeDelta treeDelta, IPath pathInTrees, IPath pathInDelta, NodeComparison comparison) {
29         this.treeDelta = treeDelta;
30         this.pathInDelta = pathInDelta;
31         this.pathInTrees = pathInTrees;
32         this.comparison = comparison;
33     }
34
35     /**
36      * Destroys this delta and all trees deltas referenced herein.
37      */

38     public void destroy() {
39         treeDelta.destroy();
40         treeDelta = null;
41         pathInDelta = null;
42         pathInTrees = null;
43         comparison = null;
44     }
45
46     /**
47      * Returns any elements in the new tree that match the given filter query.
48      */

49     protected ElementDelta[] getAddedChildren(IDeltaFilter filter) {
50         IPath[] children = treeDelta.getElementTree().getChildren(pathInTrees);
51         ElementDelta[] deltas = new ElementDelta[children.length];
52         for (int i = children.length; --i >= 0;) {
53             deltas[i] = new ElementDelta(treeDelta, children[i], pathInDelta.append(children[i].lastSegment()), comparison);
54         }
55         return deltas;
56     }
57
58     /**
59      * Returns ElementDeltas for any elements that match the given filter query.
60      */

61     public ElementDelta[] getAffectedChildren(IDeltaFilter filter) {
62         switch (comparison.getComparison()) {
63             case NodeComparison.K_CHANGED :
64                 return treeDelta.getAffectedElements(pathInDelta, filter);
65             case NodeComparison.K_ADDED :
66                 /**
67                  * Assume all children have the same user comparison as
68                  * this node. Is this a reasonable assumption?
69                  */

70                 if (filter.includeElement(comparison.getUserComparison())) {
71                     return getAddedChildren(filter);
72                 }
73                 break;
74             case NodeComparison.K_REMOVED :
75                 if (filter.includeElement(comparison.getUserComparison())) {
76                     return getRemovedChildren(filter);
77                 }
78                 break;
79         }
80         return new ElementDelta[0];
81     }
82
83     /**
84      * Returns an integer describing the changes in this element between the
85      * old and new trees. This integer can be assigned any value by the
86      * ElementTree client. A comparison value of 0 indicates that there was
87      * no change in the data.
88      * @see IElementComparator
89      */

90     public int getComparison() {
91         return comparison.getUserComparison();
92     }
93
94     /**
95      * Returns the path of the element described by this delta.
96      */

97     public IPath getPath() {
98         return pathInTrees;
99     }
100
101     /**
102      * Returns any elements in the old tree that match the given filter query.
103      */

104     protected ElementDelta[] getRemovedChildren(IDeltaFilter filter) {
105         IPath[] children = treeDelta.getParent().getChildren(pathInTrees);
106         ElementDelta[] deltas = new ElementDelta[children.length];
107         for (int i = children.length; --i >= 0;) {
108             deltas[i] = new ElementDelta(treeDelta, children[i], pathInDelta.append(children[i].lastSegment()), comparison);
109         }
110         return deltas;
111     }
112
113     /**
114      * Returns the element tree delta containing this element delta.
115      */

116     public ElementTreeDelta getTreeDelta() {
117         return treeDelta;
118     }
119
120     /**
121      * Return true if there are deltas describing affected children of the receiver
122      * including added, removed or changed children.
123      */

124     public boolean hasAffectedChildren(IDeltaFilter filter) {
125         int compare = comparison.getComparison();
126
127         /* search delta tree if it's a change */
128         if (compare == NodeComparison.K_CHANGED) {
129             return treeDelta.hasAffectedElements(pathInDelta, filter);
130         }
131
132         /**
133          * For added and deleted nodes, assume children have same
134          * comparison as parent. Is this reasonable?
135          */

136         if (filter.includeElement(comparison.getUserComparison())) {
137             if (compare == NodeComparison.K_ADDED) {
138                 /* look in new tree */
139                 return treeDelta.getElementTree().getChildCount(pathInTrees) > 0;
140             } else {
141                 return treeDelta.getParent().getChildCount(pathInTrees) > 0;
142             }
143         }
144         return false;
145     }
146
147     /**
148      * Returns a string representation of the receiver. Used for debugging
149      * purposes only
150      */

151     public String JavaDoc toString() {
152         return "ElementDelta(" + pathInTrees + ")"; //$NON-NLS-1$ //$NON-NLS-2$
153
}
154 }
Popular Tags