001/*
002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
003 * 
004 * This software is open source. 
005 * See the bottom of this file for the licence.
006 * 
007 * $Id: BranchTreeNode.java,v 1.2 2001/12/19 09:51:39 jstrachan Exp $
008 */
009
010package org.dom4j.swing;
011
012import java.util.ArrayList;
013import java.util.Enumeration;
014import java.util.List;
015
016import javax.swing.tree.TreeNode;
017   
018import org.dom4j.Branch;
019import org.dom4j.CharacterData;
020import org.dom4j.Node;
021
022/** <p><code>BranchTreeNode</code> implements the Swing TreeNode interface
023  * to bind dom4j XML Branch nodes (i.e. Document and Element nodes) to a Swing TreeModel.</p>
024  *
025  * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> (james.strachan@metastuff.com)
026  * @author Jakob Jenkov
027  * @version $Revision: 1.2 $ 
028  */
029public class BranchTreeNode extends LeafTreeNode {
030
031    /** Stores the child tree nodes */
032    protected List children;
033
034    
035    public BranchTreeNode() {
036    }
037    
038    public BranchTreeNode(Branch xmlNode) {
039        super(xmlNode);
040    }
041    
042    public BranchTreeNode(TreeNode parent, Branch xmlNode) {
043        super( parent, xmlNode );
044    }
045    
046
047    // TreeNode methods
048    //-------------------------------------------------------------------------                
049    public Enumeration children() {
050        return new Enumeration() {
051            int index = -1;
052            
053            public boolean hasMoreElements() {
054                return index++ < getChildCount();
055            }
056            
057            public Object nextElement() {
058                return getChildAt( index );
059            }
060        };
061    }
062    
063    public boolean getAllowsChildren() {
064        return true;
065    }
066    
067    public TreeNode getChildAt(int childIndex) {
068        return (TreeNode) getChildList().get(childIndex);
069    }
070    
071    public int getChildCount() {
072        return getChildList().size();
073    }
074    
075    public int getIndex(TreeNode node) {
076        return getChildList().indexOf(node);
077    }
078    
079    public boolean isLeaf() {
080        return getXmlBranch().nodeCount() <= 0;
081    }
082    
083    public String toString() {
084        return xmlNode.getName();
085    }
086
087    
088    // Implementation methods
089    //-------------------------------------------------------------------------                
090    
091    /** Uses Lazy Initialization pattern to create a List of children */
092    protected List getChildList() {
093        // for now lets just create the children once, the first time they 
094        // are asked for.
095        // XXXX - we may wish to detect inconsistencies here....
096        if ( children == null ) {
097            // add attributes and content as children?
098            Branch branch = getXmlBranch();
099            int size = branch.nodeCount();
100            children = new ArrayList( size );
101            for ( int i = 0; i < size; i++ ) {
102                Node node = branch.node(i);
103                
104                // ignore whitespace text nodes
105                if ( node instanceof CharacterData ) {
106                    String text = node.getText();
107                    if ( text == null ) {
108                        continue;
109                    }
110                    text = text.trim();
111                    if ( text.length() <= 0 ) {
112                        continue;
113                    }
114                }
115                children.add( createChildTreeNode( node ) );
116            }
117        }
118        return children;
119    }
120
121    /** Factory method to create child tree nodes for a given XML node type
122      */
123    protected TreeNode createChildTreeNode( Node xmlNode ) {
124        if ( xmlNode instanceof Branch ) {
125            return new BranchTreeNode( this, (Branch) xmlNode );
126        }
127        else { 
128            return new LeafTreeNode( this, xmlNode );
129        }
130            
131    }
132    protected Branch getXmlBranch() {
133        return (Branch) xmlNode;
134    }
135}
136
137
138
139
140/*
141 * Redistribution and use of this software and associated documentation
142 * ("Software"), with or without modification, are permitted provided
143 * that the following conditions are met:
144 *
145 * 1. Redistributions of source code must retain copyright
146 *    statements and notices.  Redistributions must also contain a
147 *    copy of this document.
148 *
149 * 2. Redistributions in binary form must reproduce the
150 *    above copyright notice, this list of conditions and the
151 *    following disclaimer in the documentation and/or other
152 *    materials provided with the distribution.
153 *
154 * 3. The name "DOM4J" must not be used to endorse or promote
155 *    products derived from this Software without prior written
156 *    permission of MetaStuff, Ltd.  For written permission,
157 *    please contact dom4j-info@metastuff.com.
158 *
159 * 4. Products derived from this Software may not be called "DOM4J"
160 *    nor may "DOM4J" appear in their names without prior written
161 *    permission of MetaStuff, Ltd. DOM4J is a registered
162 *    trademark of MetaStuff, Ltd.
163 *
164 * 5. Due credit should be given to the DOM4J Project
165 *    (http://dom4j.org/).
166 *
167 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
168 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
169 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
170 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
171 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
172 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
173 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
174 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
175 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
176 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
177 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
178 * OF THE POSSIBILITY OF SUCH DAMAGE.
179 *
180 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
181 *
182 * $Id: BranchTreeNode.java,v 1.2 2001/12/19 09:51:39 jstrachan Exp $
183 */