KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
14 import org.eclipse.core.internal.utils.Assert;
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.core.runtime.Path;
17
18 /**
19  * An implementation of the visitor pattern, for visiting the elements
20  * of an ElementTreeDelta.
21  */

22 public class DeltaIterator {
23     /** pre-order traversal means visit the root first, then the children */
24     public static final int PRE_ORDER = 0;
25
26     /** post-order means visit the children, and then the root */
27     public static final int POST_ORDER = 1;
28
29     /** traversal order */
30     private final int order;
31
32     private ElementTreeDelta elementTreeDelta;
33     private DeltaDataTree deltaTree;
34     private ElementTree oldTree;
35     private ElementTree newTree;
36
37     /**
38      * Creates a new DeltaIterator that traverses the tree in pre order.
39      */

40     public DeltaIterator() {
41         order = PRE_ORDER;
42     }
43
44     /**
45      * Creates a new DeltaIterator that traverses the tree in the specified manner.
46      */

47     public DeltaIterator(int order) {
48         this.order = order;
49     }
50
51     /**
52      * Initializes the iterator
53      */

54     private void initialize(ElementTreeDelta tree) {
55         elementTreeDelta = tree;
56         deltaTree = tree.getDeltaTree();
57         oldTree = tree.getParent();
58         newTree = tree.getElementTree();
59     }
60
61     /**
62      * Iterates through the given element tree delta and visits each element,
63      * passing in the element's path and element object.
64      */

65     public void iterate(ElementTreeDelta tree, IDeltaVisitor visitor) {
66         iterate(tree, visitor, Path.ROOT);
67     }
68
69     /**
70      * Iterates through the given element tree delta and visits each element
71      * in the subtree rooted at the given path, passing in the element's path
72      * and element object.
73      */

74     public void iterate(ElementTreeDelta tree, IDeltaVisitor visitor, IPath path) {
75         initialize(tree);
76
77         /* don't visit the root element */
78         if (path.isRoot()) {
79             IPath[] children = deltaTree.getChildren(path);
80             for (int i = 0; i < children.length; i++) {
81                 iterate(visitor, children[i], NodeComparison.K_CHANGED);
82             }
83         } else {
84             iterate(visitor, path);
85         }
86     }
87
88     /**
89      * Starts the iteration at the provided entry point. If the entry
90      * point is not in the real delta, the iteration terminates.
91      */

92     private void iterate(IDeltaVisitor visitor, IPath path) {
93         /* find which tree this element is in */
94         DataTreeNode node = (DataTreeNode) deltaTree.findNodeAt(path);
95
96         if (node == null) {
97             /* look in old tree */
98             if (oldTree.includes(path)) {
99                 iterate(visitor, path, NodeComparison.K_ADDED);
100             } else {
101                 /* look in new tree */
102                 if (newTree.includes(path)) {
103                     iterate(visitor, path, NodeComparison.K_REMOVED);
104                 }
105             }
106         } else {
107             /* look in the delta tree */
108             iterate(visitor, path, NodeComparison.K_CHANGED);
109         }
110     }
111
112     /**
113      * Iterates from the given node, based on the kind of change in the parent.
114      */

115     private void iterate(IDeltaVisitor visitor, IPath path, int parentChange) {
116         int comparison = 0, realChange = 0;
117         Object JavaDoc oldData = null;
118         Object JavaDoc newData = null;
119         IPath[] children = null;
120
121         switch (parentChange) {
122             case NodeComparison.K_ADDED :
123                 /* look in the new tree only */
124                 comparison = realChange = parentChange;
125                 newData = newTree.getElementData(path);
126                 children = newTree.getChildren(path);
127                 break;
128             case NodeComparison.K_REMOVED :
129                 /* look in the old tree only */
130                 comparison = realChange = parentChange;
131                 oldData = oldTree.getElementData(path);
132                 children = oldTree.getChildren(path);
133                 break;
134             case NodeComparison.K_CHANGED :
135                 /* look in delta tree */
136                 NodeComparison info = (NodeComparison) deltaTree.getData(path);
137                 comparison = info.getUserComparison();
138
139                 realChange = info.getComparison();
140                 children = deltaTree.getChildren(path);
141                 oldData = info.getOldData();
142                 newData = info.getNewData();
143         }
144
145         Assert.isNotNull(children);
146
147         boolean visitChildren = true;
148         if (order == PRE_ORDER) {
149             visitChildren = visitor.visitElement(elementTreeDelta, path, oldData, newData, comparison);
150         }
151
152         if (visitChildren) {
153             for (int i = 0; i < children.length; i++) {
154                 iterate(visitor, children[i], realChange);
155             }
156         }
157
158         if (order == POST_ORDER) {
159             visitor.visitElement(elementTreeDelta, path, oldData, newData, comparison);
160         }
161     }
162 }
Popular Tags