KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > treetable > AbstractTreeTableNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.usertasks.treetable;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.swing.tree.TreeNode JavaDoc;
29
30 /**
31  * Abstract node for a TreeTable
32  */

33 public abstract class AbstractTreeTableNode implements TreeTableNode {
34     /** could be used to show that this node has no children */
35     protected static TreeTableNode[] EMPTY_CHILDREN = {};
36
37     protected TreeTableNode parent;
38
39     /** children of this node. null means "not yet loaded" */
40     protected TreeTableNode children[];
41     
42     /**
43      * Creates a new instance of AbstractTreeTableNode
44      *
45      * @param parent parent of this node or null if this node is a root
46      */

47     public AbstractTreeTableNode(TreeTableNode parent) {
48         this.parent = parent;
49     }
50     
51     /**
52      * Should load the children in the field <code>children</code>
53      */

54     protected abstract void loadChildren();
55     
56     /**
57      * Returns an array with children of this node
58      *
59      * @return array with children
60      */

61     public TreeTableNode[] getChildren() {
62         if (children == null)
63             loadChildren();
64         return children;
65     }
66
67     public boolean isCellEditable(int column) {
68         return false;
69     }
70
71     public TreeNode JavaDoc getChildAt(int childIndex) {
72         return getChildren()[childIndex];
73     }
74
75     public int getChildCount() {
76         return getChildren().length;
77     }
78
79     public TreeNode JavaDoc getParent() {
80         return parent;
81     }
82
83     public int getIndex(TreeNode JavaDoc node) {
84         TreeTableNode[] ch = getChildren();
85         for (int i = 0; i < ch.length; i++) {
86             if (ch[i] == node)
87                 return i;
88         }
89         return -1;
90     }
91
92     public boolean getAllowsChildren() {
93         return true;
94     }
95
96     public boolean isLeaf() {
97         return false;
98     }
99
100     public Enumeration JavaDoc children() {
101         return Collections.enumeration(Arrays.asList(getChildren()));
102     }
103
104     /**
105      * todo
106      */

107     public void refreshChildren() {
108         this.children = null;
109     }
110     
111     /**
112      * Returns path from this node to the root
113      *
114      * @return path to the root
115      */

116     public TreeTableNode[] getPathToRoot() {
117         List JavaDoc<TreeTableNode> path = new ArrayList JavaDoc<TreeTableNode>();
118         TreeTableNode n = this;
119         while (n != null) {
120             path.add(0, n);
121             n = (TreeTableNode) n.getParent();
122         }
123         return path.toArray(new TreeTableNode[path.size()]);
124     }
125     
126     /**
127      * Finds the next node that should be selected after this node was
128      * deleted
129      *
130      * @return node to select or null
131      */

132     public TreeTableNode findNextNodeAfterDelete() {
133         if (getParent() == null)
134             return null;
135         if (getParent().getChildCount() == 1)
136             return (TreeTableNode) getParent();
137         int index = getParent().getIndex(this);
138         if (index == getParent().getChildCount() - 1)
139             return (TreeTableNode) getParent().getChildAt(index - 1);
140         else
141             return (TreeTableNode) getParent().getChildAt(index + 1);
142     }
143 }
144
Popular Tags