Subversion Repositories eFlore/Applications.cel

Rev

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