Subversion Repositories eFlore/Applications.cel

Rev

Rev 89 | Rev 91 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
54 david 1
package org.tela_botanica.client.vues;
2
 
90 jpm 3
import java.util.Comparator;
68 david 4
import java.util.Iterator;
5
 
54 david 6
import org.tela_botanica.client.interfaces.Filtrable;
7
import org.tela_botanica.client.interfaces.Rafraichissable;
68 david 8
import org.tela_botanica.client.modeles.EntiteGeographiqueObservation;
9
import org.tela_botanica.client.modeles.ListeEntiteGeographiqueObservation;
89 jpm 10
import org.tela_botanica.client.modeles.Observation;
54 david 11
import org.tela_botanica.client.observation.ObservationMediateur;
12
 
72 david 13
import com.google.gwt.core.client.GWT;
68 david 14
import com.google.gwt.json.client.JSONArray;
15
import com.google.gwt.json.client.JSONObject;
16
import com.google.gwt.user.client.Window;
54 david 17
import com.google.gwt.user.client.ui.Label;
18
import com.gwtext.client.data.Node;
19
import com.gwtext.client.data.NodeTraversalCallback;
20
import com.gwtext.client.data.Tree;
87 jpm 21
import com.gwtext.client.data.event.NodeListenerAdapter;
54 david 22
import com.gwtext.client.widgets.Component;
23
import com.gwtext.client.widgets.Panel;
24
import com.gwtext.client.widgets.event.PanelListenerAdapter;
25
import com.gwtext.client.widgets.tree.TreeNode;
26
import com.gwtext.client.widgets.tree.TreePanel;
87 jpm 27
import com.gwtext.client.widgets.tree.event.TreeNodeListenerAdapter;
85 jpm 28
import com.gwtext.client.widgets.tree.event.TreePanelListenerAdapter;
29
import com.gwtext.client.core.EventObject;
54 david 30
 
31
/**
32
 * fenêtre de recherche affichant l'arbre des mots clés en lecture et un bouton
33
 * cliquable
34
 *
35
 * @author aurelien
36
 *
37
 */
68 david 38
public class ArbreEntiteGeographiqueObservationFiltreVue extends Panel implements Rafraichissable,
54 david 39
		Filtrable {
40
 
41
	/**
42
	 * Le médiateur associé à la vue
43
	 */
44
	private ObservationMediateur	observationMediateur		= null;
45
 
46
	/**
47
	 * Les localites en cours
48
	 */
68 david 49
	private String entitesGeographiquesEncours = "";
54 david 50
 
51
	/**
52
	 * Le treepanel qui affiche l'arbre
53
	 */
68 david 54
	private TreePanel arbreEntitesGeographiques = null;
54 david 55
 
68 david 56
 
57
 
54 david 58
	/**
68 david 59
	 * La structure de donnees qui stocke l'arbre. Utilisee a ce niveau car trop liee a la vue
60
	 */
61
 
62
	private Tree donneeEntitesGeographiques = new Tree();;
63
 
64
	/**
54 david 65
	 * booléen d'initialisation
66
	 */
67
	private boolean estInstancie = false;
68
 
69
	/**
70
	 * booléen d'etat
71
	 */
72
	private boolean filtreModifie = false;
85 jpm 73
 
74
	private boolean arbreCharge = false ;
75
 
86 jpm 76
	private String nomFiltre = "" ;
77
 
54 david 78
	/**
79
	 * Constructeur sans argument (privé car ne doit pas être utilisé)
80
	 */
81
	@SuppressWarnings("unused")
68 david 82
	private ArbreEntiteGeographiqueObservationFiltreVue() {
54 david 83
		super();
84
	}
85
 
86
	/**
87
	 * Constructeur avec paramètres
88
	 *
89
	 * @param im
90
	 *            le médiateur à associer
91
	 */
68 david 92
	public ArbreEntiteGeographiqueObservationFiltreVue(ObservationMediateur obs) {
54 david 93
 
94
		// on crée le panel
70 david 95
		super("Localité");
54 david 96
 
97
		this.observationMediateur = obs;
98
 
68 david 99
		arbreEntitesGeographiques = new TreePanel();
54 david 100
 
101
		this.setPaddings(5);
102
 
103
		this.setBorder(false);
104
		this.setCollapsible(true);
105
		this.setAutoWidth(true);
106
 
107
 
108
		// on ajoute les listeners
109
		ajouterListenersPanel();
110
		estInstancie = false;
111
	}
112
 
113
	/**
114
	 * Ajoute les listeners pour le rendu du panel
115
	 */
116
	private void ajouterListenersPanel() {
68 david 117
		  this.addListener(new PanelListenerAdapter() {
54 david 118
 
119
			// on instancie réellement les composants au moment du rendu pour
120
			// accélérer l'affichage
121
			// et éviter des bugs
122
			public void onRender(Component component) {
123
 
124
				// on interdit le drag and drop dans l'arbre
68 david 125
				arbreEntitesGeographiques.setEnableDD(false);
73 david 126
				arbreEntitesGeographiques.setId("x-view-tree-filter-entity");
72 david 127
				arbreEntitesGeographiques.setAutoWidth(false);
128
 
129
				if (GWT.isScript()) {
130
					arbreEntitesGeographiques.setAutoScroll(true);
131
				}
132
 
133
				arbreEntitesGeographiques.setBorder(false);
54 david 134
 
135
				// on crée une racine pour l'arbre
72 david 136
				TreeNode root = new TreeNode("Localités");
70 david 137
				root.setId("racine_entite");
72 david 138
				String[] usObject = { "Localités" };
54 david 139
				root.setUserObject(usObject);
140
 
68 david 141
				arbreEntitesGeographiques.setRootNode(root);
142
				arbreEntitesGeographiques.setRootVisible(true);
143
				arbreEntitesGeographiques.setBorder(false);
85 jpm 144
				root.setExpandable(true) ;
54 david 145
 
68 david 146
				add(arbreEntitesGeographiques);
54 david 147
 
148
				// on ajoute les listeners d'évenements
149
				ajouterListeners();
150
 
151
 
152
				// enfin on considère le composant comme instancié
153
				estInstancie = true;
154
 
68 david 155
 
54 david 156
			}
157
 
158
		});
159
	}
160
 
68 david 161
 
162
 
54 david 163
	/**
70 david 164
	 * ajoute les listeners pour les boutons et le cochage des entites
54 david 165
	 */
166
	private void ajouterListeners() {
85 jpm 167
 
168
		arbreEntitesGeographiques.addListener(new TreePanelListenerAdapter() {
169
 
170
			public void onClick(TreeNode node, EventObject e) {
171
 
86 jpm 172
				nomFiltre = "" ;
87 jpm 173
				entitesGeographiquesEncours = "" ;
174
				String nomPere = "" ;
175
				String nomGrandPere = "" ;
85 jpm 176
 
177
				switch(node.getDepth())
178
				{
179
					case 0:
180
						if(!arbreCharge)
181
						{
182
							observationMediateur.obtenirListeEntiteGeographique() ;
183
							arbreCharge = true ;
184
						}
86 jpm 185
						else
186
						{
187
							observationMediateur.obtenirNombreObservation() ;
188
						}
85 jpm 189
						return ;
87 jpm 190
					case 3: nomFiltre += "lieudit,location,id_location";
191
						nomPere = ((String[])node.getParentNode().getUserObject())[0] ;
192
						nomGrandPere = ((String[])node.getParentNode().getParentNode().getUserObject())[0] ;
193
						entitesGeographiquesEncours += node.getText()+","+nomPere+","+nomGrandPere ;
85 jpm 194
						break;
87 jpm 195
					case 2: nomFiltre += "location,id_location";
196
						nomPere = ((String[])node.getParentNode().getUserObject())[0] ;
197
						entitesGeographiquesEncours += node.getText()+","+nomPere ;
85 jpm 198
						break;
87 jpm 199
					case 1: nomFiltre += "id_location";
200
						entitesGeographiquesEncours += node.getText() ;
85 jpm 201
						break;
202
					default:
203
						break;
204
				}
205
 
206
				filtreModifie = true ;
86 jpm 207
				observationMediateur.obtenirNombreObservation() ;
85 jpm 208
 
209
			}
210
 
211
		}) ;
87 jpm 212
 
213
		arbreEntitesGeographiques.getRootNode().addListener(new TreeNodeListenerAdapter() {
214
 
215
			public void onExpand(Node node) {
216
				if(!arbreCharge)
217
				{
218
					observationMediateur.obtenirListeEntiteGeographique() ;
219
					arbreCharge = true ;
220
				}
221
			}
222
 
223
		}) ;
54 david 224
	}
225
 
226
	/**
227
	 * Méthode héritée de l'interface rafraichissable
228
	 */
229
	public void rafraichir(Object nouvelleDonnees,
230
			boolean repandreRaffraichissement) {
231
 
232
 
68 david 233
		if (nouvelleDonnees instanceof ListeEntiteGeographiqueObservation) {
234
 
89 jpm 235
			String id_location=null;
236
			String location=null;
237
			String lieuDit=null;
68 david 238
 
239
			ListeEntiteGeographiqueObservation data = (ListeEntiteGeographiqueObservation) nouvelleDonnees ;
240
 
241
				// on crée un arbre vide
242
				TreeNode root = new TreeNode();
72 david 243
				root.setId("racine_entite");
244
				root.setText("Localités");
89 jpm 245
				String[] usObjRoot = { "Localités"};
246
				root.setUserObject(usObjRoot);
68 david 247
				donneeEntitesGeographiques.setRootNode(root);
248
 
83 david 249
			// on la parse et on récupère les informations qui nous interessent
68 david 250
			for (Iterator<String> it= data.keySet().iterator(); it.hasNext();) {
251
 
252
				EntiteGeographiqueObservation ent=(EntiteGeographiqueObservation) data.get(it.next());
253
 
89 jpm 254
				id_location=ent.getIdLocation();
255
				location=ent.getLocation();
256
				lieuDit=ent.getLieuDit();
73 david 257
 
89 jpm 258
				if(id_location.contains("000null")) {
259
					id_location="Inconnue" ;
85 jpm 260
				}
73 david 261
 
89 jpm 262
				if(location.contains("000null")) {
263
					location="Inconnue" ;
264
				}
68 david 265
 
89 jpm 266
				if(lieuDit.contains("000null")) {
267
					lieuDit="Inconnue" ;
268
				}
269
 
270
				Node noeudMemeId = donneeEntitesGeographiques.getNodeById(""+id_location);
271
				// si la région existe déjà
272
					if(noeudMemeId != null)
273
					{
274
						// on teste si la localité existe
275
						Node noeudMemeLoc = donneeEntitesGeographiques.getNodeById(""+(id_location+location));
276
						if(noeudMemeLoc != null)
85 jpm 277
						{
89 jpm 278
							// enfin on teste si le lieu dit existe
279
							Node noeudMemeLieu = donneeEntitesGeographiques.getNodeById(""+(id_location+location+lieuDit));
280
							if(noeudMemeLieu != null)
281
							{
282
								// tous les noeuds existent déjà, normalement ça ne devrait pas arriver
283
							}
284
							else
285
							{
286
								// enfin on ne crée que le noeud du lieu dit
287
								TreeNode node_lieu = new TreeNode();
288
								node_lieu.setId(""+(id_location+location+lieuDit));
289
								node_lieu.setText(lieuDit);
290
								noeudMemeLoc.appendChild(node_lieu) ;
291
								String[] usObj = {lieuDit};
292
								node_lieu.setUserObject(usObj);
293
							}
85 jpm 294
						}
89 jpm 295
						else
296
						{
297
							TreeNode node_loc = new TreeNode();
298
							node_loc.setId(""+(id_location+location));
299
							node_loc.setText(location);
300
							noeudMemeId.appendChild(node_loc) ;
301
							String[] usObj = {location};
302
							node_loc.setUserObject(usObj);
303
 
304
							TreeNode node_lieu = new TreeNode();
305
							node_lieu.setId(""+(id_location+location+lieuDit));
306
							node_lieu.setText(lieuDit);
307
							node_loc.appendChild(node_lieu) ;
308
							String[] usObj2 = {lieuDit};
309
							node_lieu.setUserObject(usObj2);
310
 
311
						}
68 david 312
					}
89 jpm 313
					else
314
					{
315
						TreeNode node_id_loc = new TreeNode();
316
						node_id_loc.setId(""+id_location);
317
						node_id_loc.setText(id_location);
318
						root.appendChild(node_id_loc) ;
319
						String[] usObj = {id_location};
320
						node_id_loc.setUserObject(usObj);
321
 
322
						TreeNode node_loc = new TreeNode();
323
						node_loc.setId(""+(id_location+location));
324
						node_loc.setText(location);
325
						node_id_loc.appendChild(node_loc) ;
326
						String[] usObj2 = {location};
327
						node_loc.setUserObject(usObj2);
328
 
329
						TreeNode node_lieu = new TreeNode();
330
						node_lieu.setId(""+(id_location+location+lieuDit));
331
						node_lieu.setText(lieuDit);
332
						node_loc.appendChild(node_lieu) ;
333
						String[] usObj3 = {lieuDit};
334
						node_lieu.setUserObject(usObj3);
335
					}
336
 
68 david 337
				}
338
 
89 jpm 339
				// on vide tous les noeuds
340
				arbreEntitesGeographiques.getRootNode().eachChild(new NodeTraversalCallback() {
341
 
342
					public boolean execute(Node node) {
343
 
344
						node.remove();
345
						return true;
346
					}
347
 
348
				});
54 david 349
 
89 jpm 350
				// et on recopie le nouvel arbre
351
				copierFilsNoeud(donneeEntitesGeographiques.getRootNode(), arbreEntitesGeographiques
352
						.getRootNode());
353
 
354
				// si l'arbre n'était pas encore considéré comme instancié
355
				if (!estInstancie) {
356
					// on signale que oui
357
					estInstancie = true;
54 david 358
				}
89 jpm 359
 
360
				// l'état du filtre est réinitialisé
361
				filtreModifie = false;
362
				//show() ;
363
				doLayout();
54 david 364
 
365
			}
89 jpm 366
 
367
		if(nouvelleDonnees instanceof Observation)
368
		{
369
			Observation obs = (Observation)nouvelleDonnees ;
370
 
371
			String id_location= obs.getIdentifiantLocalite();
372
			id_location.replaceAll("\"", "") ;
373
			String location=obs.getLocalite();
374
			String lieuDit=obs.getLieudit();
375
 
376
			Node root = arbreEntitesGeographiques.getRootNode() ;
377
 
378
			if(id_location.contains("000null") || id_location.equals(null)) {
379
				id_location="Inconnue" ;
380
			}
381
 
382
			if(location.contains("000null") || location.equals(null)) {
383
				location="Inconnue" ;
384
			}
385
 
386
			if(lieuDit.contains("000null") || lieuDit.equals(null)) {
387
				lieuDit="Inconnue" ;
388
			}
389
 
390
			Node noeudMemeId = donneeEntitesGeographiques.getNodeById(""+id_location);
391
			// si la région existe déjà
392
				if(noeudMemeId != null)
393
				{
394
					// on teste si la localité existe
395
					Node noeudMemeLoc = donneeEntitesGeographiques.getNodeById(""+(id_location+location));
396
					if(noeudMemeLoc != null)
397
					{
398
						// enfin on teste si le lieu dit existe
399
						Node noeudMemeLieu = donneeEntitesGeographiques.getNodeById(""+(id_location+location+lieuDit));
400
						if(noeudMemeLieu != null)
401
						{
402
							// tous les noeuds existent déjà, normalement ça ne devrait pas arriver
403
						}
404
						else
405
						{
406
							// enfin on ne crée que le noeud du lieu dit
407
							TreeNode node_lieu = new TreeNode();
408
							node_lieu.setId(""+(id_location+location+lieuDit));
409
							node_lieu.setText(lieuDit);
410
							String[] usObj = {lieuDit};
411
							node_lieu.setUserObject(usObj);
412
							noeudMemeLoc.appendChild(node_lieu) ;
413
						}
414
					}
415
					else
416
					{
417
						TreeNode node_loc = new TreeNode();
418
						node_loc.setId(""+(id_location+location));
419
						node_loc.setText(location);
420
						String[] usObj = {location};
421
						node_loc.setUserObject(usObj);
422
						noeudMemeId.appendChild(node_loc) ;
423
 
424
						TreeNode node_lieu = new TreeNode();
425
						node_lieu.setId(""+(id_location+location+lieuDit));
426
						node_lieu.setText(lieuDit);
427
						String[] usObj2 = {lieuDit};
428
						node_lieu.setUserObject(usObj2);
429
						node_loc.appendChild(node_lieu) ;
430
 
431
					}
432
			}
433
			else
434
			{
435
				TreeNode node_id_loc = new TreeNode();
436
				node_id_loc.setId(""+id_location);
437
				node_id_loc.setText(id_location);
438
				String[] usObj = {id_location};
439
				node_id_loc.setUserObject(usObj);
440
				root.appendChild(node_id_loc) ;
441
 
442
				TreeNode node_loc = new TreeNode();
443
				node_loc.setId(""+(id_location+location));
444
				node_loc.setText(location);
445
				String[] usObj2 = {location};
446
				node_loc.setUserObject(usObj2);
447
				node_id_loc.appendChild(node_loc) ;
448
 
449
				TreeNode node_lieu = new TreeNode();
450
				node_lieu.setId(""+(id_location+location+lieuDit));
451
				node_lieu.setText(lieuDit);
452
				String[] usObj3 = {lieuDit};
453
				node_lieu.setUserObject(usObj3);
454
				node_loc.appendChild(node_lieu) ;
90 jpm 455
 
456
				root.sort(comparerNoeuds()) ;
89 jpm 457
			}
458
 
459
				arbreEntitesGeographiques.doLayout() ;
460
		}
68 david 461
	}
54 david 462
 
463
 
464
	/**
465
	 * Accesseur pour le panneau contenant l'arbre
466
	 *
467
	 * @return le panneau de l'arbre des mots clés
468
	 */
469
	public TreePanel getArbreMotsCles() {
68 david 470
		return arbreEntitesGeographiques;
54 david 471
	}
472
 
473
	/**
474
	 * Méthode héritée de Filtrable renvoie le nom du filtre
475
	 */
476
	public String renvoyerNomFiltre() {
477
 
478
		return "Localités";
479
	}
480
 
481
	/**
482
	 * Renvoie un tableau contenant le nom du champ à filtrer et la valeur
483
	 *
484
	 * @return un tableau contenant le nom du champ à filtrer et sa valeur
485
	 */
486
	public String[] renvoyerValeursAFiltrer() {
487
 
488
		valider();
85 jpm 489
 
86 jpm 490
		String valeursFiltrees[] = {nomFiltre, entitesGeographiquesEncours } ;
54 david 491
 
492
		return valeursFiltrees;
493
	}
494
 
495
	/**
496
	 * Fonction récursive qui prend deux noeuds d'arbre en paramètre et crée un
497
	 * copie du sous arbre du premier noeud, qu'elle concatène au deuxième
498
	 *
499
	 * @param ndPereOriginal
500
	 *            le père des noeuds de l'arbre original
501
	 * @param ndPereCopie
502
	 *            le père qui va recevoir les copies
503
	 */
504
	private void copierFilsNoeud(Node ndPereOriginal, TreeNode ndPereCopie) {
505
		if (ndPereCopie != null && ndPereOriginal != null) {
506
			Node[] ndNodeFils = ndPereOriginal.getChildNodes();
507
 
508
			for (int i = 0; i < ndNodeFils.length; i++) {
509
 
510
				String[] usObj = (String[]) ndNodeFils[i].getUserObject();
511
				TreeNode child = new TreeNode(usObj[0]);
512
				child.setUserObject(usObj);
513
				ndPereCopie.appendChild(child);
514
 
515
				if (!ndNodeFils[i].isLeaf()) {
516
					copierFilsNoeud(ndNodeFils[i], child);
517
				}
518
 
519
			}
520
		}
521
	}
522
 
523
	/**
524
	 * Méthode héritée de Filtrable Renvoie l'état du filtre (modifié ou non)
525
	 */
526
	public boolean renvoyerEtatFiltre() {
527
 
528
		return filtreModifie;
529
	}
530
 
531
	public void valider() {
86 jpm 532
 
54 david 533
		if (estInstancie) {
86 jpm 534
 
54 david 535
		}
536
	}
90 jpm 537
 
538
	public Comparator<TreeNode> comparerNoeuds()
539
	{
540
		return new Comparator<TreeNode>() {
54 david 541
 
90 jpm 542
			public int compare(TreeNode o1, TreeNode o2) {
543
 
544
				String n1 = o1.getText() ;
545
				String n2 = o2.getText() ;
546
 
547
				return n1.compareTo(n2) ;
548
			}
549
 
550
		} ;
551
	}
552
 
54 david 553
}