View Javadoc

1   /*
2    * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3    *
4    * This software is open source.
5    * See the bottom of this file for the licence.
6    */
7   
8   package org.dom4j.swing;
9   
10  import java.util.ArrayList;
11  import java.util.Enumeration;
12  import java.util.List;
13  
14  import javax.swing.tree.TreeNode;
15  
16  import org.dom4j.Branch;
17  import org.dom4j.CharacterData;
18  import org.dom4j.Node;
19  
20  /***
21   * <p>
22   * <code>BranchTreeNode</code> implements the Swing TreeNode interface to bind
23   * dom4j XML Branch nodes (i.e. Document and Element nodes) to a Swing
24   * TreeModel.
25   * </p>
26   * 
27   * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
28   * @author Jakob Jenkov
29   * @version $Revision: 1.10 $
30   */
31  public class BranchTreeNode extends LeafTreeNode {
32      /*** Stores the child tree nodes */
33      protected List children;
34  
35      public BranchTreeNode() {
36      }
37  
38      public BranchTreeNode(Branch xmlNode) {
39          super(xmlNode);
40      }
41  
42      public BranchTreeNode(TreeNode parent, Branch xmlNode) {
43          super(parent, xmlNode);
44      }
45  
46      // TreeNode methods
47      // -------------------------------------------------------------------------
48      public Enumeration children() {
49          return new Enumeration() {
50              private int index = -1;
51  
52              public boolean hasMoreElements() {
53                  return (index + 1) < getChildCount();
54              }
55  
56              public Object nextElement() {
57                  return getChildAt(++index);
58              }
59          };
60      }
61  
62      public boolean getAllowsChildren() {
63          return true;
64      }
65  
66      public TreeNode getChildAt(int childIndex) {
67          return (TreeNode) getChildList().get(childIndex);
68      }
69  
70      public int getChildCount() {
71          return getChildList().size();
72      }
73  
74      public int getIndex(TreeNode node) {
75          return getChildList().indexOf(node);
76      }
77  
78      public boolean isLeaf() {
79          return getXmlBranch().nodeCount() <= 0;
80      }
81  
82      public String toString() {
83          return xmlNode.getName();
84      }
85  
86      // Implementation methods
87      // -------------------------------------------------------------------------
88  
89      /***
90       * Uses Lazy Initialization pattern to create a List of children
91       * 
92       * @return DOCUMENT ME!
93       */
94      protected List getChildList() {
95          // for now lets just create the children once, the first time they
96          // are asked for.
97          // XXXX - we may wish to detect inconsistencies here....
98          if (children == null) {
99              children = createChildList();
100         }
101 
102         return children;
103     }
104 
105     /***
106      * Factory method to create List of children TreeNodes
107      * 
108      * @return DOCUMENT ME!
109      */
110     protected List createChildList() {
111         // add attributes and content as children?
112         Branch branch = getXmlBranch();
113         int size = branch.nodeCount();
114         List childList = new ArrayList(size);
115 
116         for (int i = 0; i < size; i++) {
117             Node node = branch.node(i);
118 
119             // ignore whitespace text nodes
120             if (node instanceof CharacterData) {
121                 String text = node.getText();
122 
123                 if (text == null) {
124                     continue;
125                 }
126 
127                 text = text.trim();
128 
129                 if (text.length() <= 0) {
130                     continue;
131                 }
132             }
133 
134             childList.add(createChildTreeNode(node));
135         }
136 
137         return childList;
138     }
139 
140     /***
141      * Factory method to create child tree nodes for a given XML node type
142      * 
143      * @param xmlNode
144      *            DOCUMENT ME!
145      * 
146      * @return DOCUMENT ME!
147      */
148     protected TreeNode createChildTreeNode(Node xmlNode) {
149         if (xmlNode instanceof Branch) {
150             return new BranchTreeNode(this, (Branch) xmlNode);
151         } else {
152             return new LeafTreeNode(this, xmlNode);
153         }
154     }
155 
156     protected Branch getXmlBranch() {
157         return (Branch) xmlNode;
158     }
159 }
160 
161 /*
162  * Redistribution and use of this software and associated documentation
163  * ("Software"), with or without modification, are permitted provided that the
164  * following conditions are met:
165  * 
166  * 1. Redistributions of source code must retain copyright statements and
167  * notices. Redistributions must also contain a copy of this document.
168  * 
169  * 2. Redistributions in binary form must reproduce the above copyright notice,
170  * this list of conditions and the following disclaimer in the documentation
171  * and/or other materials provided with the distribution.
172  * 
173  * 3. The name "DOM4J" must not be used to endorse or promote products derived
174  * from this Software without prior written permission of MetaStuff, Ltd. For
175  * written permission, please contact dom4j-info@metastuff.com.
176  * 
177  * 4. Products derived from this Software may not be called "DOM4J" nor may
178  * "DOM4J" appear in their names without prior written permission of MetaStuff,
179  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
180  * 
181  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
182  * 
183  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
184  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
185  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
186  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
187  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
188  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
189  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
190  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
191  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
192  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
193  * POSSIBILITY OF SUCH DAMAGE.
194  * 
195  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
196  */