Subversion Repositories eFlore/Applications.cel

Rev

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

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