001    package org.apache.myfaces.tobago.model;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import java.util.ArrayList;
021    import java.util.Collections;
022    import java.util.List;
023    
024    
025    public class Node {
026    
027      private List<Node> children;
028    
029      private Node parent;
030    
031      // XXX is this okay?
032      private String label;
033    
034      public void add(Node node) {
035        initChildren();
036        children.add(node);
037        node.setParent(this);
038      }
039    
040      public List<Node> getChildren() {
041        initChildren();
042        return children;
043      }
044    
045      private void initChildren() {
046        if (children == null) {
047          children = new ArrayList<Node>();
048        }
049      }
050    
051      public Node getChildAt(int index) {
052        initChildren();
053        return children.get(index);
054      }
055    
056      public boolean isRoot() {
057        return parent == null;
058      }
059    
060      public int getIndex(Node node) {
061        for (int i = 0; i < children.size(); i++) {
062          Node child = children.get(i);
063          if (child.equals(node)) {
064             return i;
065          }
066        }
067        return -1;
068      }
069    
070      public int getChildCount() {
071        return children == null ? 0 : children.size();
072      }
073    
074      public boolean hasNextSibling() {
075        return parent != null && parent.getIndex(this) + 1 < parent.getChildCount();
076      }
077    
078      public TreePath getPath() {
079        List<Integer> result = new ArrayList<Integer>();
080        Node node = this;
081        Node parent = this.parent;
082        while (parent != null) {
083          int index = parent.getIndex(node);
084          result.add(index);
085          node = parent;
086          parent = node.getParent();
087        }
088        result.add(0);
089        Collections.reverse(result);
090        return new TreePath(result);
091      }
092    
093      public Node getParent() {
094        return parent;
095      }
096    
097      public void setParent(Node parent) {
098        this.parent = parent;
099      }
100    
101      public String getLabel() {
102        return label;
103      }
104    
105      public void setLabel(String label) {
106        this.label = label;
107      }
108    }