Author: tchemit Date: 2008-01-23 21:24:03 +0000 (Wed, 23 Jan 2008) New Revision: 453 Added: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/model/LoggableElementTreeHelper.java Log: helper pour construire l'arbre Added: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/model/LoggableElementTreeHelper.java =================================================================== --- trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/model/LoggableElementTreeHelper.java (rev 0) +++ trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/model/LoggableElementTreeHelper.java 2008-01-23 21:24:03 UTC (rev 453) @@ -0,0 +1,193 @@ +/* +* \#\#% 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.Code; +import fr.cemagref.simexplorer.is.entities.data.Component; +import fr.cemagref.simexplorer.is.entities.data.Constant; +import fr.cemagref.simexplorer.is.entities.data.ConstantValue; +import fr.cemagref.simexplorer.is.entities.data.ExplorationApplication; +import fr.cemagref.simexplorer.is.entities.data.ExplorationData; +import fr.cemagref.simexplorer.is.entities.data.Library; +import fr.cemagref.simexplorer.is.entities.data.LoggableElement; +import fr.cemagref.simexplorer.is.entities.data.Result; +import fr.cemagref.simexplorer.is.entities.data.Structure; +import fr.cemagref.simexplorer.is.ui.SimExplorerRuntimeException; +import static org.codelutin.i18n.I18n._; + +import javax.swing.tree.TreeNode; + +/** + * Une classe d'utilitaire pour construire les arbres de LoggableElement + * + * @author chemit + * @see LoggableElementTreeNode + */ +public class LoggableElementTreeHelper { + + public enum TypeNode { + explorationApplication(ExplorationApplication.class), + explorationData(ExplorationData.class), + component(Component.class), + result(Result.class), + library(Library.class); + + private Class<?> klass; + + TypeNode(Class<?> aClass) { + this.klass = aClass; + } + + public Class<?> getKlass() { + return klass; + } + } + + public static TypeNode getTypeNode(LoggableElementTreeNode node) { + Object userObject = node.getUserObject(); + if (userObject instanceof String) { + // do not treate string + return null; + } + for (TypeNode typeNode : TypeNode.values()) { + if (typeNode.getKlass() == node.getClass()) { + return typeNode; + } + } + return null; + } + + public static TreeNode buildLoggableElementNode(LoggableElement element) { + if (element instanceof ExplorationApplication) { + return buildExplorationApplicationNode(null, (ExplorationApplication) element); + } + if (element instanceof Component) { + return buildComponentNode(null, (Component) element); + } + if (element instanceof ExplorationData) { + return buildExplorationDataNode(null, (ExplorationData) element); + } + throw new SimExplorerRuntimeException("could not foud a builder for " + element); + + } + + public static TreeNode buildExplorationApplicationNode(LoggableElementTreeNode parent, ExplorationApplication element) { + LoggableElementTreeNode result = new LoggableElementTreeNode(parent, element, true); + LoggableElementTreeNode tmpNode; + // build components node and his childs + tmpNode = new LoggableElementTreeNode(result, _("simexplorer.node.components"), true); + for (Component component : element.getComponents()) { + buildComponentNode(tmpNode, component); + } + + // build explorations node and his childs + tmpNode = new LoggableElementTreeNode(result, _("simexplorer.node.explorations"), true); + for (ExplorationData explorationData : element.getExplorations()) { + buildExplorationDataNode(tmpNode, explorationData); + } + return result; + } + + public static TreeNode buildComponentNode(LoggableElementTreeNode parent, Component element) { + + LoggableElementTreeNode result = new LoggableElementTreeNode(parent, element, true); + LoggableElementTreeNode tmpNode; + + // build constantes nodes + tmpNode = new LoggableElementTreeNode(result, _("simexplorer.node.constants"), true); + for (Constant constante : element.getConstants()) { + buildConstantNode(tmpNode, constante); + } + + // build structures nodes + tmpNode = new LoggableElementTreeNode(result, _("simexplorer.node.structures"), true); + for (Structure structure : element.getStructures()) { + buildStructureNode(tmpNode, structure); + } + + // build Codes nodes + tmpNode = new LoggableElementTreeNode(result, _("simexplorer.node.codes"), true); + for (Code code : element.getCodes()) { + buildCodeNode(tmpNode, code); + } + + // build libraries nodes + tmpNode = new LoggableElementTreeNode(result, _("simexplorer.node.libraries"), true); + for (Library library : element.getLibraries()) { + buildLibraryNode(tmpNode, library); + } + + return result; + } + + public static LoggableElementTreeNode buildExplorationDataNode(LoggableElementTreeNode parent, ExplorationData element) { + LoggableElementTreeNode result = new LoggableElementTreeNode(parent, element, true); + LoggableElementTreeNode tmpNode; + // build constant values nodes + tmpNode = new LoggableElementTreeNode(result, _("simexplorer.node.constantvalues"), true); + for (ConstantValue library : element.getValuesMap()) { + buildConstantValueNode(tmpNode, library); + } + // build result node + buildResultNode(result, element.getResult()); + return result; + } + + public static TreeNode buildLibraryNode(LoggableElementTreeNode parent, Library element) { + LoggableElementTreeNode result; + result = new LoggableElementTreeNode(parent, element, false); + return result; + } + + public static TreeNode buildCodeNode(LoggableElementTreeNode parent, Code element) { + LoggableElementTreeNode result; + result = new LoggableElementTreeNode(parent, element, false); + return result; + } + + public static TreeNode buildStructureNode(LoggableElementTreeNode parent, Structure element) { + LoggableElementTreeNode result; + result = new LoggableElementTreeNode(parent, element, false); + return result; + } + + public static TreeNode buildConstantNode(LoggableElementTreeNode parent, Constant element) { + LoggableElementTreeNode result; + result = new LoggableElementTreeNode(parent, element, false); + return result; + } + + + public static TreeNode buildConstantValueNode(LoggableElementTreeNode parent, ConstantValue element) { + LoggableElementTreeNode result; + result = new LoggableElementTreeNode(parent, element.getConstant().getName(), true); + result.add(new LoggableElementTreeNode(result, element.getValue(), false)); + return result; + } + + public static TreeNode buildResultNode(LoggableElementTreeNode parent, Result element) { + LoggableElementTreeNode result; + result = new LoggableElementTreeNode(parent, element, false); + return result; + } + + protected LoggableElementTreeHelper() { + // no instance please + } +}