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: LeafTreeNode.java,v 1.1 2001/04/10 19:12:08 jstrachan Exp $
008 */
009
010package org.dom4j.swing;
011
012import java.util.Enumeration;
013
014import javax.swing.tree.TreeNode;
015   
016import org.dom4j.Node;
017
018/** <p><code>LeafTreeNode</code> implements the Swing TreeNode interface
019  * to bind a leaf XML nodes to a Swing TreeModel.</p>
020  *
021  * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> (james.strachan@metastuff.com)
022  * @author Jakob Jenkov
023  * @version $Revision: 1.1 $ 
024  */
025public class LeafTreeNode implements TreeNode {
026
027    protected static final Enumeration EMPTY_ENUMERATION = new Enumeration() {
028        public boolean hasMoreElements() {
029            return false;
030        }
031        public Object nextElement() {
032            return null;
033        }
034    };
035
036    /** The parent node of this TreeNode */
037    private TreeNode parent;
038    
039    /** The dom4j Node which contains the */
040    protected Node xmlNode;
041
042    
043    public LeafTreeNode() {
044    }
045    
046    public LeafTreeNode(Node xmlNode) {
047        this.xmlNode = xmlNode;
048    }
049    
050    public LeafTreeNode(TreeNode parent, Node xmlNode) {
051        this.parent = parent;
052        this.xmlNode = xmlNode;
053    }
054    
055
056    // TreeNode methods
057    //-------------------------------------------------------------------------                
058    public Enumeration children() {
059        return EMPTY_ENUMERATION;
060    }
061    
062    public boolean getAllowsChildren() {
063        return false;
064    }
065    
066    public TreeNode getChildAt(int childIndex) {
067        return null;
068    }
069    
070    public int getChildCount() {
071        return 0;
072    }
073    
074    public int getIndex(TreeNode node) {
075        return -1;
076    }
077    
078    public TreeNode getParent() {
079        return parent;
080    }
081    
082    public boolean isLeaf() {
083        return true;
084    }
085    
086    public String toString() {
087        // should maybe do things differently based on content?
088        String text = xmlNode.getText();
089        return (text != null) ? text.trim() : "";
090    }
091    
092    // Properties
093    //-------------------------------------------------------------------------                
094    
095    /** Sets the parent of this node but doesn't change the parents children */
096    public void setParent(LeafTreeNode parent) {
097        this.parent = parent;
098    }
099    
100    public Node getXmlNode() {
101        return xmlNode;
102    }
103}
104
105
106
107
108/*
109 * Redistribution and use of this software and associated documentation
110 * ("Software"), with or without modification, are permitted provided
111 * that the following conditions are met:
112 *
113 * 1. Redistributions of source code must retain copyright
114 *    statements and notices.  Redistributions must also contain a
115 *    copy of this document.
116 *
117 * 2. Redistributions in binary form must reproduce the
118 *    above copyright notice, this list of conditions and the
119 *    following disclaimer in the documentation and/or other
120 *    materials provided with the distribution.
121 *
122 * 3. The name "DOM4J" must not be used to endorse or promote
123 *    products derived from this Software without prior written
124 *    permission of MetaStuff, Ltd.  For written permission,
125 *    please contact dom4j-info@metastuff.com.
126 *
127 * 4. Products derived from this Software may not be called "DOM4J"
128 *    nor may "DOM4J" appear in their names without prior written
129 *    permission of MetaStuff, Ltd. DOM4J is a registered
130 *    trademark of MetaStuff, Ltd.
131 *
132 * 5. Due credit should be given to the DOM4J Project
133 *    (http://dom4j.org/).
134 *
135 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
136 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
137 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
138 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
139 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
140 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
141 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
142 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
143 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
144 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
145 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
146 * OF THE POSSIBILITY OF SUCH DAMAGE.
147 *
148 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
149 *
150 * $Id: LeafTreeNode.java,v 1.1 2001/04/10 19:12:08 jstrachan Exp $
151 */