Subversion Repositories eFlore/Applications.cel

Rev

Rev 87 | Rev 90 | 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
 
68 david 3
import java.util.Iterator;
4
 
54 david 5
import org.tela_botanica.client.interfaces.Filtrable;
6
import org.tela_botanica.client.interfaces.Rafraichissable;
68 david 7
import org.tela_botanica.client.modeles.EntiteGeographiqueObservation;
8
import org.tela_botanica.client.modeles.ListeEntiteGeographiqueObservation;
89 jpm 9
import org.tela_botanica.client.modeles.Observation;
54 david 10
import org.tela_botanica.client.observation.ObservationMediateur;
11
 
72 david 12
import com.google.gwt.core.client.GWT;
68 david 13
import com.google.gwt.json.client.JSONArray;
14
import com.google.gwt.json.client.JSONObject;
15
import com.google.gwt.user.client.Window;
54 david 16
import com.google.gwt.user.client.ui.Label;
17
import com.gwtext.client.data.Node;
18
import com.gwtext.client.data.NodeTraversalCallback;
19
import com.gwtext.client.data.Tree;
87 jpm 20
import com.gwtext.client.data.event.NodeListenerAdapter;
54 david 21
import com.gwtext.client.widgets.Component;
22
import com.gwtext.client.widgets.Panel;
23
import com.gwtext.client.widgets.event.PanelListenerAdapter;
24
import com.gwtext.client.widgets.tree.TreeNode;
25
import com.gwtext.client.widgets.tree.TreePanel;
87 jpm 26
import com.gwtext.client.widgets.tree.event.TreeNodeListenerAdapter;
85 jpm 27
import com.gwtext.client.widgets.tree.event.TreePanelListenerAdapter;
28
import com.gwtext.client.core.EventObject;
54 david 29
 
30
/**
31
 * fenêtre de recherche affichant l'arbre des mots clés en lecture et un bouton
32
 * cliquable
33
 *
34
 * @author aurelien
35
 *
36
 */
68 david 37
public class ArbreEntiteGeographiqueObservationFiltreVue extends Panel implements Rafraichissable,
54 david 38
		Filtrable {
39
 
40
	/**
41
	 * Le médiateur associé à la vue
42
	 */
43
	private ObservationMediateur	observationMediateur		= null;
44
 
45
	/**
46
	 * Les localites en cours
47
	 */
68 david 48
	private String entitesGeographiquesEncours = "";
54 david 49
 
50
	/**
51
	 * Le treepanel qui affiche l'arbre
52
	 */
68 david 53
	private TreePanel arbreEntitesGeographiques = null;
54 david 54
 
68 david 55
 
56
 
54 david 57
	/**
68 david 58
	 * La structure de donnees qui stocke l'arbre. Utilisee a ce niveau car trop liee a la vue
59
	 */
60
 
61
	private Tree donneeEntitesGeographiques = new Tree();;
62
 
63
	/**
54 david 64
	 * booléen d'initialisation
65
	 */
66
	private boolean estInstancie = false;
67
 
68
	/**
69
	 * booléen d'etat
70
	 */
71
	private boolean filtreModifie = false;
85 jpm 72
 
73
	private boolean arbreCharge = false ;
74
 
86 jpm 75
	private String nomFiltre = "" ;
76
 
54 david 77
	/**
78
	 * Constructeur sans argument (privé car ne doit pas être utilisé)
79
	 */
80
	@SuppressWarnings("unused")
68 david 81
	private ArbreEntiteGeographiqueObservationFiltreVue() {
54 david 82
		super();
83
	}
84
 
85
	/**
86
	 * Constructeur avec paramètres
87
	 *
88
	 * @param im
89
	 *            le médiateur à associer
90
	 */
68 david 91
	public ArbreEntiteGeographiqueObservationFiltreVue(ObservationMediateur obs) {
54 david 92
 
93
		// on crée le panel
70 david 94
		super("Localité");
54 david 95
 
96
		this.observationMediateur = obs;
97
 
68 david 98
		arbreEntitesGeographiques = new TreePanel();
54 david 99
 
100
		this.setPaddings(5);
101
 
102
		this.setBorder(false);
103
		this.setCollapsible(true);
104
		this.setAutoWidth(true);
105
 
106
 
107
		// on ajoute les listeners
108
		ajouterListenersPanel();
109
		estInstancie = false;
110
	}
111
 
112
	/**
113
	 * Ajoute les listeners pour le rendu du panel
114
	 */
115
	private void ajouterListenersPanel() {
68 david 116
		  this.addListener(new PanelListenerAdapter() {
54 david 117
 
118
			// on instancie réellement les composants au moment du rendu pour
119
			// accélérer l'affichage
120
			// et éviter des bugs
121
			public void onRender(Component component) {
122
 
123
				// on interdit le drag and drop dans l'arbre
68 david 124
				arbreEntitesGeographiques.setEnableDD(false);
73 david 125
				arbreEntitesGeographiques.setId("x-view-tree-filter-entity");
72 david 126
				arbreEntitesGeographiques.setAutoWidth(false);
127
 
128
				if (GWT.isScript()) {
129
					arbreEntitesGeographiques.setAutoScroll(true);
130
				}
131
 
132
				arbreEntitesGeographiques.setBorder(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
						{
181
							observationMediateur.obtenirListeEntiteGeographique() ;
182
							arbreCharge = true ;
183
						}
86 jpm 184
						else
185
						{
186
							observationMediateur.obtenirNombreObservation() ;
187
						}
85 jpm 188
						return ;
87 jpm 189
					case 3: nomFiltre += "lieudit,location,id_location";
190
						nomPere = ((String[])node.getParentNode().getUserObject())[0] ;
191
						nomGrandPere = ((String[])node.getParentNode().getParentNode().getUserObject())[0] ;
192
						entitesGeographiquesEncours += node.getText()+","+nomPere+","+nomGrandPere ;
85 jpm 193
						break;
87 jpm 194
					case 2: nomFiltre += "location,id_location";
195
						nomPere = ((String[])node.getParentNode().getUserObject())[0] ;
196
						entitesGeographiquesEncours += node.getText()+","+nomPere ;
85 jpm 197
						break;
87 jpm 198
					case 1: nomFiltre += "id_location";
199
						entitesGeographiquesEncours += node.getText() ;
85 jpm 200
						break;
201
					default:
202
						break;
203
				}
204
 
205
				filtreModifie = true ;
86 jpm 206
				observationMediateur.obtenirNombreObservation() ;
85 jpm 207
 
208
			}
209
 
210
		}) ;
87 jpm 211
 
212
		arbreEntitesGeographiques.getRootNode().addListener(new TreeNodeListenerAdapter() {
213
 
214
			public void onExpand(Node node) {
215
				if(!arbreCharge)
216
				{
217
					observationMediateur.obtenirListeEntiteGeographique() ;
218
					arbreCharge = true ;
219
				}
220
			}
221
 
222
		}) ;
54 david 223
	}
224
 
225
	/**
226
	 * Méthode héritée de l'interface rafraichissable
227
	 */
228
	public void rafraichir(Object nouvelleDonnees,
229
			boolean repandreRaffraichissement) {
230
 
231
 
68 david 232
		if (nouvelleDonnees instanceof ListeEntiteGeographiqueObservation) {
233
 
89 jpm 234
			String id_location=null;
235
			String location=null;
236
			String lieuDit=null;
68 david 237
 
238
			ListeEntiteGeographiqueObservation data = (ListeEntiteGeographiqueObservation) nouvelleDonnees ;
239
 
240
				// on crée un arbre vide
241
				TreeNode root = new TreeNode();
72 david 242
				root.setId("racine_entite");
243
				root.setText("Localités");
89 jpm 244
				String[] usObjRoot = { "Localités"};
245
				root.setUserObject(usObjRoot);
68 david 246
				donneeEntitesGeographiques.setRootNode(root);
247
 
83 david 248
			// on la parse et on récupère les informations qui nous interessent
68 david 249
			for (Iterator<String> it= data.keySet().iterator(); it.hasNext();) {
250
 
251
				EntiteGeographiqueObservation ent=(EntiteGeographiqueObservation) data.get(it.next());
252
 
89 jpm 253
				id_location=ent.getIdLocation();
254
				location=ent.getLocation();
255
				lieuDit=ent.getLieuDit();
73 david 256
 
89 jpm 257
				if(id_location.contains("000null")) {
258
					id_location="Inconnue" ;
85 jpm 259
				}
73 david 260
 
89 jpm 261
				if(location.contains("000null")) {
262
					location="Inconnue" ;
263
				}
68 david 264
 
89 jpm 265
				if(lieuDit.contains("000null")) {
266
					lieuDit="Inconnue" ;
267
				}
268
 
269
				Node noeudMemeId = donneeEntitesGeographiques.getNodeById(""+id_location);
270
				// si la région existe déjà
271
					if(noeudMemeId != null)
272
					{
273
						// on teste si la localité existe
274
						Node noeudMemeLoc = donneeEntitesGeographiques.getNodeById(""+(id_location+location));
275
						if(noeudMemeLoc != null)
85 jpm 276
						{
89 jpm 277
							// enfin on teste si le lieu dit existe
278
							Node noeudMemeLieu = donneeEntitesGeographiques.getNodeById(""+(id_location+location+lieuDit));
279
							if(noeudMemeLieu != null)
280
							{
281
								// tous les noeuds existent déjà, normalement ça ne devrait pas arriver
282
							}
283
							else
284
							{
285
								// enfin on ne crée que le noeud du lieu dit
286
								TreeNode node_lieu = new TreeNode();
287
								node_lieu.setId(""+(id_location+location+lieuDit));
288
								node_lieu.setText(lieuDit);
289
								noeudMemeLoc.appendChild(node_lieu) ;
290
								String[] usObj = {lieuDit};
291
								node_lieu.setUserObject(usObj);
292
							}
85 jpm 293
						}
89 jpm 294
						else
295
						{
296
							TreeNode node_loc = new TreeNode();
297
							node_loc.setId(""+(id_location+location));
298
							node_loc.setText(location);
299
							noeudMemeId.appendChild(node_loc) ;
300
							String[] usObj = {location};
301
							node_loc.setUserObject(usObj);
302
 
303
							TreeNode node_lieu = new TreeNode();
304
							node_lieu.setId(""+(id_location+location+lieuDit));
305
							node_lieu.setText(lieuDit);
306
							node_loc.appendChild(node_lieu) ;
307
							String[] usObj2 = {lieuDit};
308
							node_lieu.setUserObject(usObj2);
309
 
310
						}
68 david 311
					}
89 jpm 312
					else
313
					{
314
						TreeNode node_id_loc = new TreeNode();
315
						node_id_loc.setId(""+id_location);
316
						node_id_loc.setText(id_location);
317
						root.appendChild(node_id_loc) ;
318
						String[] usObj = {id_location};
319
						node_id_loc.setUserObject(usObj);
320
 
321
						TreeNode node_loc = new TreeNode();
322
						node_loc.setId(""+(id_location+location));
323
						node_loc.setText(location);
324
						node_id_loc.appendChild(node_loc) ;
325
						String[] usObj2 = {location};
326
						node_loc.setUserObject(usObj2);
327
 
328
						TreeNode node_lieu = new TreeNode();
329
						node_lieu.setId(""+(id_location+location+lieuDit));
330
						node_lieu.setText(lieuDit);
331
						node_loc.appendChild(node_lieu) ;
332
						String[] usObj3 = {lieuDit};
333
						node_lieu.setUserObject(usObj3);
334
					}
335
 
68 david 336
				}
337
 
89 jpm 338
				// on vide tous les noeuds
339
				arbreEntitesGeographiques.getRootNode().eachChild(new NodeTraversalCallback() {
340
 
341
					public boolean execute(Node node) {
342
 
343
						node.remove();
344
						return true;
345
					}
346
 
347
				});
54 david 348
 
89 jpm 349
				// et on recopie le nouvel arbre
350
				copierFilsNoeud(donneeEntitesGeographiques.getRootNode(), arbreEntitesGeographiques
351
						.getRootNode());
352
 
353
				// si l'arbre n'était pas encore considéré comme instancié
354
				if (!estInstancie) {
355
					// on signale que oui
356
					estInstancie = true;
54 david 357
				}
89 jpm 358
 
359
				// l'état du filtre est réinitialisé
360
				filtreModifie = false;
361
				//show() ;
362
				doLayout();
54 david 363
 
364
			}
89 jpm 365
 
366
		if(nouvelleDonnees instanceof Observation)
367
		{
368
			Observation obs = (Observation)nouvelleDonnees ;
369
 
370
			String id_location= obs.getIdentifiantLocalite();
371
			id_location.replaceAll("\"", "") ;
372
			String location=obs.getLocalite();
373
			String lieuDit=obs.getLieudit();
374
 
375
			Node root = arbreEntitesGeographiques.getRootNode() ;
376
 
377
			if(id_location.contains("000null") || id_location.equals(null)) {
378
				id_location="Inconnue" ;
379
			}
380
 
381
			if(location.contains("000null") || location.equals(null)) {
382
				location="Inconnue" ;
383
			}
384
 
385
			if(lieuDit.contains("000null") || lieuDit.equals(null)) {
386
				lieuDit="Inconnue" ;
387
			}
388
 
389
			Node noeudMemeId = donneeEntitesGeographiques.getNodeById(""+id_location);
390
			// si la région existe déjà
391
				if(noeudMemeId != null)
392
				{
393
					// on teste si la localité existe
394
					Node noeudMemeLoc = donneeEntitesGeographiques.getNodeById(""+(id_location+location));
395
					if(noeudMemeLoc != null)
396
					{
397
						// enfin on teste si le lieu dit existe
398
						Node noeudMemeLieu = donneeEntitesGeographiques.getNodeById(""+(id_location+location+lieuDit));
399
						if(noeudMemeLieu != null)
400
						{
401
							// tous les noeuds existent déjà, normalement ça ne devrait pas arriver
402
						}
403
						else
404
						{
405
							// enfin on ne crée que le noeud du lieu dit
406
							TreeNode node_lieu = new TreeNode();
407
							node_lieu.setId(""+(id_location+location+lieuDit));
408
							node_lieu.setText(lieuDit);
409
							String[] usObj = {lieuDit};
410
							node_lieu.setUserObject(usObj);
411
							noeudMemeLoc.appendChild(node_lieu) ;
412
						}
413
					}
414
					else
415
					{
416
						TreeNode node_loc = new TreeNode();
417
						node_loc.setId(""+(id_location+location));
418
						node_loc.setText(location);
419
						String[] usObj = {location};
420
						node_loc.setUserObject(usObj);
421
						noeudMemeId.appendChild(node_loc) ;
422
 
423
						TreeNode node_lieu = new TreeNode();
424
						node_lieu.setId(""+(id_location+location+lieuDit));
425
						node_lieu.setText(lieuDit);
426
						String[] usObj2 = {lieuDit};
427
						node_lieu.setUserObject(usObj2);
428
						node_loc.appendChild(node_lieu) ;
429
 
430
					}
431
			}
432
			else
433
			{
434
				TreeNode node_id_loc = new TreeNode();
435
				node_id_loc.setId(""+id_location);
436
				node_id_loc.setText(id_location);
437
				String[] usObj = {id_location};
438
				node_id_loc.setUserObject(usObj);
439
				root.appendChild(node_id_loc) ;
440
 
441
				TreeNode node_loc = new TreeNode();
442
				node_loc.setId(""+(id_location+location));
443
				node_loc.setText(location);
444
				String[] usObj2 = {location};
445
				node_loc.setUserObject(usObj2);
446
				node_id_loc.appendChild(node_loc) ;
447
 
448
				TreeNode node_lieu = new TreeNode();
449
				node_lieu.setId(""+(id_location+location+lieuDit));
450
				node_lieu.setText(lieuDit);
451
				String[] usObj3 = {lieuDit};
452
				node_lieu.setUserObject(usObj3);
453
				node_loc.appendChild(node_lieu) ;
454
			}
455
 
456
				arbreEntitesGeographiques.doLayout() ;
457
		}
68 david 458
	}
54 david 459
 
460
 
461
	/**
462
	 * Accesseur pour le panneau contenant l'arbre
463
	 *
464
	 * @return le panneau de l'arbre des mots clés
465
	 */
466
	public TreePanel getArbreMotsCles() {
68 david 467
		return arbreEntitesGeographiques;
54 david 468
	}
469
 
470
	/**
471
	 * Méthode héritée de Filtrable renvoie le nom du filtre
472
	 */
473
	public String renvoyerNomFiltre() {
474
 
475
		return "Localités";
476
	}
477
 
478
	/**
479
	 * Renvoie un tableau contenant le nom du champ à filtrer et la valeur
480
	 *
481
	 * @return un tableau contenant le nom du champ à filtrer et sa valeur
482
	 */
483
	public String[] renvoyerValeursAFiltrer() {
484
 
485
		valider();
85 jpm 486
 
86 jpm 487
		String valeursFiltrees[] = {nomFiltre, entitesGeographiquesEncours } ;
54 david 488
 
489
		return valeursFiltrees;
490
	}
491
 
492
	/**
493
	 * Fonction récursive qui prend deux noeuds d'arbre en paramètre et crée un
494
	 * copie du sous arbre du premier noeud, qu'elle concatène au deuxième
495
	 *
496
	 * @param ndPereOriginal
497
	 *            le père des noeuds de l'arbre original
498
	 * @param ndPereCopie
499
	 *            le père qui va recevoir les copies
500
	 */
501
	private void copierFilsNoeud(Node ndPereOriginal, TreeNode ndPereCopie) {
502
		if (ndPereCopie != null && ndPereOriginal != null) {
503
			Node[] ndNodeFils = ndPereOriginal.getChildNodes();
504
 
505
			for (int i = 0; i < ndNodeFils.length; i++) {
506
 
507
				String[] usObj = (String[]) ndNodeFils[i].getUserObject();
508
				TreeNode child = new TreeNode(usObj[0]);
509
				child.setUserObject(usObj);
510
				ndPereCopie.appendChild(child);
511
 
512
				if (!ndNodeFils[i].isLeaf()) {
513
					copierFilsNoeud(ndNodeFils[i], child);
514
				}
515
 
516
			}
517
		}
518
	}
519
 
520
	/**
521
	 * Méthode héritée de Filtrable Renvoie l'état du filtre (modifié ou non)
522
	 */
523
	public boolean renvoyerEtatFiltre() {
524
 
525
		return filtreModifie;
526
	}
527
 
528
	public void valider() {
86 jpm 529
 
54 david 530
		if (estInstancie) {
86 jpm 531
 
54 david 532
		}
533
	}
534
 
535
}