Subversion Repositories eFlore/Applications.cel

Rev

Rev 2195 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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