Project dom4j 1.5.2 [5/2/05 10:13 PM]
 
Coverage - org/dom4j/swing/BranchTreeNode.java
  /*
   * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
   * 
   * This software is open source. 
   * See the bottom of this file for the licence.
   * 
   * $Id: BranchTreeNode.java,v 1.8 2004/06/25 08:03:40 maartenc Exp $
   */
 
  package org.dom4j.swing;
 
  import java.util.ArrayList;
  import java.util.Enumeration;
  import java.util.List;
 
  import javax.swing.tree.TreeNode;
 
  import org.dom4j.Branch;
  import org.dom4j.CharacterData;
  import org.dom4j.Node;
 
  /** <p><code>BranchTreeNode</code> implements the Swing TreeNode interface
    * to bind dom4j XML Branch nodes (i.e. Document and Element nodes) to a Swing TreeModel.</p>
    *
    * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> (james.strachan@metastuff.com)
    * @author Jakob Jenkov
    * @version $Revision: 1.8 $ 
    */
  public class BranchTreeNode extends LeafTreeNode {
 
      /** Stores the child tree nodes */
      protected List children;
 
      
0x       public BranchTreeNode() {
0x       }
      
      public BranchTreeNode(Branch xmlNode) {
0x           super(xmlNode);
0x       }
      
      public BranchTreeNode(TreeNode parent, Branch xmlNode) {
0x           super( parent, xmlNode );
0x       }
      
 
      // TreeNode methods
      //-------------------------------------------------------------------------                
      public Enumeration children() {
0x           return new Enumeration() {
              int index = -1;
              
              public boolean hasMoreElements() {
                  return index + 1 < getChildCount();
              }
              
              public Object nextElement() {
                  return getChildAt( ++index );
              }
          };
      }
      
      public boolean getAllowsChildren() {
0x           return true;
      }
      
      public TreeNode getChildAt(int childIndex) {
0x           return (TreeNode) getChildList().get(childIndex);
      }
      
      public int getChildCount() {
0x           return getChildList().size();
      }
      
      public int getIndex(TreeNode node) {
0x           return getChildList().indexOf(node);
      }
      
      public boolean isLeaf() {
0/2 0x           return getXmlBranch().nodeCount() <= 0;
      }
      
      public String toString() {
0x           return xmlNode.getName();
      }
 
      
      // Implementation methods
      //-------------------------------------------------------------------------                
      
      /** Uses Lazy Initialization pattern to create a List of children */
      protected List getChildList() {
          // for now lets just create the children once, the first time they 
          // are asked for.
          // XXXX - we may wish to detect inconsistencies here....
0/2 0x           if ( children == null ) {
0x               children = createChildList();
          }
0x           return children;
      }
      
      
      /** Factory method to create List of children TreeNodes */
      protected List createChildList() {
          // add attributes and content as children?
0x           Branch branch = getXmlBranch();
0x           int size = branch.nodeCount();
0x           List children = new ArrayList( size );
0/2 0x           for ( int i = 0; i < size; i++ ) {
0x               Node node = branch.node(i);
              
              // ignore whitespace text nodes
0/2 0x               if ( node instanceof CharacterData ) {
0x                   String text = node.getText();
0/2 0x                   if ( text == null ) {
0x                       continue;
                  }
0x                   text = text.trim();
0/2 0x                   if ( text.length() <= 0 ) {
0x                       continue;
                  }
              }
0x               children.add( createChildTreeNode( node ) );
          }
0x           return children;
      }
 
      /** Factory method to create child tree nodes for a given XML node type
        */
      protected TreeNode createChildTreeNode( Node xmlNode ) {
0/2 0x           if ( xmlNode instanceof Branch ) {
0x               return new BranchTreeNode( this, (Branch) xmlNode );
          }
          else { 
0x               return new LeafTreeNode( this, xmlNode );
          }
              
      }
      protected Branch getXmlBranch() {
0x           return (Branch) xmlNode;
      }
  }
 
 
 
 
  /*
   * Redistribution and use of this software and associated documentation
   * ("Software"), with or without modification, are permitted provided
   * that the following conditions are met:
   *
   * 1. Redistributions of source code must retain copyright
   *    statements and notices.  Redistributions must also contain a
   *    copy of this document.
   *
   * 2. Redistributions in binary form must reproduce the
   *    above copyright notice, this list of conditions and the
   *    following disclaimer in the documentation and/or other
   *    materials provided with the distribution.
   *
   * 3. The name "DOM4J" must not be used to endorse or promote
   *    products derived from this Software without prior written
   *    permission of MetaStuff, Ltd.  For written permission,
   *    please contact dom4j-info@metastuff.com.
   *
   * 4. Products derived from this Software may not be called "DOM4J"
   *    nor may "DOM4J" appear in their names without prior written
   *    permission of MetaStuff, Ltd. DOM4J is a registered
   *    trademark of MetaStuff, Ltd.
   *
   * 5. Due credit should be given to the DOM4J Project - 
   *    http://www.dom4j.org
   *
   * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
   * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   * OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
   *
   * $Id: BranchTreeNode.java,v 1.8 2004/06/25 08:03:40 maartenc Exp $
   */