/* * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved. * * This software is open source. * See the bottom of this file for the licence. */ package view.swing; import java.util.ArrayList; import java.util.Collections; 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; /** *

* Node4DOM implémente l'interface Swing TreeNode * afin de faire le lien entre les noeuds XML DOM4J et un * TreeModel Swing *

*

* Il s'agit d'une fusion des classes BranchTreeNode et * LeafTreeNode permetant une mise à jour de l'affichage du * JTree lors d'une suppression via l'ajout des methodes * removeFromParent() et remove() *

* * @author James Strachan * @author Jakob Jenkov * @author OsoPardo */ public class Node4DOM implements TreeNode{ /** * Parent du noeud */ protected Node4DOM parent; /** * Fils du noeuds */ protected List children; /** * Noeud Node de DOM4J encapsulé */ protected Node xmlNode; public Node4DOM() { } /** * Constructeur prenant en parametre un noeud DOM4J * * @param xmlNode Noeud Node de DOM4J */ public Node4DOM( Node xmlNode ) { this.xmlNode = xmlNode; } /** * Constructeur prenant en parametre le parent du noeud et un * noeud DOM4J * * @param parent Parent de ce noeud * @param xmlNode Noeud Node de DOM4J */ public Node4DOM( Node4DOM parent, Node xmlNode ){ this.parent = parent; this.xmlNode = xmlNode; } /** * @see TreeNode#getChildAt(int) */ public TreeNode getChildAt( int index ) { return (TreeNode)getChildList().get( index ); } /** * @see TreeNode#getChildCount() */ public int getChildCount() { return getChildList().size(); } /** * @see TreeNode#getParent() */ public TreeNode getParent() { return parent; } /** * @see TreeNode#getIndex(javax.swing.tree.TreeNode) */ public int getIndex( TreeNode node ) { if( node == null ) throw new IllegalArgumentException( "Null 'node' argument."); return children.indexOf( node ); } /** * @see TreeNode#getAllowsChildren() */ public boolean getAllowsChildren() { return ( xmlNode instanceof Branch ); } /** * @see TreeNode#isLeaf() */ public boolean isLeaf() { return getChildList().size() <= 0; } /** * @see TreeNode#children() */ public Enumeration children() { return Collections.enumeration( children ); } /** * Fixe le parent du noeud * * @param parent Parent du noeud */ public void setParent( Node4DOM parent ){ this.parent = parent; } @Override public String toString() { // Si le noeud est une branche if( xmlNode instanceof Branch ){ return xmlNode.getName(); } // Si le noeud est une feuille else{ String text = xmlNode.getText(); return ( text != null ) ? text.trim() : ""; } } /** * Donne le noeud Node de DOM4J encapsulé * @return Noeud Node de DOM4J encapsulé */ public Node getXmlNode(){ return xmlNode; } /** * Donne la liste des fils du noeud avec une creation a retardement * (Pattern Lazy Loading) * * @return Liste des fils du noeud */ protected List getChildList(){ if ( children == null ) children = createChildList(); return children; } /** * Creation de la liste des fils du noeud, reflet des fils du noeud * xmlNode encapsulé * * @return Liste des fils du noeud */ private List createChildList() { // Si le noeud encapsulé n'est pas une branche, pas de fils if( ! ( xmlNode instanceof Branch ) ) return new ArrayList(0); Branch branch = getXmlBranch(); int size = branch.nodeCount(); List childList = new ArrayList(size); for (int i = 0; i < size; i++) { Node node = branch.node(i); if (node instanceof CharacterData) { String text = node.getText(); if (text == null) { continue; } text = text.trim(); if (text.length() <= 0) { continue; } } childList.add( new Node4DOM( this, node ) ); } return childList; } protected Branch getXmlBranch() { return (Branch) xmlNode; } /** * Supprime le noeud à la vue de son père * */ public void removeFromParent(){ parent.remove( this ); } /** * Supprime un des fils du noeud * * @param node Noeud à supprimer */ public void remove( Node4DOM node ){ if( node == null ) throw new IllegalArgumentException( "Null 'node' argument." ); if( node.getParent() != this ) throw new IllegalArgumentException( "The given 'node' is not a child of this node." ); children.remove( node ); node.setParent( null ); } } /* * 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-2005 (C) MetaStuff, Ltd. All Rights Reserved. */