Subversion Repositories eFlore/Applications.cel

Rev

Rev 2172 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2172 Rev 2195
1
package org.tela_botanica.client.util;
1
package org.tela_botanica.client.util;
2
 
2
 
3
import java.util.HashMap;
3
import java.util.HashMap;
4
 
4
 
5
import org.tela_botanica.client.cel2;
5
import org.tela_botanica.client.cel2;
6
 
6
 
7
import com.google.gwt.json.client.JSONArray;
7
import com.google.gwt.json.client.JSONArray;
8
import com.google.gwt.json.client.JSONObject;
8
import com.google.gwt.json.client.JSONObject;
9
import com.google.gwt.user.client.Window;
9
import com.google.gwt.user.client.Window;
10
import com.gwtext.client.data.Node;
10
import com.gwtext.client.data.Node;
11
import com.gwtext.client.data.Tree;
11
import com.gwtext.client.data.Tree;
12
import com.gwtext.client.widgets.tree.TreeNode;
12
import com.gwtext.client.widgets.tree.TreeNode;
13
 
13
 
14
public class MotsClesUtilitaire {
14
public class MotsClesUtilitaire {
15
	
15
	
16
	//TODO rassembler tout ce qui construit les arbres de mots clés ici
16
	//TODO rassembler tout ce qui construit les arbres de mots clés ici
17
	private static String[] caracteresInterdits = {"\\", "/", ","};
17
	private static String[] caracteresInterdits = {"\\", "/", ","};
18
	
18
	
19
	public static String obtenirCheminParent(String cheminMotCle, String motCle) {
19
	public static String obtenirCheminParent(String cheminMotCle) {
-
 
20
		String[] tabChemin = cheminMotCle.split("/");
-
 
21
		String cheminParent = "/";
-
 
22
		if(tabChemin.length >= 1) {
-
 
23
			for(int i = 1; i < tabChemin.length - 1; i++) {
-
 
24
				cheminParent += tabChemin[i]+"/";
-
 
25
			}
-
 
26
		}
20
		return cheminMotCle.substring(0, cheminMotCle.length() - motCle.length() -1).toLowerCase();
27
		return cheminParent;
21
	}
28
	}
22
 
29
 
23
	public static Object[] construireArbre(String NomRacine, String idRacine,
30
	public static Object[] construireArbre(String NomRacine, String idRacine,
24
			JSONArray arbreJson, boolean afficherCheckbox) {
31
			JSONArray arbreJson, boolean afficherCheckbox) {
25
		
32
		
26
		Tree arbreMotsCles = new Tree() ;	
33
		Tree arbreMotsCles = new Tree() ;	
27
		HashMap<String, String> motsCles = new HashMap<String, String>(0);
34
		HashMap<String, String> motsCles = new HashMap<String, String>(0);
28
		HashMap<String, String> parentes = new HashMap<String, String>();
35
		HashMap<String, String> parentes = new HashMap<String, String>();
29
		
36
		
30
		final int taillemax = arbreJson.size();
37
		final int taillemax = arbreJson.size();
31
 
38
 
32
		// on crée un arbre vide
39
		// on crée un arbre vide
33
		TreeNode root = new TreeNode();
40
		TreeNode root = new TreeNode();
34
		root.setId(idRacine);
41
		root.setId(idRacine);
35
		root.setText(NomRacine);
42
		root.setText(NomRacine);
36
		String[] usObjRacine = {NomRacine, idRacine};
43
		String[] usObjRacine = {NomRacine, idRacine};
37
		root.setUserObject(usObjRacine);
44
		root.setUserObject(usObjRacine);
38
		arbreMotsCles.setRootNode(root);
45
		arbreMotsCles.setRootNode(root);
39
		
46
		
40
		// le mot clé racine possède le chemin racine ("/")
47
		// le mot clé racine possède le chemin racine ("/")
41
		parentes.put("/", idRacine);
48
		parentes.put("/", idRacine);
42
 
49
 
43
		// pour chacun des élements du tableau
50
		// pour chacun des élements du tableau
44
		for (int j = 0; j < taillemax; j++) {
51
		for (int j = 0; j < taillemax; j++) {
45
			// on extrait les élements du tableau
52
			// on extrait les élements du tableau
46
			if (arbreJson.get(j).isObject() != null) {
53
			if (arbreJson.get(j).isObject() != null) {
47
				JSONObject noeud = (JSONObject) arbreJson.get(j);
54
				JSONObject noeud = (JSONObject) arbreJson.get(j);
48
 
55
 
49
				String idMotCle = noeud.get("id_mot_cle")
56
				String idMotCle = noeud.get("id_mot_cle")
50
						.isString().stringValue();
57
						.isString().stringValue();
51
				String motCle = noeud.get("mot_cle").isString()
58
				String motCle = noeud.get("mot_cle").isString()
52
						.stringValue();
59
						.stringValue();
53
				
60
				
54
				// le chemin est mis en minuscule afin de fusionner toutes 
61
				// le chemin est mis en minuscule afin de fusionner toutes 
55
				// la variantes minuscules majuscule d'un mot clé
62
				// la variantes minuscules majuscule d'un mot clé
56
				// qui peuvent poser problème
63
				// qui peuvent poser problème
57
				String chemin = noeud.get("chemin").isString()
64
				String chemin = noeud.get("chemin").isString()
58
						.stringValue().toLowerCase();
65
						.stringValue().toLowerCase();
59
				
66
				
60
				String cheminParent = MotsClesUtilitaire.obtenirCheminParent(chemin, motCle);
67
				String cheminParent = MotsClesUtilitaire.obtenirCheminParent(chemin);
61
				String parent = parentes.get(cheminParent);
68
				String parent = parentes.get(cheminParent);
62
				
69
				
63
				String[] usObj = {motCle, idMotCle};
70
				String[] usObj = {motCle, idMotCle};
64
 
71
 
65
				// et on construit l'arbre en ajoutant les noeuds un à un (qui sont renvoyés
72
				// et on construit l'arbre en ajoutant les noeuds un à un (qui sont renvoyés
66
				// dans l'ordre hierarchique de leur niveau
73
				// dans l'ordre hierarchique de leur niveau
67
				// ce qui permet de les traiter séquentiellement)
74
				// ce qui permet de les traiter séquentiellement)
68
				TreeNode node = new TreeNode();
75
				TreeNode node = new TreeNode();
69
				node.setId(idMotCle);
76
				node.setId(idMotCle);
70
				node.setText(motCle);
77
				node.setText(motCle);
71
				if(afficherCheckbox) {
78
				if(afficherCheckbox) {
72
					node.setChecked(false);
79
					node.setChecked(false);
73
				}
80
				}
74
 
81
 
75
				Node parentNode = arbreMotsCles.getNodeById(parent);
82
				Node parentNode = arbreMotsCles.getNodeById(parent);
76
				node.setUserObject(usObj);
83
				node.setUserObject(usObj);
77
				parentNode.appendChild(node);
84
				parentNode.appendChild(node);
78
				
85
				
79
				parentes.put(chemin, idMotCle);
86
				parentes.put(chemin, idMotCle);
80
				motsCles.put(idMotCle, motCle);
87
				motsCles.put(idMotCle, motCle);
81
			}
88
			}
82
		}
89
		}
83
		Object[] retour = {arbreMotsCles, motsCles};
90
		Object[] retour = {arbreMotsCles, motsCles};
84
		return retour;
91
		return retour;
85
	}
92
	}
86
 
93
 
87
	/**
94
	/**
88
	 * Fonction récursive qui prend deux noeuds d'arbre en paramètre et crée un
95
	 * Fonction récursive qui prend deux noeuds d'arbre en paramètre et crée un
89
	 * copie du sous arbre du premier noeud, qu'elle concatène au deuxième
96
	 * copie du sous arbre du premier noeud, qu'elle concatène au deuxième
90
	 * 
97
	 * 
91
	 * @param ndPereOriginal
98
	 * @param ndPereOriginal
92
	 *            le père des noeuds de l'arbre original
99
	 *            le père des noeuds de l'arbre original
93
	 * @param ndPereCopie
100
	 * @param ndPereCopie
94
	 *            le père qui va recevoir les copies
101
	 *            le père qui va recevoir les copies
95
	 */
102
	 */
96
	public static void copierFilsNoeud(Node ndPereOriginal, TreeNode ndPereCopie, boolean afficherCheckbox) {
103
	public static void copierFilsNoeud(Node ndPereOriginal, TreeNode ndPereCopie, boolean afficherCheckbox) {
97
		if (ndPereCopie != null && ndPereOriginal != null) {
104
		if (ndPereCopie != null && ndPereOriginal != null) {
98
			Node[] ndNodeFils = ndPereOriginal.getChildNodes();
105
			Node[] ndNodeFils = ndPereOriginal.getChildNodes();
99
			
106
			
100
			for (int i = 0; i < ndNodeFils.length; i++) {
107
			for (int i = 0; i < ndNodeFils.length; i++) {
101
				
108
				
102
				String[] usObj = (String[]) ndNodeFils[i].getUserObject();
109
				String[] usObj = (String[]) ndNodeFils[i].getUserObject();
103
				TreeNode child = new TreeNode(usObj[0]);
110
				TreeNode child = new TreeNode(usObj[0]);
104
				child.setId(usObj[1]);
111
				child.setId(usObj[1]);
105
				if(afficherCheckbox) {
112
				if(afficherCheckbox) {
106
					child.setChecked(false);
113
					child.setChecked(false);
107
				}
114
				}
108
				child.setUserObject(usObj);
115
				child.setUserObject(usObj);
109
				ndPereCopie.appendChild(child);
116
				ndPereCopie.appendChild(child);
110
 
117
 
111
				if (!ndNodeFils[i].isLeaf()) {
118
				if (!ndNodeFils[i].isLeaf()) {
112
					copierFilsNoeud(ndNodeFils[i], child, afficherCheckbox);
119
					copierFilsNoeud(ndNodeFils[i], child, afficherCheckbox);
113
				}
120
				}
114
			}
121
			}
115
		}
122
		}
116
	}
123
	}
117
	
124
	
118
	public static TreeNode ajouterNoeud(TreeNode parent, boolean afficherCheckbox) {
125
	public static TreeNode ajouterNoeud(TreeNode parent, boolean afficherCheckbox) {
119
		// on crée un nouveau noeud vide
126
		// on crée un nouveau noeud vide
120
		TreeNode nd = new TreeNode("");
127
		TreeNode nd = new TreeNode("");
121
		nd.setCls("x-view-treenode-keyword");
128
		nd.setCls("x-view-treenode-keyword");
122
		
129
		
123
		if(afficherCheckbox) {
130
		if(afficherCheckbox) {
124
			nd.setChecked(true);
131
			nd.setChecked(true);
125
		}
132
		}
126
		
133
		
127
		// on associe un objet au noeud qui contient des infos
134
		// on associe un objet au noeud qui contient des infos
128
		String[] usObject = new String[2];
135
		String[] usObject = new String[2];
129
		// l'objet contient le nom du noeud
136
		// l'objet contient le nom du noeud
130
		usObject[0] = "";
137
		usObject[0] = "";
131
		usObject[1] = "";
138
		usObject[1] = "";
132
		nd.setUserObject(usObject);
139
		nd.setUserObject(usObject);
133
		
140
		
134
		String cheminTemporaireAjout = parent.getPath()+"/"+nd.getId();
141
		String cheminTemporaireAjout = parent.getPath()+"/"+nd.getId();
135
		nd.setId(cheminTemporaireAjout);
142
		nd.setId(cheminTemporaireAjout);
136
				
143
				
137
		return nd;
144
		return nd;
138
	}
145
	}
139
	
146
	
140
	public static String getChaineCaracteresInterdits() {
147
	public static String getChaineCaracteresInterdits() {
141
		String interdits = "";
148
		String interdits = "";
142
		for (int i = 0; i < MotsClesUtilitaire.caracteresInterdits.length; i++) {
149
		for (int i = 0; i < MotsClesUtilitaire.caracteresInterdits.length; i++) {
143
			interdits += MotsClesUtilitaire.caracteresInterdits[i]+" ";
150
			interdits += MotsClesUtilitaire.caracteresInterdits[i]+" ";
144
		}
151
		}
145
		return interdits;
152
		return interdits;
146
	}
153
	}
147
	
154
	
148
	public static boolean estUnMotCleAutorise(String motCle) {
155
	public static boolean estUnMotCleAutorise(String motCle) {
149
		boolean valide = !motCle.trim().isEmpty();
156
		boolean valide = !motCle.trim().isEmpty();
150
		for (int i = 0; i < MotsClesUtilitaire.caracteresInterdits.length; i++) {
157
		for (int i = 0; i < MotsClesUtilitaire.caracteresInterdits.length; i++) {
151
			if(motCle.indexOf(MotsClesUtilitaire.caracteresInterdits[i]) != -1) {
158
			if(motCle.indexOf(MotsClesUtilitaire.caracteresInterdits[i]) != -1) {
152
				valide = false;
159
				valide = false;
153
				break;
160
				break;
154
			}
161
			}
155
		}		
162
		}		
156
		return valide;
163
		return valide;
157
	}
164
	}
-
 
165
 
-
 
166
	public static boolean estUnePositionAutorisee(TreeNode node, Node parent, String text) {
-
 
167
		if(parent == null) {
-
 
168
			parent = node.getParentNode();
-
 
169
		}
-
 
170
		Node[] noeudsFreres = parent.getChildNodes(); 
-
 
171
		boolean ok = true;
-
 
172
		for(int i = 0; i <= noeudsFreres.length; i++) {
-
 
173
			if(noeudsFreres[i] != null) {
-
 
174
				String cheminFrere = noeudsFreres[i].getPath();
-
 
175
				String cheminNoeud = node.getPath();
-
 
176
				String texteNoeudfrere = ((String[])noeudsFreres[i].getUserObject())[0];
-
 
177
				if(!cheminFrere.equals(cheminNoeud) && text.equals(texteNoeudfrere)) {
-
 
178
					ok = false;
-
 
179
				}
-
 
180
			}
-
 
181
		}
-
 
182
		return ok;
-
 
183
	}
158
}
184
}