KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > TreeChild


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 package org.netbeans.tax;
20
21 /**
22  * Child adds notion of parent node.
23  *
24  * @author Libor Kramolis
25  * @version 0.1
26  */

27 public abstract class TreeChild extends TreeNode {
28
29     /** */
30     public static final String JavaDoc PROP_PARENT_NODE = "parentNode"; // NOI18N
31

32
33     /** -- can be null. */
34     private TreeParentNode parentNode;
35
36
37     //
38
// init
39
//
40

41     /** Creates new TreeChild. */
42     protected TreeChild () {
43     }
44
45
46     /**
47      * Creates new TreeChild -- copy constructor.
48      * (parentNode information is lost)
49      */

50     protected TreeChild (TreeChild child) {
51         super (child);
52     }
53     
54     
55     //
56
// from TreeNode
57
//
58

59     /**
60      */

61     public final TreeDocumentRoot getOwnerDocument () {
62         if ( this instanceof TreeDocumentRoot ) {
63             return (TreeDocumentRoot)this;
64         }
65         if ( getParentNode () == null ) {
66             return null;
67         }
68         return getParentNode ().getOwnerDocument ();
69     }
70     
71     
72     //
73
// context
74
//
75

76     /**
77      */

78     public final boolean isInContext () {
79         return ( getParentNode () != null );
80     }
81     
82     /**
83      */

84     public final void removeFromContext () throws ReadOnlyException {
85         if ( isInContext () ) {
86             getParentNode ().removeChild (this);
87         }
88     }
89     
90     
91     //
92
// itself
93
//
94

95     /**
96      */

97     public final TreeParentNode getParentNode () {
98         return parentNode;
99     }
100     
101     /**
102      */

103     protected final void setParentNode (TreeParentNode newParentNode) {
104         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("TreeChild::setParentNode [ " + this + " ] : newParentNode = " + newParentNode); // NOI18N
105

106         //
107
// check new value
108
//
109
if ( Util.equals (this.parentNode, newParentNode) )
110             return;
111         
112         //
113
// set new value
114
//
115
TreeParentNode oldParentNode = this.parentNode;
116         
117         this.parentNode = newParentNode;
118         
119         firePropertyChange (PROP_PARENT_NODE, oldParentNode, newParentNode);
120     }
121     
122     
123     //
124
// Children manipulation
125
//
126

127     /**
128      */

129     public final TreeChild getPreviousSibling () {
130         int index = index ();
131         if ( index == -1 ) { // does not have parent node
132
return null;
133         }
134         if ( index == 0 ) { // it is first node of parent node
135
return null;
136         }
137         return (TreeChild)getParentNode ().getChildNodes ().get (index - 1);
138     }
139     
140     /**
141      */

142     public final TreeChild getNextSibling () {
143         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("TreeChild [ " + this + " ] ::getNextSibling: parentNode = " + getParentNode ()); // NOI18N
144

145         int index = index ();
146         
147         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" index : " + index); // NOI18N
148

149         if ( index == -1 ) { // does not have parent node
150
return null;
151         }
152         
153         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" parentNode.childNodes.size : " + getParentNode ().getChildNodes ().size ()); // NOI18N
154

155         if ( (index + 1) == getParentNode ().getChildNodes ().size () ) { // it is last node of parent node
156
return null;
157         }
158         return (TreeChild)getParentNode ().getChildNodes ().get (index + 1);
159     }
160     
161     /**
162      * @return index of this node in parent node child list or -1 if it does not have parent node.
163      */

164     public final int index () {
165         if ( getParentNode () == null ) {
166             return -1;
167         }
168         return getParentNode ().indexOf (this);
169     }
170     
171     
172     //
173
// util
174
//
175

176     /**
177      */

178     public final boolean isDescendantOf (TreeParentNode testParentNode) {
179         TreeParentNode ancestor = getParentNode ();
180         
181         while ( ancestor != null ) {
182             if ( ancestor == testParentNode )
183                 return true;
184             ancestor = ancestor.getParentNode ();
185         }
186         
187         return false;
188     }
189     
190 }
191
Popular Tags