1 18 package org.apache.batik.gvt; 19 20 import java.util.List ; 21 22 28 public class GVTTreeWalker { 29 30 31 protected GraphicsNode gvtRoot; 32 33 34 protected GraphicsNode treeRoot; 35 36 37 protected GraphicsNode currentNode; 38 39 44 public GVTTreeWalker(GraphicsNode treeRoot) { 45 this.gvtRoot = treeRoot.getRoot(); 46 this.treeRoot = treeRoot; 47 this.currentNode = treeRoot; 48 } 49 50 53 public GraphicsNode getRoot() { 54 return treeRoot; 55 } 56 57 60 public GraphicsNode getGVTRoot() { 61 return gvtRoot; 62 } 63 64 71 public void setCurrentGraphicsNode(GraphicsNode node) { 72 if (node.getRoot() != gvtRoot) { 73 throw new IllegalArgumentException 74 ("The node "+node+" is not part of the document "+gvtRoot); 75 } 76 currentNode = node; 77 } 78 79 82 public GraphicsNode getCurrentGraphicsNode() { 83 return currentNode; 84 } 85 86 91 public GraphicsNode previousGraphicsNode() { 92 GraphicsNode result = getPreviousGraphicsNode(currentNode); 93 if (result != null) { 94 currentNode = result; 95 } 96 return result; 97 } 98 99 104 public GraphicsNode nextGraphicsNode() { 105 GraphicsNode result = getNextGraphicsNode(currentNode); 106 if (result != null) { 107 currentNode = result; 108 } 109 return result; 110 } 111 112 117 public GraphicsNode parentGraphicsNode() { 118 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 133 public GraphicsNode getNextSibling() { 134 GraphicsNode result = getNextSibling(currentNode); 135 if (result != null) { 136 currentNode = result; 137 } 138 return result; 139 } 140 141 147 public GraphicsNode getPreviousSibling() { 148 GraphicsNode result = getPreviousSibling(currentNode); 149 if (result != null) { 150 currentNode = result; 151 } 152 return result; 153 } 154 155 160 public GraphicsNode firstChild() { 161 GraphicsNode result = getFirstChild(currentNode); 162 if (result != null) { 163 currentNode = result; 164 } 165 return result; 166 } 167 168 173 public GraphicsNode lastChild() { 174 GraphicsNode result = getLastChild(currentNode); 175 if (result != null) { 176 currentNode = result; 177 } 178 return result; 179 } 180 181 184 protected GraphicsNode getNextGraphicsNode(GraphicsNode node) { 185 if (node == null) { 186 return null; 187 } 188 GraphicsNode n = getFirstChild(node); 190 if (n != null) { 191 return n; 192 } 193 194 n = getNextSibling(node); 196 if (n != null) { 197 return n; 198 } 199 200 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 if (node == treeRoot) { 218 return null; 219 } 220 221 GraphicsNode n = getPreviousSibling(node); 222 223 if (n == null) { 225 return node.getParent(); 226 } 227 228 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 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 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 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 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 |