Subversion Repositories eFlore/Applications.cel

Rev

Rev 99 | 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;
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);
99 jpm 106
		this.setAutoScroll(true) ;
54 david 107
 
108
 
109
		// on ajoute les listeners
110
		ajouterListenersPanel();
111
		estInstancie = false;
112
	}
113
 
114
	/**
115
	 * Ajoute les listeners pour le rendu du panel
116
	 */
117
	private void ajouterListenersPanel() {
68 david 118
		  this.addListener(new PanelListenerAdapter() {
54 david 119
 
120
			// on instancie réellement les composants au moment du rendu pour
121
			// accélérer l'affichage
122
			// et éviter des bugs
123
			public void onRender(Component component) {
124
 
125
				// on interdit le drag and drop dans l'arbre
68 david 126
				arbreEntitesGeographiques.setEnableDD(false);
73 david 127
				arbreEntitesGeographiques.setId("x-view-tree-filter-entity");
72 david 128
				arbreEntitesGeographiques.setAutoWidth(false);
129
 
130
				if (GWT.isScript()) {
131
					arbreEntitesGeographiques.setAutoScroll(true);
132
				}
133
 
134
				arbreEntitesGeographiques.setBorder(false);
54 david 135
 
136
				// on crée une racine pour l'arbre
72 david 137
				TreeNode root = new TreeNode("Localités");
70 david 138
				root.setId("racine_entite");
72 david 139
				String[] usObject = { "Localités" };
54 david 140
				root.setUserObject(usObject);
141
 
68 david 142
				arbreEntitesGeographiques.setRootNode(root);
143
				arbreEntitesGeographiques.setRootVisible(true);
144
				arbreEntitesGeographiques.setBorder(false);
85 jpm 145
				root.setExpandable(true) ;
54 david 146
 
68 david 147
				add(arbreEntitesGeographiques);
54 david 148
 
149
				// on ajoute les listeners d'évenements
150
				ajouterListeners();
151
 
152
 
153
				// enfin on considère le composant comme instancié
154
				estInstancie = true;
155
 
68 david 156
 
54 david 157
			}
158
 
159
		});
160
	}
161
 
68 david 162
 
163
 
54 david 164
	/**
70 david 165
	 * ajoute les listeners pour les boutons et le cochage des entites
54 david 166
	 */
167
	private void ajouterListeners() {
85 jpm 168
 
169
		arbreEntitesGeographiques.addListener(new TreePanelListenerAdapter() {
170
 
171
			public void onClick(TreeNode node, EventObject e) {
172
 
86 jpm 173
				nomFiltre = "" ;
87 jpm 174
				entitesGeographiquesEncours = "" ;
175
				String nomPere = "" ;
176
				String nomGrandPere = "" ;
85 jpm 177
 
178
				switch(node.getDepth())
179
				{
180
					case 0:
181
						if(!arbreCharge)
182
						{
183
							observationMediateur.obtenirListeEntiteGeographique() ;
184
							arbreCharge = true ;
185
						}
86 jpm 186
						else
187
						{
188
							observationMediateur.obtenirNombreObservation() ;
189
						}
85 jpm 190
						return ;
87 jpm 191
					case 3: nomFiltre += "lieudit,location,id_location";
192
						nomPere = ((String[])node.getParentNode().getUserObject())[0] ;
193
						nomGrandPere = ((String[])node.getParentNode().getParentNode().getUserObject())[0] ;
194
						entitesGeographiquesEncours += node.getText()+","+nomPere+","+nomGrandPere ;
85 jpm 195
						break;
87 jpm 196
					case 2: nomFiltre += "location,id_location";
197
						nomPere = ((String[])node.getParentNode().getUserObject())[0] ;
198
						entitesGeographiquesEncours += node.getText()+","+nomPere ;
85 jpm 199
						break;
87 jpm 200
					case 1: nomFiltre += "id_location";
201
						entitesGeographiquesEncours += node.getText() ;
85 jpm 202
						break;
203
					default:
204
						break;
205
				}
206
 
207
				filtreModifie = true ;
86 jpm 208
				observationMediateur.obtenirNombreObservation() ;
85 jpm 209
 
210
			}
211
 
212
		}) ;
87 jpm 213
 
214
		arbreEntitesGeographiques.getRootNode().addListener(new TreeNodeListenerAdapter() {
215
 
216
			public void onExpand(Node node) {
217
				if(!arbreCharge)
218
				{
219
					observationMediateur.obtenirListeEntiteGeographique() ;
220
					arbreCharge = true ;
221
				}
222
			}
223
 
224
		}) ;
54 david 225
	}
226
 
227
	/**
228
	 * Méthode héritée de l'interface rafraichissable
229
	 */
230
	public void rafraichir(Object nouvelleDonnees,
231
			boolean repandreRaffraichissement) {
232
 
233
 
68 david 234
		if (nouvelleDonnees instanceof ListeEntiteGeographiqueObservation) {
235
 
89 jpm 236
			String id_location=null;
237
			String location=null;
238
			String lieuDit=null;
68 david 239
 
240
			ListeEntiteGeographiqueObservation data = (ListeEntiteGeographiqueObservation) nouvelleDonnees ;
241
 
242
				// on crée un arbre vide
243
				TreeNode root = new TreeNode();
72 david 244
				root.setId("racine_entite");
245
				root.setText("Localités");
89 jpm 246
				String[] usObjRoot = { "Localités"};
247
				root.setUserObject(usObjRoot);
68 david 248
				donneeEntitesGeographiques.setRootNode(root);
249
 
83 david 250
			// on la parse et on récupère les informations qui nous interessent
68 david 251
			for (Iterator<String> it= data.keySet().iterator(); it.hasNext();) {
252
 
253
				EntiteGeographiqueObservation ent=(EntiteGeographiqueObservation) data.get(it.next());
254
 
89 jpm 255
				id_location=ent.getIdLocation();
256
				location=ent.getLocation();
257
				lieuDit=ent.getLieuDit();
73 david 258
 
89 jpm 259
				if(id_location.contains("000null")) {
260
					id_location="Inconnue" ;
85 jpm 261
				}
73 david 262
 
89 jpm 263
				if(location.contains("000null")) {
264
					location="Inconnue" ;
265
				}
68 david 266
 
89 jpm 267
				if(lieuDit.contains("000null")) {
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
					}
337
 
68 david 338
				}
339
 
89 jpm 340
				// on vide tous les noeuds
341
				arbreEntitesGeographiques.getRootNode().eachChild(new NodeTraversalCallback() {
342
 
343
					public boolean execute(Node node) {
344
 
345
						node.remove();
346
						return true;
347
					}
348
 
349
				});
54 david 350
 
89 jpm 351
				// et on recopie le nouvel arbre
352
				copierFilsNoeud(donneeEntitesGeographiques.getRootNode(), arbreEntitesGeographiques
353
						.getRootNode());
354
 
355
				// si l'arbre n'était pas encore considéré comme instancié
356
				if (!estInstancie) {
357
					// on signale que oui
358
					estInstancie = true;
54 david 359
				}
89 jpm 360
 
361
				// l'état du filtre est réinitialisé
362
				filtreModifie = false;
363
				//show() ;
364
				doLayout();
54 david 365
 
366
			}
89 jpm 367
 
368
		if(nouvelleDonnees instanceof Observation)
369
		{
370
			Observation obs = (Observation)nouvelleDonnees ;
371
 
372
			String id_location= obs.getIdentifiantLocalite();
373
			id_location.replaceAll("\"", "") ;
374
			String location=obs.getLocalite();
375
			String lieuDit=obs.getLieudit();
376
 
377
			Node root = arbreEntitesGeographiques.getRootNode() ;
378
 
379
			if(id_location.contains("000null") || id_location.equals(null)) {
380
				id_location="Inconnue" ;
381
			}
382
 
383
			if(location.contains("000null") || location.equals(null)) {
384
				location="Inconnue" ;
385
			}
386
 
387
			if(lieuDit.contains("000null") || lieuDit.equals(null)) {
388
				lieuDit="Inconnue" ;
389
			}
390
 
391
			Node noeudMemeId = donneeEntitesGeographiques.getNodeById(""+id_location);
392
			// si la région existe déjà
393
				if(noeudMemeId != null)
394
				{
395
					// on teste si la localité existe
396
					Node noeudMemeLoc = donneeEntitesGeographiques.getNodeById(""+(id_location+location));
397
					if(noeudMemeLoc != null)
398
					{
399
						// enfin on teste si le lieu dit existe
400
						Node noeudMemeLieu = donneeEntitesGeographiques.getNodeById(""+(id_location+location+lieuDit));
401
						if(noeudMemeLieu != null)
402
						{
403
							// tous les noeuds existent déjà, normalement ça ne devrait pas arriver
404
						}
405
						else
406
						{
407
							// enfin on ne crée que le noeud du lieu dit
408
							TreeNode node_lieu = new TreeNode();
409
							node_lieu.setId(""+(id_location+location+lieuDit));
410
							node_lieu.setText(lieuDit);
411
							String[] usObj = {lieuDit};
412
							node_lieu.setUserObject(usObj);
413
							noeudMemeLoc.appendChild(node_lieu) ;
414
						}
415
					}
416
					else
417
					{
418
						TreeNode node_loc = new TreeNode();
419
						node_loc.setId(""+(id_location+location));
420
						node_loc.setText(location);
421
						String[] usObj = {location};
422
						node_loc.setUserObject(usObj);
423
						noeudMemeId.appendChild(node_loc) ;
424
 
425
						TreeNode node_lieu = new TreeNode();
426
						node_lieu.setId(""+(id_location+location+lieuDit));
427
						node_lieu.setText(lieuDit);
428
						String[] usObj2 = {lieuDit};
429
						node_lieu.setUserObject(usObj2);
430
						node_loc.appendChild(node_lieu) ;
431
 
432
					}
433
			}
434
			else
435
			{
91 jpm 436
				// TODO: Pourquoi l'ajout ne marche que sur la racine ?
437
 
89 jpm 438
				TreeNode node_id_loc = new TreeNode();
439
				node_id_loc.setId(""+id_location);
440
				node_id_loc.setText(id_location);
441
				String[] usObj = {id_location};
442
				node_id_loc.setUserObject(usObj);
443
				root.appendChild(node_id_loc) ;
444
 
445
				TreeNode node_loc = new TreeNode();
446
				node_loc.setId(""+(id_location+location));
447
				node_loc.setText(location);
448
				String[] usObj2 = {location};
449
				node_loc.setUserObject(usObj2);
450
				node_id_loc.appendChild(node_loc) ;
451
 
452
				TreeNode node_lieu = new TreeNode();
453
				node_lieu.setId(""+(id_location+location+lieuDit));
454
				node_lieu.setText(lieuDit);
455
				String[] usObj3 = {lieuDit};
456
				node_lieu.setUserObject(usObj3);
457
				node_loc.appendChild(node_lieu) ;
90 jpm 458
 
91 jpm 459
				// TODO : améliorer la compararaison des noeuds
90 jpm 460
				root.sort(comparerNoeuds()) ;
89 jpm 461
			}
462
 
463
				arbreEntitesGeographiques.doLayout() ;
464
		}
68 david 465
	}
54 david 466
 
467
 
468
	/**
469
	 * Accesseur pour le panneau contenant l'arbre
470
	 *
471
	 * @return le panneau de l'arbre des mots clés
472
	 */
473
	public TreePanel getArbreMotsCles() {
68 david 474
		return arbreEntitesGeographiques;
54 david 475
	}
476
 
477
	/**
478
	 * Méthode héritée de Filtrable renvoie le nom du filtre
479
	 */
480
	public String renvoyerNomFiltre() {
481
 
482
		return "Localités";
483
	}
484
 
485
	/**
486
	 * Renvoie un tableau contenant le nom du champ à filtrer et la valeur
487
	 *
488
	 * @return un tableau contenant le nom du champ à filtrer et sa valeur
489
	 */
490
	public String[] renvoyerValeursAFiltrer() {
491
 
492
		valider();
85 jpm 493
 
86 jpm 494
		String valeursFiltrees[] = {nomFiltre, entitesGeographiquesEncours } ;
54 david 495
 
496
		return valeursFiltrees;
497
	}
498
 
499
	/**
500
	 * Fonction récursive qui prend deux noeuds d'arbre en paramètre et crée un
501
	 * copie du sous arbre du premier noeud, qu'elle concatène au deuxième
502
	 *
503
	 * @param ndPereOriginal
504
	 *            le père des noeuds de l'arbre original
505
	 * @param ndPereCopie
506
	 *            le père qui va recevoir les copies
507
	 */
508
	private void copierFilsNoeud(Node ndPereOriginal, TreeNode ndPereCopie) {
509
		if (ndPereCopie != null && ndPereOriginal != null) {
510
			Node[] ndNodeFils = ndPereOriginal.getChildNodes();
511
 
512
			for (int i = 0; i < ndNodeFils.length; i++) {
513
 
514
				String[] usObj = (String[]) ndNodeFils[i].getUserObject();
515
				TreeNode child = new TreeNode(usObj[0]);
516
				child.setUserObject(usObj);
517
				ndPereCopie.appendChild(child);
518
 
519
				if (!ndNodeFils[i].isLeaf()) {
520
					copierFilsNoeud(ndNodeFils[i], child);
521
				}
522
 
523
			}
524
		}
525
	}
526
 
527
	/**
528
	 * Méthode héritée de Filtrable Renvoie l'état du filtre (modifié ou non)
529
	 */
530
	public boolean renvoyerEtatFiltre() {
531
 
532
		return filtreModifie;
533
	}
534
 
535
	public void valider() {
86 jpm 536
 
54 david 537
		if (estInstancie) {
86 jpm 538
 
54 david 539
		}
540
	}
90 jpm 541
 
542
	public Comparator<TreeNode> comparerNoeuds()
543
	{
544
		return new Comparator<TreeNode>() {
54 david 545
 
90 jpm 546
			public int compare(TreeNode o1, TreeNode o2) {
547
 
94 jpm 548
				if(o1.getDepth() == 1 && o2.getDepth() == 1 && o1.getText().equals("Inconnue") && o2.getText().equals("Inconnue"))
91 jpm 549
				{
550
					Integer n1 = Integer.parseInt(((String[])o1.getUserObject())[0]) ;
551
					Integer n2 = Integer.parseInt(((String[])o2.getUserObject())[0]) ;
552
 
553
					return n1.compareTo(n2) ;
554
				}
555
				else
94 jpm 556
				{
91 jpm 557
					String n1 = o1.getText() ;
558
					String n2 = o2.getText() ;
90 jpm 559
 
91 jpm 560
					return n1.compareTo(n2) ;
561
				}
90 jpm 562
			}
563
 
564
		} ;
565
	}
104 jpm 566
 
567
	public void raz() {
568
 
569
		arbreCharge = false ;
570
		arbreEntitesGeographiques.clear() ;
571
 
572
		// on crée une racine pour l'arbre
573
		TreeNode root = new TreeNode("Localités");
574
		root.setId("racine_entite");
575
		String[] usObject = { "Localités" };
576
		root.setUserObject(usObject);
577
 
578
		arbreEntitesGeographiques.setRootNode(root);
579
 
580
		arbreEntitesGeographiques.getRootNode().addListener(new TreeNodeListenerAdapter() {
581
 
582
			public void onExpand(Node node) {
583
				if(!arbreCharge)
584
				{
585
					observationMediateur.obtenirDatesObservation() ;
586
					arbreCharge = true ;
587
				}
588
			}
589
 
590
		}) ;
591
 
592
		arbreCharge = false ;
593
 
594
		this.doLayout() ;
595
 
596
	}
91 jpm 597
 
54 david 598
}