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-2007 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:
042: /*
043: * Created on Jun 27, 2003
044: *
045: */
046: package org.netbeans.modules.uml.ui.swing.treetable;
047:
048: import java.util.Enumeration;
049: import javax.swing.tree.DefaultMutableTreeNode;
050: import javax.swing.tree.TreeNode;
051:
052: /**
053: * @author sumitabhk
054: *
055: */
056: public class JDefaultMutableTreeNode extends DefaultMutableTreeNode {
057: private String m_Key = null;
058: private int m_Row = 0;
059: private boolean m_ExpandedState = false;
060:
061: //this variable will be used by the node to decide if it is one of the roots.
062: private boolean m_IsRoot = false;
063:
064: /**
065: *
066: */
067: public JDefaultMutableTreeNode() {
068: super ();
069: }
070:
071: public JDefaultMutableTreeNode(Object userObj) {
072: super (userObj);
073: }
074:
075: public JDefaultMutableTreeNode(Object userObj, boolean allowChild) {
076: super (userObj, allowChild);
077: }
078:
079: public String getKey() {
080: return m_Key;
081: }
082:
083: public void setKey(String key) {
084: m_Key = key;
085: }
086:
087: public int getRow() {
088: if (m_Row != 0) {
089: return m_Row;
090: }
091: int row = 0;
092: TreeNode nodeWhoseIndexIsNeeded = this ;
093: TreeNode treeNode = this .getParent();
094: TreeNode root = this .getRoot();
095: while (!root.equals(treeNode)) {
096: int index = treeNode.getIndex(nodeWhoseIndexIsNeeded);
097: for (int x = 0; x < index; x++) {
098: row += countAllNodes(treeNode.getChildAt(x));
099: }
100: row++;
101: nodeWhoseIndexIsNeeded = treeNode;
102: treeNode = treeNode.getParent();
103: }
104:
105: int index = treeNode.getIndex(nodeWhoseIndexIsNeeded);
106: for (int x = 0; x < index; x++) {
107: row += countAllNodes(treeNode.getChildAt(x));
108: }
109: row++;
110: return row;
111: }
112:
113: public void setRow(int row) {
114: m_Row = row;
115: }
116:
117: public void setExpanded(boolean expand) {
118: m_ExpandedState = expand;
119: }
120:
121: public boolean isExpanded() {
122: return m_ExpandedState;
123: }
124:
125: public boolean isRoot() {
126: return m_IsRoot;
127: }
128:
129: public void setIsRoot(boolean val) {
130: m_IsRoot = val;
131: }
132:
133: private int countAllNodes(TreeNode node) {
134: if (node.isLeaf()) {
135: return 1;
136: }
137: int res = 1;
138: for (Enumeration e = node.children(); e.hasMoreElements();) {
139: TreeNode ch = (TreeNode) e.nextElement();
140: res += countAllNodes(ch);
141: }
142: return res;
143: }
144: }
|