r1015 - trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/entities
Author: tchemit Date: 2008-02-16 14:54:21 +0000 (Sat, 16 Feb 2008) New Revision: 1015 Added: trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/entities/EntityHelper.java Log: ajout d'un helper avec deux enums : une pour definir les libelles des Entities (Type) et une autre pour definir le mapping des actions sur les loggableElement (Action) Added: trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/entities/EntityHelper.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/entities/EntityHelper.java (rev 0) +++ trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/entities/EntityHelper.java 2008-02-16 14:54:21 UTC (rev 1015) @@ -0,0 +1,161 @@ +/* +* ##% Copyright (C) 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.entities; + +import fr.cemagref.simexplorer.is.entities.data.Component; +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.metadata.MetaData; +import static org.codelutin.i18n.I18n._; +import static org.codelutin.i18n.I18n.n_; + +import javax.swing.tree.DefaultMutableTreeNode; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * Une classe utilitaires pour les Entities du projet. + * + * @author chemit + */ +public class EntityHelper { + /** + * Une enum pour definir les differents typed'entities rencontres dans le projet. + * <p/> + * On ajoute ici une clef i18n pour pouvoir traduire plus tard ces types. + * <p/> + * Note: on n'utilise pas directement les traductions pour permettre le mécanisme + * de changement de langue (voir module swing et web). + * + * @author chemit + */ + public static enum Type { + Attachment(n_("simexplorer.common.attachment"), n_("simexplorer.common.attachments")), + Code(n_("simexplorer.common.code"), n_("simexplorer.common.codes")), + Component(n_("simexplorer.common.component"), n_("simexplorer.common.components")), + Constant(n_("simexplorer.common.constant"), n_("simexplorer.common.constants")), + ConstantValue(n_("simexplorer.common.constantvalue"), n_("simexplorer.common.constantvalues")), + ExplorationApplication(n_("simexplorer.common.explorationapplication"), n_("simexplorer.common.explorationapplications")), + ExplorationData(n_("simexplorer.common.explorationdata"), n_("simexplorer.common.explorationdatas")), + Library(n_("simexplorer.common.library"), n_("simexplorer.common.libraries")), + Repository(n_("simexplorer.common.repository"), n_("simexplorer.common.repositories")), + Result(n_("simexplorer.common.result"), n_("simexplorer.common.results")), + Structure(n_("simexplorer.common.structure"), n_("simexplorer.common.structures")); + + private String i18nKey; + private String i18nKeys; + + public static String getLibelle(Object entity) { + if (entity == null) { + return ""; + } + if (entity instanceof LoggableElement) { + return getLibelle((LoggableElement) entity); + } + return getLibelle(entity.getClass().getSimpleName()); + } + + public static String getLibelle(LoggableElement element) { + return element == null ? "" : getLibelle(element.getMetaData()); + } + + public static String getLibelle(MetaData data) { + return data == null ? "" : getLibelle(data.getType()); + } + + public String getLibelle() { + return _(i18nKey); + } + + public String getLibelles() { + return _(i18nKeys); + } + + protected static String getLibelle(String type) { + if (type != null) { + try { + Type type1 = Type.valueOf(type); + return type1.getLibelle(); + } catch (IllegalArgumentException e) { + //TODO log it + System.err.println(e.getMessage()); + } + } + return ""; + } + + private Type(String i18nKey, String i18nKeys) { + this.i18nKey = i18nKey; + this.i18nKeys = i18nKeys; + } + } + + /** + * Une énumération pour définir les actions possibles sur les entités du + * projet. + * + * @author chemit + */ + public static enum Action { + + DOWNLOAD(ExplorationApplication.class, Result.class, Library.class), + EXPORT(ExplorationApplication.class, Result.class, Library.class), + IMPORT(ExplorationApplication.class, Library.class), + DELETE(ExplorationApplication.class, ExplorationData.class, Component.class); + + private List<Class<?>> classes; + private List<String> types; + + public boolean accept(DefaultMutableTreeNode node) { + return node != null && node.getUserObject() != null && + accept(node.getUserObject().getClass()); + } + + public boolean accept(LoggableElement sNode) { + return sNode != null && accept(sNode.getClass()); + } + + public boolean accept(MetaData metaData) { + return metaData != null && accept(metaData.getType()); + } + + private Action(Class<?>... classes) { + this.classes = Arrays.asList(classes); + this.types = new ArrayList<String>(classes.length); + for (Class<?> aClass : classes) { + this.types.add(aClass.getSimpleName()); + } + } + + private boolean accept(Class<?> klass) { + return classes.contains(klass); + } + + private boolean accept(String type) { + return types.contains(type); + } + } + + protected EntityHelper() { + // no instance + } +}
participants (1)
-
tchemit@users.labs.libre-entreprise.org