KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > viewers > ModelNode


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.viewers;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.jface.viewers.TreePath;
17
18 /**
19  * A node in an asynchronous model.
20  *
21  * @since 3.2
22  */

23 public class ModelNode {
24
25     private Object JavaDoc fElement; // model element
26
private boolean fIsContainer; // whether this element may have children
27
private ModelNode fParent; // parent node or null for root
28
private ModelNode[] fChildren; // child nodes, possibly null
29
private boolean fDisposed; // whether this node has been disposed
30

31     public ModelNode(ModelNode parent, Object JavaDoc element) {
32         fParent = parent;
33         fElement = element;
34     }
35     
36     public synchronized Object JavaDoc getElement() {
37         return fElement;
38     }
39     
40     public synchronized void remap(Object JavaDoc element) {
41         fElement = element;
42     }
43     
44     public ModelNode getParentNode() {
45         return fParent;
46     }
47     
48     public synchronized boolean isContainer() {
49         return fIsContainer;
50     }
51     
52     public synchronized ModelNode[] getChildrenNodes() {
53         return fChildren;
54     }
55     
56     public synchronized boolean isDisposed() {
57         return fDisposed;
58     }
59     
60     public synchronized void dispose() {
61         fDisposed = true;
62         ModelNode[] childrenNodes = getChildrenNodes();
63         if (childrenNodes != null) {
64             for (int i = 0; i < childrenNodes.length; i++) {
65                 childrenNodes[i].dispose();
66             }
67         }
68     }
69     
70     /**
71      * Returns whether this node corresponds to the given path
72      *
73      * @param path tree path
74      */

75     public synchronized boolean correspondsTo(TreePath path) {
76         int index = path.getSegmentCount() - 1;
77         ModelNode node = this;
78         while (index >= 0 && node != null) {
79             Object JavaDoc pathElement = path.getSegment(index);
80             if (pathElement.equals(node.getElement())) {
81                 index--;
82                 node = node.getParentNode();
83             } else {
84                 return false;
85             }
86         }
87         return index == -1;
88     }
89     
90     /**
91      * Returns a tree path corresponding to this node.
92      *
93      * @return
94      */

95     public synchronized TreePath getTreePath() {
96         List JavaDoc path = new ArrayList JavaDoc();
97         ModelNode node = this;
98         while (node != null) {
99             path.add(0, node.getElement());
100             node = node.getParentNode();
101         }
102         return new TreePath(path.toArray());
103     }
104     
105     /**
106      * Adds the given child to this node.
107      *
108      * @param child
109      */

110     public synchronized void addChild(ModelNode child) {
111         if (fChildren == null) {
112             fChildren = new ModelNode[] {child};
113         } else {
114             ModelNode[] kids = new ModelNode[fChildren.length + 1];
115             System.arraycopy(fChildren, 0, kids, 0, fChildren.length);
116             kids[fChildren.length] = child;
117             fChildren = kids;
118         }
119     }
120     
121     /**
122      * Removes the given child from this node.
123      *
124      * @param child
125      */

126     public synchronized void removeChild(ModelNode child) {
127         if (fChildren != null) {
128             for (int i = 0; i < fChildren.length; i++) {
129                 ModelNode kid = fChildren[i];
130                 if (child == kid) {
131                     ModelNode[] newNodes= new ModelNode[fChildren.length - 1];
132                     System.arraycopy(fChildren, 0, newNodes, 0, i);
133                     if (i < newNodes.length) {
134                         System.arraycopy(fChildren, i + 1, newNodes, i, newNodes.length - i);
135                     }
136                     fChildren = newNodes;
137                     return;
138                 }
139             }
140         }
141     }
142     
143     /**
144      * Sets the children for this node
145      *
146      * @param children
147      */

148     public synchronized void setChildren(ModelNode[] children) {
149         if (children != null && children.length == 0) {
150             fChildren = null;
151             setIsContainer(false);
152         } else {
153             fChildren = children;
154         }
155     }
156     
157     /**
158      * Returns the number of children for this node.
159      *
160      * @return
161      */

162     public synchronized int getChildCount() {
163         if (fChildren == null) {
164             if (isContainer()) {
165                 return 1;
166             }
167             return 0;
168         }
169         return fChildren.length;
170     }
171     
172     /**
173      * Returns the index of the given child in this parent, or -1
174      *
175      * @param child
176      */

177     public synchronized int getChildIndex(ModelNode child) {
178        if (fChildren != null) {
179            for (int i = 0; i < fChildren.length; i++) {
180                 if (child == fChildren[i]) {
181                     return i;
182                 }
183            }
184        }
185        return -1;
186     }
187     
188     /**
189      * Sets whether this node has children.
190      *
191      * @param container
192      */

193     public synchronized void setIsContainer(boolean container) {
194         fIsContainer = container;
195     }
196     
197     public String JavaDoc toString() {
198         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
199         if (isDisposed()) {
200             buf.append("[DISPOSED] "); //$NON-NLS-1$
201
}
202         if (isContainer()) {
203             buf.append("[+] "); //$NON-NLS-1$
204
}
205         buf.append(getElement());
206         return buf.toString();
207     }
208     
209 }
210
Popular Tags