Subversion Repositories eFlore/Applications.cel

Compare Revisions

Ignore whitespace Rev 2171 → Rev 2172

/branches/v2.2-faucille/src/org/tela_botanica/client/util/MotsClesUtilitaire.java
New file
0,0 → 1,158
package org.tela_botanica.client.util;
 
import java.util.HashMap;
 
import org.tela_botanica.client.cel2;
 
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.Window;
import com.gwtext.client.data.Node;
import com.gwtext.client.data.Tree;
import com.gwtext.client.widgets.tree.TreeNode;
 
public class MotsClesUtilitaire {
//TODO rassembler tout ce qui construit les arbres de mots clés ici
private static String[] caracteresInterdits = {"\\", "/", ","};
public static String obtenirCheminParent(String cheminMotCle, String motCle) {
return cheminMotCle.substring(0, cheminMotCle.length() - motCle.length() -1).toLowerCase();
}
 
public static Object[] construireArbre(String NomRacine, String idRacine,
JSONArray arbreJson, boolean afficherCheckbox) {
Tree arbreMotsCles = new Tree() ;
HashMap<String, String> motsCles = new HashMap<String, String>(0);
HashMap<String, String> parentes = new HashMap<String, String>();
final int taillemax = arbreJson.size();
 
// on crée un arbre vide
TreeNode root = new TreeNode();
root.setId(idRacine);
root.setText(NomRacine);
String[] usObjRacine = {NomRacine, idRacine};
root.setUserObject(usObjRacine);
arbreMotsCles.setRootNode(root);
// le mot clé racine possède le chemin racine ("/")
parentes.put("/", idRacine);
 
// pour chacun des élements du tableau
for (int j = 0; j < taillemax; j++) {
// on extrait les élements du tableau
if (arbreJson.get(j).isObject() != null) {
JSONObject noeud = (JSONObject) arbreJson.get(j);
 
String idMotCle = noeud.get("id_mot_cle")
.isString().stringValue();
String motCle = noeud.get("mot_cle").isString()
.stringValue();
// le chemin est mis en minuscule afin de fusionner toutes
// la variantes minuscules majuscule d'un mot clé
// qui peuvent poser problème
String chemin = noeud.get("chemin").isString()
.stringValue().toLowerCase();
String cheminParent = MotsClesUtilitaire.obtenirCheminParent(chemin, motCle);
String parent = parentes.get(cheminParent);
String[] usObj = {motCle, idMotCle};
 
// et on construit l'arbre en ajoutant les noeuds un à un (qui sont renvoyés
// dans l'ordre hierarchique de leur niveau
// ce qui permet de les traiter séquentiellement)
TreeNode node = new TreeNode();
node.setId(idMotCle);
node.setText(motCle);
if(afficherCheckbox) {
node.setChecked(false);
}
 
Node parentNode = arbreMotsCles.getNodeById(parent);
node.setUserObject(usObj);
parentNode.appendChild(node);
parentes.put(chemin, idMotCle);
motsCles.put(idMotCle, motCle);
}
}
Object[] retour = {arbreMotsCles, motsCles};
return retour;
}
 
/**
* Fonction récursive qui prend deux noeuds d'arbre en paramètre et crée un
* copie du sous arbre du premier noeud, qu'elle concatène au deuxième
*
* @param ndPereOriginal
* le père des noeuds de l'arbre original
* @param ndPereCopie
* le père qui va recevoir les copies
*/
public static void copierFilsNoeud(Node ndPereOriginal, TreeNode ndPereCopie, boolean afficherCheckbox) {
if (ndPereCopie != null && ndPereOriginal != null) {
Node[] ndNodeFils = ndPereOriginal.getChildNodes();
for (int i = 0; i < ndNodeFils.length; i++) {
String[] usObj = (String[]) ndNodeFils[i].getUserObject();
TreeNode child = new TreeNode(usObj[0]);
child.setId(usObj[1]);
if(afficherCheckbox) {
child.setChecked(false);
}
child.setUserObject(usObj);
ndPereCopie.appendChild(child);
 
if (!ndNodeFils[i].isLeaf()) {
copierFilsNoeud(ndNodeFils[i], child, afficherCheckbox);
}
}
}
}
public static TreeNode ajouterNoeud(TreeNode parent, boolean afficherCheckbox) {
// on crée un nouveau noeud vide
TreeNode nd = new TreeNode("");
nd.setCls("x-view-treenode-keyword");
if(afficherCheckbox) {
nd.setChecked(true);
}
// on associe un objet au noeud qui contient des infos
String[] usObject = new String[2];
// l'objet contient le nom du noeud
usObject[0] = "";
usObject[1] = "";
nd.setUserObject(usObject);
String cheminTemporaireAjout = parent.getPath()+"/"+nd.getId();
nd.setId(cheminTemporaireAjout);
return nd;
}
public static String getChaineCaracteresInterdits() {
String interdits = "";
for (int i = 0; i < MotsClesUtilitaire.caracteresInterdits.length; i++) {
interdits += MotsClesUtilitaire.caracteresInterdits[i]+" ";
}
return interdits;
}
public static boolean estUnMotCleAutorise(String motCle) {
boolean valide = !motCle.trim().isEmpty();
for (int i = 0; i < MotsClesUtilitaire.caracteresInterdits.length; i++) {
if(motCle.indexOf(MotsClesUtilitaire.caracteresInterdits[i]) != -1) {
valide = false;
break;
}
}
return valide;
}
}