KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > watson > ElementSubtree


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

11 package org.eclipse.core.internal.watson;
12
13 import org.eclipse.core.internal.dtree.AbstractDataTreeNode;
14 import org.eclipse.core.internal.dtree.DataTreeNode;
15
16 /**
17  * An <code>ElementSubtree</code> is a simple datastructure representing the
18  * contents of an element tree. It can be used for rapidly creating ElementTree
19  * objects, but cannot be treated as an ElementTree itself.
20  * @see ElementTree(ElementSubtree)
21  * @see ElementTree#getElementSubtree()
22  */

23 class ElementSubtree {
24     protected String JavaDoc elementName;
25     protected Object JavaDoc elementData;
26     protected ElementSubtree[] children;
27
28     static final ElementSubtree[] EMPTY_ARRAY = new ElementSubtree[0];
29
30     /**
31      * Creates an <code>ElementChildSubtree</code> with the given element name and element,
32      * and child elements (organized by type).
33      * Passing either null or an empty array for childTypes indicates no children.
34      */

35     public ElementSubtree(String JavaDoc elementName, Object JavaDoc elementData, ElementSubtree[] children) {
36         if (children == null || children.length == 0) {
37             children = EMPTY_ARRAY;
38         }
39         this.elementName = elementName;
40         this.elementData = elementData;
41         this.children = children;
42     }
43
44     /**
45      * Creates an <code>ElementSubtree</code> from a (complete) element node.
46      */

47     ElementSubtree(DataTreeNode childNode) {
48         AbstractDataTreeNode[] childNodes = childNode.getChildren();
49         if (childNodes.length == 0) {
50             children = EMPTY_ARRAY;
51         } else {
52             ElementSubtree[] types = new ElementSubtree[childNodes.length];
53             for (int i = childNodes.length; --i >= 0;) {
54                 types[i] = new ElementSubtree((DataTreeNode) childNodes[i]);
55             }
56             children = types;
57         }
58         elementName = childNode.getName();
59         elementData = childNode.getData();
60     }
61
62     /**
63      * Returns the child subtrees.
64      * Returns null if there are no children.
65      */

66     public ElementSubtree[] getChildren() {
67         return children;
68     }
69
70     /**
71      * Returns the element data.
72      */

73     public Object JavaDoc getElementData() {
74         return elementData;
75     }
76
77     /**
78      * Returns the element name.
79      */

80     public String JavaDoc getElementName() {
81         return elementName;
82     }
83
84     /**
85      * For debugging purposes
86      */

87     public String JavaDoc toString() {
88         return "ElementSubtree(" + elementName + ", " + elementData + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
89
}
90 }
Popular Tags