Subversion Repositories eFlore/Applications.cel

Rev

Rev 109 | 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.setCollapsible(true);
106
		this.setAutoWidth(true);
128 aurelien 107
 
108
		if (GWT.isScript()) {
109
			setAutoScroll(true);
110
		}
54 david 111
 
112
 
113
		// on ajoute les listeners
114
		ajouterListenersPanel();
115
		estInstancie = false;
116
	}
117
 
118
	/**
119
	 * Ajoute les listeners pour le rendu du panel
120
	 */
121
	private void ajouterListenersPanel() {
68 david 122
		  this.addListener(new PanelListenerAdapter() {
54 david 123
 
124
			// on instancie réellement les composants au moment du rendu pour
125
			// accélérer l'affichage
126
			// et éviter des bugs
127
			public void onRender(Component component) {
128
 
129
				// on interdit le drag and drop dans l'arbre
68 david 130
				arbreEntitesGeographiques.setEnableDD(false);
73 david 131
				arbreEntitesGeographiques.setId("x-view-tree-filter-entity");
72 david 132
				arbreEntitesGeographiques.setAutoWidth(false);
54 david 133
 
134
				// on crée une racine pour l'arbre
72 david 135
				TreeNode root = new TreeNode("Localités");
70 david 136
				root.setId("racine_entite");
72 david 137
				String[] usObject = { "Localités" };
54 david 138
				root.setUserObject(usObject);
139
 
68 david 140
				arbreEntitesGeographiques.setRootNode(root);
141
				arbreEntitesGeographiques.setRootVisible(true);
142
				arbreEntitesGeographiques.setBorder(false);
85 jpm 143
				root.setExpandable(true) ;
54 david 144
 
68 david 145
				add(arbreEntitesGeographiques);
54 david 146
 
147
				// on ajoute les listeners d'évenements
148
				ajouterListeners();
149
 
150
 
151
				// enfin on considère le composant comme instancié
152
				estInstancie = true;
153
 
68 david 154
 
54 david 155
			}
156
 
157
		});
158
	}
159
 
68 david 160
 
161
 
54 david 162
	/**
70 david 163
	 * ajoute les listeners pour les boutons et le cochage des entites
54 david 164
	 */
165
	private void ajouterListeners() {
85 jpm 166
 
167
		arbreEntitesGeographiques.addListener(new TreePanelListenerAdapter() {
168
 
169
			public void onClick(TreeNode node, EventObject e) {
170
 
86 jpm 171
				nomFiltre = "" ;
87 jpm 172
				entitesGeographiquesEncours = "" ;
173
				String nomPere = "" ;
174
				String nomGrandPere = "" ;
85 jpm 175
 
176
				switch(node.getDepth())
177
				{
178
					case 0:
179
						if(!arbreCharge)
180
						{
128 aurelien 181
							/*observationMediateur.obtenirListeEntiteGeographique() ;
182
							arbreCharge = true ;*/
183
							arbreEntitesGeographiques.getRootNode().expand();
85 jpm 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
 
128 aurelien 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();
128 aurelien 255
				id_location = id_location.replaceAll("\"", "");
89 jpm 256
				location=ent.getLocation();
257
				lieuDit=ent.getLieuDit();
73 david 258
 
128 aurelien 259
				if(id_location.contains("000null") || id_location.equals(null) || (id_location.trim()).equals("")) {
89 jpm 260
					id_location="Inconnue" ;
85 jpm 261
				}
73 david 262
 
128 aurelien 263
				if(location.contains("000null") || location.equals(null) || (location.trim().equals(""))) {
89 jpm 264
					location="Inconnue" ;
265
				}
68 david 266
 
128 aurelien 267
				if(lieuDit.contains("000null") || lieuDit.equals(null) || (lieuDit.trim().equals(""))) {
89 jpm 268
					lieuDit="Inconnue" ;
269
				}
270
 
271
				Node noeudMemeId = donneeEntitesGeographiques.getNodeById(""+id_location);
272
				// si la région existe déjà
273
					if(noeudMemeId != null)
274
					{
275
						// on teste si la localité existe
276
						Node noeudMemeLoc = donneeEntitesGeographiques.getNodeById(""+(id_location+location));
277
						if(noeudMemeLoc != null)
85 jpm 278
						{
89 jpm 279
							// enfin on teste si le lieu dit existe
280
							Node noeudMemeLieu = donneeEntitesGeographiques.getNodeById(""+(id_location+location+lieuDit));
281
							if(noeudMemeLieu != null)
282
							{
283
								// tous les noeuds existent déjà, normalement ça ne devrait pas arriver
284
							}
285
							else
286
							{
287
								// enfin on ne crée que le noeud du lieu dit
288
								TreeNode node_lieu = new TreeNode();
289
								node_lieu.setId(""+(id_location+location+lieuDit));
290
								node_lieu.setText(lieuDit);
291
								noeudMemeLoc.appendChild(node_lieu) ;
292
								String[] usObj = {lieuDit};
293
								node_lieu.setUserObject(usObj);
294
							}
85 jpm 295
						}
89 jpm 296
						else
297
						{
298
							TreeNode node_loc = new TreeNode();
299
							node_loc.setId(""+(id_location+location));
300
							node_loc.setText(location);
301
							noeudMemeId.appendChild(node_loc) ;
302
							String[] usObj = {location};
303
							node_loc.setUserObject(usObj);
304
 
305
							TreeNode node_lieu = new TreeNode();
306
							node_lieu.setId(""+(id_location+location+lieuDit));
307
							node_lieu.setText(lieuDit);
308
							node_loc.appendChild(node_lieu) ;
309
							String[] usObj2 = {lieuDit};
310
							node_lieu.setUserObject(usObj2);
311
 
312
						}
68 david 313
					}
89 jpm 314
					else
315
					{
316
						TreeNode node_id_loc = new TreeNode();
317
						node_id_loc.setId(""+id_location);
318
						node_id_loc.setText(id_location);
319
						root.appendChild(node_id_loc) ;
320
						String[] usObj = {id_location};
321
						node_id_loc.setUserObject(usObj);
322
 
323
						TreeNode node_loc = new TreeNode();
324
						node_loc.setId(""+(id_location+location));
325
						node_loc.setText(location);
326
						node_id_loc.appendChild(node_loc) ;
327
						String[] usObj2 = {location};
328
						node_loc.setUserObject(usObj2);
329
 
330
						TreeNode node_lieu = new TreeNode();
331
						node_lieu.setId(""+(id_location+location+lieuDit));
332
						node_lieu.setText(lieuDit);
333
						node_loc.appendChild(node_lieu) ;
334
						String[] usObj3 = {lieuDit};
335
						node_lieu.setUserObject(usObj3);
336
					}
109 aurelien 337
 
338
					root.sort(comparerNoeuds()) ;
339
					doLayout();
89 jpm 340
 
68 david 341
				}
342
 
89 jpm 343
				// on vide tous les noeuds
344
				arbreEntitesGeographiques.getRootNode().eachChild(new NodeTraversalCallback() {
345
 
346
					public boolean execute(Node node) {
347
 
348
						node.remove();
349
						return true;
350
					}
351
 
352
				});
54 david 353
 
89 jpm 354
				// et on recopie le nouvel arbre
355
				copierFilsNoeud(donneeEntitesGeographiques.getRootNode(), arbreEntitesGeographiques
356
						.getRootNode());
357
 
358
				// si l'arbre n'était pas encore considéré comme instancié
359
				if (!estInstancie) {
360
					// on signale que oui
361
					estInstancie = true;
54 david 362
				}
89 jpm 363
 
364
				// l'état du filtre est réinitialisé
365
				filtreModifie = false;
109 aurelien 366
 
89 jpm 367
				//show() ;
368
				doLayout();
54 david 369
 
370
			}
89 jpm 371
 
372
		if(nouvelleDonnees instanceof Observation)
128 aurelien 373
		{
374
			// si l'arbre n'est pas encore chargé, on ne tient pas compte de l'ajout
375
			// l'arbre complet sera de toute façon renvoyé plus tard lors du premier chargement
376
			// de l'arbre
377
			if(!arbreCharge) {
378
				return;
379
			}
380
 
89 jpm 381
			Observation obs = (Observation)nouvelleDonnees ;
382
 
383
			String id_location= obs.getIdentifiantLocalite();
128 aurelien 384
			id_location = id_location.replaceAll("\"", "");
89 jpm 385
			String location=obs.getLocalite();
386
			String lieuDit=obs.getLieudit();
387
 
388
			Node root = arbreEntitesGeographiques.getRootNode() ;
389
 
128 aurelien 390
			if(id_location.contains("000null") || id_location.equals(null) || (id_location.trim()).equals("")) {
89 jpm 391
				id_location="Inconnue" ;
392
			}
393
 
128 aurelien 394
			if(location.contains("000null") || location.equals(null) || (location.trim().equals(""))) {
89 jpm 395
				location="Inconnue" ;
396
			}
397
 
128 aurelien 398
			if(lieuDit.contains("000null") || lieuDit.equals(null) || (lieuDit.trim().equals(""))) {
89 jpm 399
				lieuDit="Inconnue" ;
400
			}
401
 
402
			Node noeudMemeId = donneeEntitesGeographiques.getNodeById(""+id_location);
403
			// si la région existe déjà
404
				if(noeudMemeId != null)
405
				{
406
					// on teste si la localité existe
407
					Node noeudMemeLoc = donneeEntitesGeographiques.getNodeById(""+(id_location+location));
408
					if(noeudMemeLoc != null)
409
					{
410
						// enfin on teste si le lieu dit existe
411
						Node noeudMemeLieu = donneeEntitesGeographiques.getNodeById(""+(id_location+location+lieuDit));
412
						if(noeudMemeLieu != null)
413
						{
414
							// tous les noeuds existent déjà, normalement ça ne devrait pas arriver
415
						}
416
						else
417
						{
418
							// enfin on ne crée que le noeud du lieu dit
419
							TreeNode node_lieu = new TreeNode();
420
							node_lieu.setId(""+(id_location+location+lieuDit));
421
							node_lieu.setText(lieuDit);
422
							String[] usObj = {lieuDit};
423
							node_lieu.setUserObject(usObj);
424
							noeudMemeLoc.appendChild(node_lieu) ;
109 aurelien 425
 
426
							root.sort(comparerNoeuds()) ;
89 jpm 427
						}
428
					}
429
					else
430
					{
431
						TreeNode node_loc = new TreeNode();
432
						node_loc.setId(""+(id_location+location));
433
						node_loc.setText(location);
434
						String[] usObj = {location};
435
						node_loc.setUserObject(usObj);
436
						noeudMemeId.appendChild(node_loc) ;
437
 
438
						TreeNode node_lieu = new TreeNode();
439
						node_lieu.setId(""+(id_location+location+lieuDit));
440
						node_lieu.setText(lieuDit);
441
						String[] usObj2 = {lieuDit};
442
						node_lieu.setUserObject(usObj2);
443
						node_loc.appendChild(node_lieu) ;
444
 
109 aurelien 445
						root.sort(comparerNoeuds()) ;
446
 
89 jpm 447
					}
448
			}
449
			else
450
			{
91 jpm 451
				// TODO: Pourquoi l'ajout ne marche que sur la racine ?
452
 
89 jpm 453
				TreeNode node_id_loc = new TreeNode();
454
				node_id_loc.setId(""+id_location);
455
				node_id_loc.setText(id_location);
456
				String[] usObj = {id_location};
457
				node_id_loc.setUserObject(usObj);
458
				root.appendChild(node_id_loc) ;
459
 
460
				TreeNode node_loc = new TreeNode();
461
				node_loc.setId(""+(id_location+location));
462
				node_loc.setText(location);
463
				String[] usObj2 = {location};
464
				node_loc.setUserObject(usObj2);
465
				node_id_loc.appendChild(node_loc) ;
466
 
467
				TreeNode node_lieu = new TreeNode();
468
				node_lieu.setId(""+(id_location+location+lieuDit));
469
				node_lieu.setText(lieuDit);
470
				String[] usObj3 = {lieuDit};
471
				node_lieu.setUserObject(usObj3);
472
				node_loc.appendChild(node_lieu) ;
90 jpm 473
 
474
				root.sort(comparerNoeuds()) ;
89 jpm 475
 
109 aurelien 476
			}
89 jpm 477
				arbreEntitesGeographiques.doLayout() ;
478
		}
68 david 479
	}
54 david 480
 
481
 
482
	/**
483
	 * Accesseur pour le panneau contenant l'arbre
484
	 *
485
	 * @return le panneau de l'arbre des mots clés
486
	 */
487
	public TreePanel getArbreMotsCles() {
68 david 488
		return arbreEntitesGeographiques;
54 david 489
	}
490
 
491
	/**
492
	 * Méthode héritée de Filtrable renvoie le nom du filtre
493
	 */
494
	public String renvoyerNomFiltre() {
495
 
496
		return "Localités";
497
	}
498
 
499
	/**
500
	 * Renvoie un tableau contenant le nom du champ à filtrer et la valeur
501
	 *
502
	 * @return un tableau contenant le nom du champ à filtrer et sa valeur
503
	 */
504
	public String[] renvoyerValeursAFiltrer() {
505
 
506
		valider();
85 jpm 507
 
86 jpm 508
		String valeursFiltrees[] = {nomFiltre, entitesGeographiquesEncours } ;
54 david 509
 
510
		return valeursFiltrees;
511
	}
512
 
513
	/**
514
	 * Fonction récursive qui prend deux noeuds d'arbre en paramètre et crée un
515
	 * copie du sous arbre du premier noeud, qu'elle concatène au deuxième
516
	 *
517
	 * @param ndPereOriginal
518
	 *            le père des noeuds de l'arbre original
519
	 * @param ndPereCopie
520
	 *            le père qui va recevoir les copies
521
	 */
522
	private void copierFilsNoeud(Node ndPereOriginal, TreeNode ndPereCopie) {
523
		if (ndPereCopie != null && ndPereOriginal != null) {
524
			Node[] ndNodeFils = ndPereOriginal.getChildNodes();
525
 
526
			for (int i = 0; i < ndNodeFils.length; i++) {
527
 
528
				String[] usObj = (String[]) ndNodeFils[i].getUserObject();
529
				TreeNode child = new TreeNode(usObj[0]);
530
				child.setUserObject(usObj);
531
				ndPereCopie.appendChild(child);
532
 
533
				if (!ndNodeFils[i].isLeaf()) {
534
					copierFilsNoeud(ndNodeFils[i], child);
535
				}
536
 
537
			}
538
		}
539
	}
540
 
541
	/**
542
	 * Méthode héritée de Filtrable Renvoie l'état du filtre (modifié ou non)
543
	 */
544
	public boolean renvoyerEtatFiltre() {
545
 
546
		return filtreModifie;
547
	}
548
 
549
	public void valider() {
86 jpm 550
 
54 david 551
		if (estInstancie) {
86 jpm 552
 
54 david 553
		}
554
	}
90 jpm 555
 
556
	public Comparator<TreeNode> comparerNoeuds()
557
	{
558
		return new Comparator<TreeNode>() {
54 david 559
 
90 jpm 560
			public int compare(TreeNode o1, TreeNode o2) {
561
 
109 aurelien 562
				if(o1.getText().equals("Inconnue")) {
563
					return -1 ;
564
				}
565
 
566
				if(o2.getText().equals("Inconnue")) {
567
					return 1 ;
568
				}
569
 
570
				if(o1.getDepth() == 1 && o2.getDepth() == 1)
91 jpm 571
				{
109 aurelien 572
					String l1 = o1.getText() ;
573
					String l2 = o2.getText() ;
574
					if(l1.length() == 1) {
575
						l1 = "0"+l1;
576
					}
91 jpm 577
 
109 aurelien 578
					if(l2.length() == 1) {
579
						l2 = "0"+l2;
580
					}
581
 
128 aurelien 582
					Integer n1 = 0;
583
					Integer n2 = 0;
109 aurelien 584
 
128 aurelien 585
					try{
586
						n1 = Integer.parseInt(l1) ;
587
						n2 = Integer.parseInt(l2) ;
588
					} catch(NumberFormatException ne)  {
589
						n1 = 0;
590
						n2 = 0;
591
					}
592
 
91 jpm 593
					return n1.compareTo(n2) ;
594
				}
595
				else
94 jpm 596
				{
91 jpm 597
					String n1 = o1.getText() ;
598
					String n2 = o2.getText() ;
90 jpm 599
 
91 jpm 600
					return n1.compareTo(n2) ;
601
				}
90 jpm 602
			}
603
		} ;
604
	}
104 jpm 605
 
606
	public void raz() {
607
 
608
		arbreCharge = false ;
609
		arbreEntitesGeographiques.clear() ;
610
 
611
		// on crée une racine pour l'arbre
612
		TreeNode root = new TreeNode("Localités");
613
		root.setId("racine_entite");
614
		String[] usObject = { "Localités" };
615
		root.setUserObject(usObject);
616
 
617
		arbreEntitesGeographiques.setRootNode(root);
618
 
619
		arbreEntitesGeographiques.getRootNode().addListener(new TreeNodeListenerAdapter() {
620
 
621
			public void onExpand(Node node) {
622
				if(!arbreCharge)
623
				{
624
					observationMediateur.obtenirDatesObservation() ;
625
					arbreCharge = true ;
626
				}
627
			}
628
 
629
		}) ;
630
 
631
		arbreCharge = false ;
632
 
633
		this.doLayout() ;
634
 
635
	}
91 jpm 636
 
54 david 637
}