KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > gvt > GVTTreeWalker


1 /*
2
3    Copyright 2001 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.gvt;
19
20 import java.util.List JavaDoc;
21
22 /**
23  * <tt>GVTTreeWalker</tt> objects are used to navigate a GVT tree or subtree.
24  *
25  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
26  * @version $Id: GVTTreeWalker.java,v 1.4 2004/09/24 10:56:37 deweese Exp $
27  */

28 public class GVTTreeWalker {
29
30     /** The GVT root into which text is searched. */
31     protected GraphicsNode gvtRoot;
32
33     /** The root of the subtree of the GVT which is traversed. */
34     protected GraphicsNode treeRoot;
35
36     /** The current GraphicsNode. */
37     protected GraphicsNode currentNode;
38
39     /**
40      * Constructs a new <tt>GVTTreeWalker</tt>.
41      *
42      * @param treeRoot the top of the graphics node tree to search
43      */

44     public GVTTreeWalker(GraphicsNode treeRoot) {
45         this.gvtRoot = treeRoot.getRoot();
46         this.treeRoot = treeRoot;
47         this.currentNode = treeRoot;
48     }
49
50     /**
51      * Returns the root graphics node.
52      */

53     public GraphicsNode getRoot() {
54         return treeRoot;
55     }
56
57     /**
58      * Returns the GVT root graphics node.
59      */

60     public GraphicsNode getGVTRoot() {
61         return gvtRoot;
62     }
63
64     /**
65      * Sets the current GraphicsNode to the specified node.
66      *
67      * @param node the new current graphics node
68      * @exception IllegalArgumentException if the node is not part of
69      * the GVT Tree this walker is dedicated to
70      */

71     public void setCurrentGraphicsNode(GraphicsNode node) {
72         if (node.getRoot() != gvtRoot) {
73             throw new IllegalArgumentException JavaDoc
74                 ("The node "+node+" is not part of the document "+gvtRoot);
75         }
76         currentNode = node;
77     }
78
79     /**
80      * Returns the current <tt>GraphicsNode</tt>.
81      */

82     public GraphicsNode getCurrentGraphicsNode() {
83         return currentNode;
84     }
85
86     /**
87      * Returns the previous <tt>GraphicsNode</tt>. If the current
88      * graphics node does not have a previous node, returns null and
89      * retains the current node.
90      */

91     public GraphicsNode previousGraphicsNode() {
92         GraphicsNode result = getPreviousGraphicsNode(currentNode);
93         if (result != null) {
94             currentNode = result;
95         }
96         return result;
97     }
98
99     /**
100      * Returns the next <tt>GraphicsNode</tt>. If the current graphics
101      * node does not have a next node, returns null and retains the
102      * current node.
103      */

104     public GraphicsNode nextGraphicsNode() {
105         GraphicsNode result = getNextGraphicsNode(currentNode);
106         if (result != null) {
107             currentNode = result;
108         }
109         return result;
110     }
111
112     /**
113      * Returns the parent of the current <tt>GraphicsNode</tt>. If the
114      * current graphics node has no parent, returns null and retains
115      * the current node.
116      */

117     public GraphicsNode parentGraphicsNode() {
118         // Don't ascend above treeRoot.
119
if (currentNode == treeRoot) return null;
120
121         GraphicsNode result = currentNode.getParent();
122         if (result != null) {
123             currentNode = result;
124         }
125         return result;
126     }
127
128     /**
129      * Returns the next sibling of the current
130      * <tt>GraphicsNode</tt>. If the current graphics node does not
131      * have a next sibling, returns null and retains the current node.
132      */

133     public GraphicsNode getNextSibling() {
134         GraphicsNode result = getNextSibling(currentNode);
135         if (result != null) {
136             currentNode = result;
137         }
138         return result;
139     }
140
141     /**
142      * Returns the next previous of the current
143      * <tt>GraphicsNode</tt>. If the current graphics node does not
144      * have a previous sibling, returns null and retains the current
145      * node.
146      */

147     public GraphicsNode getPreviousSibling() {
148         GraphicsNode result = getPreviousSibling(currentNode);
149         if (result != null) {
150             currentNode = result;
151         }
152         return result;
153     }
154
155     /**
156      * Returns the first child of the current
157      * <tt>GraphicsNode</tt>. If the current graphics node does not
158      * have a first child, returns null and retains the current node.
159      */

160     public GraphicsNode firstChild() {
161         GraphicsNode result = getFirstChild(currentNode);
162         if (result != null) {
163             currentNode = result;
164         }
165         return result;
166     }
167
168     /**
169      * Returns the last child of the current <tt>GraphicsNode</tt>. If
170      * the current graphics node does not have a last child, returns
171      * null and retains the current node.
172      */

173     public GraphicsNode lastChild() {
174         GraphicsNode result = getLastChild(currentNode);
175         if (result != null) {
176             currentNode = result;
177         }
178         return result;
179     }
180
181     //////////////////////////////////////////////////////////////////////////
182
//////////////////////////////////////////////////////////////////////////
183

184     protected GraphicsNode getNextGraphicsNode(GraphicsNode node) {
185         if (node == null) {
186             return null;
187         }
188         // Go to the first child
189
GraphicsNode n = getFirstChild(node);
190         if (n != null) {
191             return n;
192         }
193
194         // Go to the next sibling
195
n = getNextSibling(node);
196         if (n != null) {
197             return n;
198         }
199
200         // Go to the first sibling of one of the ancestors
201
n = node;
202         while ((n = n.getParent()) != null && n != treeRoot) {
203             GraphicsNode t = getNextSibling(n);
204             if (t != null) {
205                 return t;
206             }
207         }
208         return null;
209     }
210
211     protected GraphicsNode getPreviousGraphicsNode(GraphicsNode node) {
212         if (node == null) {
213             return null;
214         }
215
216         // The previous of root is null
217
if (node == treeRoot) {
218             return null;
219         }
220
221         GraphicsNode n = getPreviousSibling(node);
222
223         // Go to the parent of a first child
224
if (n == null) {
225             return node.getParent();
226         }
227
228         // Go to the last child of child...
229
GraphicsNode t;
230         while ((t = getLastChild(n)) != null) {
231             n = t;
232         }
233         return n;
234     }
235
236     protected static GraphicsNode getLastChild(GraphicsNode node) {
237         if (!(node instanceof CompositeGraphicsNode)) {
238             return null;
239         }
240         CompositeGraphicsNode parent = (CompositeGraphicsNode)node;
241         List JavaDoc children = parent.getChildren();
242         if (children == null) {
243             return null;
244         }
245         if (children.size() >= 1) {
246             return (GraphicsNode)children.get(children.size()-1);
247         } else {
248             return null;
249         }
250     }
251
252     protected static GraphicsNode getPreviousSibling(GraphicsNode node) {
253         CompositeGraphicsNode parent = node.getParent();
254         if (parent == null) {
255             return null;
256         }
257         List JavaDoc children = parent.getChildren();
258         if (children == null) {
259             return null;
260         }
261         int index = children.indexOf(node);
262         if (index-1 >= 0) {
263             return (GraphicsNode)children.get(index-1);
264         } else {
265             return null;
266         }
267     }
268
269     protected static GraphicsNode getFirstChild(GraphicsNode node) {
270         if (!(node instanceof CompositeGraphicsNode)) {
271             return null;
272         }
273         CompositeGraphicsNode parent = (CompositeGraphicsNode)node;
274         List JavaDoc children = parent.getChildren();
275         if (children == null) {
276             return null;
277         }
278         if (children.size() >= 1) {
279             return (GraphicsNode)children.get(0);
280         } else {
281             return null;
282         }
283     }
284
285     protected static GraphicsNode getNextSibling(GraphicsNode node) {
286         CompositeGraphicsNode parent = node.getParent();
287         if (parent == null) {
288             return null;
289         }
290         List JavaDoc children = parent.getChildren();
291         if (children == null) {
292             return null;
293         }
294         int index = children.indexOf(node);
295         if (index+1 < children.size()) {
296             return (GraphicsNode)children.get(index+1);
297         } else {
298             return null;
299         }
300     }
301 }
302
Popular Tags