Subversion Repositories eFlore/Applications.cel

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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