Author: glandais Date: 2008-03-08 00:25:39 +0000 (Sat, 08 Mar 2008) New Revision: 1304 Added: trunk/simexplorer-is/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/entities/EntityVisitorTreeNode.java Log: Share UI node build Added: trunk/simexplorer-is/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/entities/EntityVisitorTreeNode.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/entities/EntityVisitorTreeNode.java (rev 0) +++ trunk/simexplorer-is/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/entities/EntityVisitorTreeNode.java 2008-03-08 00:25:39 UTC (rev 1304) @@ -0,0 +1,77 @@ +/* +* ##% Copyright (C) 2008 Code Lutin, 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.entities; + +import java.util.ArrayDeque; +import java.util.Deque; + +import fr.cemagref.simexplorer.is.entities.data.LoggableElement; + +public abstract class EntityVisitorTreeNode<TN extends Object> implements EntityVisitor { + + protected abstract TN createNode(Entity v, boolean allowedChildren); + + private TN rootNode; + + /** The node stack. */ + private Deque<TN> nodeStack; + + public TN treeNodeVisit(LoggableElement v) { + nodeStack = new ArrayDeque<TN>(); + visit(v); + return rootNode; + } + + protected TN getCurrentParent() { + return nodeStack.peekFirst(); + } + + @Override + public void enterComposite(Composite v) { + TN newNode; + newNode = createNode(v, true); + if (rootNode == null) { + rootNode = newNode; + } + nodeStack.addFirst(newNode); + } + + @Override + public void exitComposite(Composite v) { + nodeStack.removeFirst(); + } + + @Override + public void visit(EntityVisitable v) { + if (v != null) { + // on lance la visite + v.accept(this); + } + } + + @Override + public void visitComposite(Composite v) { + createNode(v, true); + } + + @Override + public void visitLeaf(Leaf v) { + createNode(v, false); + } + +}