001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.tax;
042:
043: /**
044: * Child adds notion of parent node.
045: *
046: * @author Libor Kramolis
047: * @version 0.1
048: */
049: public abstract class TreeChild extends TreeNode {
050:
051: /** */
052: public static final String PROP_PARENT_NODE = "parentNode"; // NOI18N
053:
054: /** -- can be null. */
055: private TreeParentNode parentNode;
056:
057: //
058: // init
059: //
060:
061: /** Creates new TreeChild. */
062: protected TreeChild() {
063: }
064:
065: /**
066: * Creates new TreeChild -- copy constructor.
067: * (parentNode information is lost)
068: */
069: protected TreeChild(TreeChild child) {
070: super (child);
071: }
072:
073: //
074: // from TreeNode
075: //
076:
077: /**
078: */
079: public final TreeDocumentRoot getOwnerDocument() {
080: if (this instanceof TreeDocumentRoot) {
081: return (TreeDocumentRoot) this ;
082: }
083: if (getParentNode() == null) {
084: return null;
085: }
086: return getParentNode().getOwnerDocument();
087: }
088:
089: //
090: // context
091: //
092:
093: /**
094: */
095: public final boolean isInContext() {
096: return (getParentNode() != null);
097: }
098:
099: /**
100: */
101: public final void removeFromContext() throws ReadOnlyException {
102: if (isInContext()) {
103: getParentNode().removeChild(this );
104: }
105: }
106:
107: //
108: // itself
109: //
110:
111: /**
112: */
113: public final TreeParentNode getParentNode() {
114: return parentNode;
115: }
116:
117: /**
118: */
119: protected final void setParentNode(TreeParentNode newParentNode) {
120: if (Util.THIS.isLoggable()) /* then */
121: Util.THIS.debug("TreeChild::setParentNode [ " + this
122: + " ] : newParentNode = " + newParentNode); // NOI18N
123:
124: //
125: // check new value
126: //
127: if (Util.equals(this .parentNode, newParentNode))
128: return;
129:
130: //
131: // set new value
132: //
133: TreeParentNode oldParentNode = this .parentNode;
134:
135: this .parentNode = newParentNode;
136:
137: firePropertyChange(PROP_PARENT_NODE, oldParentNode,
138: newParentNode);
139: }
140:
141: //
142: // Children manipulation
143: //
144:
145: /**
146: */
147: public final TreeChild getPreviousSibling() {
148: int index = index();
149: if (index == -1) { // does not have parent node
150: return null;
151: }
152: if (index == 0) { // it is first node of parent node
153: return null;
154: }
155: return (TreeChild) getParentNode().getChildNodes().get(
156: index - 1);
157: }
158:
159: /**
160: */
161: public final TreeChild getNextSibling() {
162: if (Util.THIS.isLoggable()) /* then */
163: Util.THIS.debug("TreeChild [ " + this
164: + " ] ::getNextSibling: parentNode = "
165: + getParentNode()); // NOI18N
166:
167: int index = index();
168:
169: if (Util.THIS.isLoggable()) /* then */
170: Util.THIS.debug(" index : " + index); // NOI18N
171:
172: if (index == -1) { // does not have parent node
173: return null;
174: }
175:
176: if (Util.THIS.isLoggable()) /* then */
177: Util.THIS.debug(" parentNode.childNodes.size : "
178: + getParentNode().getChildNodes().size()); // NOI18N
179:
180: if ((index + 1) == getParentNode().getChildNodes().size()) { // it is last node of parent node
181: return null;
182: }
183: return (TreeChild) getParentNode().getChildNodes().get(
184: index + 1);
185: }
186:
187: /**
188: * @return index of this node in parent node child list or -1 if it does not have parent node.
189: */
190: public final int index() {
191: if (getParentNode() == null) {
192: return -1;
193: }
194: return getParentNode().indexOf(this );
195: }
196:
197: //
198: // util
199: //
200:
201: /**
202: */
203: public final boolean isDescendantOf(TreeParentNode testParentNode) {
204: TreeParentNode ancestor = getParentNode();
205:
206: while (ancestor != null) {
207: if (ancestor == testParentNode)
208: return true;
209: ancestor = ancestor.getParentNode();
210: }
211:
212: return false;
213: }
214:
215: }
|