Author: tchemit Date: 2008-01-23 16:16:27 +0000 (Wed, 23 Jan 2008) New Revision: 444 Added: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/model/LoggableElementTreeNode.java Log: ajout du model de Node pour les LoggableElement Added: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/model/LoggableElementTreeNode.java =================================================================== --- trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/model/LoggableElementTreeNode.java (rev 0) +++ trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/model/LoggableElementTreeNode.java 2008-01-23 16:16:27 UTC (rev 444) @@ -0,0 +1,152 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin, +* Tony Chemit, Gabriel Landais +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* ##% */ +package fr.cemagref.simexplorer.is.ui.swing.model; + +import fr.cemagref.simexplorer.is.entities.data.LoggableElement; + +import javax.swing.JTree; +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.TreeNode; +import javax.swing.tree.TreePath; +import java.util.Arrays; +import java.util.Enumeration; +import java.util.LinkedList; +import java.util.Queue; + +/** + * Le TreeNode de base pour afficher des LoggableElement dans un arbre. + * + * @author chemit + */ +public class LoggableElementTreeNode implements TreeNode { + + protected LoggableElementTreeNode parent; + + protected LoggableElementTreeNode[] childs; + + protected boolean allowedChildren; + + protected LoggableElement userObject; + + public LoggableElementTreeNode(LoggableElementTreeNode parent, LoggableElement root, boolean allowedChildren) { + this.parent = parent; + this.allowedChildren = allowedChildren; + this.userObject = root; + } + + + public void collaspeAll(JTree tree) { + if (isLeaf()) { + return; + } + for (LoggableElementTreeNode child : childs) { + child.collaspeAll(tree); + } + tree.collapsePath(new TreePath(getPath())); + } + + public void expandAll(JTree tree) { + if (isLeaf()) { + return; + } + tree.expandPath(new TreePath(getPath())); + for (LoggableElementTreeNode child : childs) { + child.expandAll(tree); + } + } + + public TreeNode getChildAt(int childIndex) { + if (childIndex < 0 || childIndex > getChildCount()) { + return null; + } + return childs[childIndex]; + } + + @Override + public int getChildCount() { + return childs == null ? 0 : childs.length; + } + + public TreeNode getParent() { + return parent; + } + + public int getIndex(TreeNode node) { + if (getChildCount() == 0) { + return -1; + } + for (int i = 0; i < childs.length; i++) { + LoggableElementTreeNode child = childs[i]; + if (node.equals(child)) { + return i; + } + } + return -1; + } + + public boolean getAllowsChildren() { + return false; + } + + @Override + public boolean isLeaf() { + return getChildCount() == 0; + } + + public Enumeration children() { + return childs == null ? DefaultMutableTreeNode.EMPTY_ENUMERATION : new MyEnumeration<LoggableElementTreeNode>(childs); + } + + public TreeNode[] getPath() { + return getPathToRoot(this, 0); + } + + + protected TreeNode[] getPathToRoot(TreeNode aNode, int depth) { + TreeNode[] retNodes; + if (aNode == null) { + if (depth == 0) + return null; + else + retNodes = new TreeNode[depth]; + } else { + depth++; + retNodes = getPathToRoot(aNode.getParent(), depth); + retNodes[retNodes.length - depth] = aNode; + } + return retNodes; + } + + class MyEnumeration<T> implements Enumeration<T> { + + Queue<T> queue; + + MyEnumeration(T[] queue) { + this.queue = new LinkedList<T>(Arrays.asList(queue)); + } + + public boolean hasMoreElements() { + return !queue.isEmpty(); + } + + public T nextElement() { + return queue.poll(); + } + } +}