Index: lutini18neditor/src/java/org/codelutin/i18n/editor/model/PropertieNode.java diff -u /dev/null lutini18neditor/src/java/org/codelutin/i18n/editor/model/PropertieNode.java:1.1 --- /dev/null Fri Jan 18 18:15:08 2008 +++ lutini18neditor/src/java/org/codelutin/i18n/editor/model/PropertieNode.java Fri Jan 18 18:15:03 2008 @@ -0,0 +1,93 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin. +* +* 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 org.codelutin.i18n.editor.model; + +import java.io.Serializable; + +import javax.swing.tree.DefaultMutableTreeNode; + +/** + * Représente un noeud de l'ardre des propriétés affichées. Le noeud contient + * le chemin de la propriétés, par exemple tutu.toto.tata va être représenté avec + * 3 noeuds avec comme chemin tutu, tutu.toto, tutu.toto.tata + * + * @author julien + */ +public class PropertieNode extends DefaultMutableTreeNode implements Comparable { + + /** + * @see Serializable + */ + private static final long serialVersionUID = 1L; + + /** + * Consturcteur avec l'objet contenu dans le noeud + * @param userObject chemin de la propriété + */ + public PropertieNode(Object userObject) { + super(userObject); + } + + /* + * (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + public boolean equals(Object obj) { + if(obj instanceof PropertieNode) { + // Comparaison du chemin + PropertieNode node = (PropertieNode) obj; + return getUserObject().equals(node.getUserObject()); + } else { + return false; + } + } + + /* + * (non-Javadoc) + * @see javax.swing.tree.DefaultMutableTreeNode#toString() + */ + public String toString() { + String userObject = getUserObject().toString(); + int lastSeparator = userObject.lastIndexOf("."); + if(lastSeparator == -1) { + // Si c'est la racine + return userObject; + } else { + // Si c'est un chemin + return userObject.substring(lastSeparator + 1); + } + } + + /* + * (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + public int hashCode() { + return getUserObject().hashCode(); + } + + /* + * (non-Javadoc) + * @see java.lang.Comparable#compareTo(java.lang.Object) + */ + public int compareTo(PropertieNode node) { + // Compare les chemins + return getUserObject().toString().compareTo(node.getUserObject().toString()); + } +} Index: lutini18neditor/src/java/org/codelutin/i18n/editor/model/PropertiesTreeModel.java diff -u /dev/null lutini18neditor/src/java/org/codelutin/i18n/editor/model/PropertiesTreeModel.java:1.1 --- /dev/null Fri Jan 18 18:15:08 2008 +++ lutini18neditor/src/java/org/codelutin/i18n/editor/model/PropertiesTreeModel.java Fri Jan 18 18:15:03 2008 @@ -0,0 +1,139 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin. +* +* 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 org.codelutin.i18n.editor.model; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.swing.tree.DefaultTreeModel; + +import org.codelutin.i18n.editor.ManagerI18n; + +/** + * Modèle de l'arbre des propriétés. Le modèle permet de sructurer les propriétés + * (ex: tutu.toto.tata) en un des noeuds de l'arbre. + * + * @author julien + */ +public class PropertiesTreeModel extends DefaultTreeModel { + + /** + * @see Serializable + */ + private static final long serialVersionUID = 1L; + + /** + * Ensemble des noeuds de l'ardre avec ses enfants + */ + protected Map> nodes; + + /** + * Constructeur avec un liste de proprétés + * @param list liste de propriétés + */ + public PropertiesTreeModel(List list) { + super(null); + nodes = new HashMap>(); + + // Création de la racine + PropertieNode root = new PropertieNode(ManagerI18n.ROOT_NAME); + List childrenRoot = new ArrayList(); + nodes.put(root, childrenRoot); + setRoot(root); + + // Création des noeuds + for (String property : list) { + String path = ManagerI18n.ROOT_NAME; + PropertieNode parent = root; + + String[] names = property.split("\\" + ManagerI18n.SEPARATOR); + for (String name : names) { + // Récupération des enfants pour le parent sous forme d'un + // ensemble pour éviter les doublons + List childrenParent = nodes.get(parent); + Set setChildrenParent; + if(childrenParent == null) { + setChildrenParent = new HashSet(); + } else { + setChildrenParent = new HashSet(childrenParent); + } + + // Création ou récupération du noeud enfant + path += ManagerI18n.SEPARATOR + name; + + PropertieNode node = new PropertieNode(path); + List children = nodes.get(node); + nodes.put(node, children); + + // Ajout de l'enfant au parent + setChildrenParent.add(node); + List newChildrenParent = new ArrayList(setChildrenParent); + nodes.put(parent, newChildrenParent); + + // Trie des enfants + Collections.sort(newChildrenParent); + + parent = node; + } + } + } + + /* + * (non-Javadoc) + * @see javax.swing.tree.DefaultTreeModel#getChild(java.lang.Object, int) + */ + @Override + public Object getChild(Object parent, int index) { + // Renvoi l'enfant à une position donnée + PropertieNode treeNode = (PropertieNode) parent; + List children = nodes.get(treeNode); + return children.get(index); + } + + /* + * (non-Javadoc) + * @see javax.swing.tree.DefaultTreeModel#getChildCount(java.lang.Object) + */ + @Override + public int getChildCount(Object parent) { + // Nombre d'enfant pour un parent + PropertieNode treeNode = (PropertieNode) parent; + List children = nodes.get(treeNode); + return children.size(); + } + + /* + * (non-Javadoc) + * @see javax.swing.tree.DefaultTreeModel#isLeaf(java.lang.Object) + */ + @Override + public boolean isLeaf(Object node) { + // Le noeuds est un feuille si il n'a pas d'enfant + PropertieNode treeNode = (PropertieNode) node; + List children = nodes.get(treeNode); + return children == null || children.isEmpty(); + } + +}