LeafTreeNode xref

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.Enumeration;
    11
    12  import javax.swing.tree.TreeNode;
    13
    14  import org.dom4j.Node;
    15
    16  /***
    17   * <p>
    18   * <code>LeafTreeNode</code> implements the Swing TreeNode interface to bind a
    19   * leaf XML nodes to a Swing TreeModel.
    20   * </p>
    21   *
    22   * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
    23   * @author Jakob Jenkov
    24   * @version $Revision: 1.7 $
    25   */
    26  public class LeafTreeNode implements TreeNode {
    27      protected static final Enumeration EMPTY_ENUMERATION = new Enumeration() {
    28          public boolean hasMoreElements() {
    29              return false;
    30          }
    31
    32          public Object nextElement() {
    33              return null;
    34          }
    35      };
    36
    37      /*** The parent node of this TreeNode */
    38      private TreeNode parent;
    39
    40      /*** The dom4j Node which contains the */
    41      protected Node xmlNode;
    42
    43      public LeafTreeNode() {
    44      }
    45
    46      public LeafTreeNode(Node xmlNode) {
    47          this.xmlNode = xmlNode;
    48      }
    49
    50      public LeafTreeNode(TreeNode parent, Node xmlNode) {
    51          this.parent = parent;
    52          this.xmlNode = xmlNode;
    53      }
    54
    55      // TreeNode methods
    56      // -------------------------------------------------------------------------
    57      public Enumeration children() {
    58          return EMPTY_ENUMERATION;
    59      }
    60
    61      public boolean getAllowsChildren() {
    62          return false;
    63      }
    64
    65      public TreeNode getChildAt(int childIndex) {
    66          return null;
    67      }
    68
    69      public int getChildCount() {
    70          return 0;
    71      }
    72
    73      public int getIndex(TreeNode node) {
    74          return -1;
    75      }
    76
    77      public TreeNode getParent() {
    78          return parent;
    79      }
    80
    81      public boolean isLeaf() {
    82          return true;
    83      }
    84
    85      public String toString() {
    86          // should maybe do things differently based on content?
    87          String text = xmlNode.getText();
    88
    89          return (text != null) ? text.trim() : "";
    90      }
    91
    92      // Properties
    93      // -------------------------------------------------------------------------
    94
    95      /***
    96       * Sets the parent of this node but doesn't change the parents children
    97       *
    98       * @param parent
    99       *            DOCUMENT ME!
    100      */
    101     public void setParent(LeafTreeNode parent) {
    102         this.parent = parent;
    103     }
    104
    105     public Node getXmlNode() {
    106         return xmlNode;
    107     }
    108 }
    109
    110 /*
    111  * Redistribution and use of this software and associated documentation
    112  * ("Software"), with or without modification, are permitted provided that the
    113  * following conditions are met:
    114  *
    115  * 1. Redistributions of source code must retain copyright statements and
    116  * notices. Redistributions must also contain a copy of this document.
    117  *
    118  * 2. Redistributions in binary form must reproduce the above copyright notice,
    119  * this list of conditions and the following disclaimer in the documentation
    120  * and/or other materials provided with the distribution.
    121  *
    122  * 3. The name "DOM4J" must not be used to endorse or promote products derived
    123  * from this Software without prior written permission of MetaStuff, Ltd. For
    124  * written permission, please contact dom4j-info@metastuff.com.
    125  *
    126  * 4. Products derived from this Software may not be called "DOM4J" nor may
    127  * "DOM4J" appear in their names without prior written permission of MetaStuff,
    128  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    129  *
    130  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    131  *
    132  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    133  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    134  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    135  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    136  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    137  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    138  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    139  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    140  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    141  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    142  * POSSIBILITY OF SUCH DAMAGE.
    143  *
    144  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    145  */