Subversion Repositories eFlore/Applications.cel

Rev

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

Rev Author Line No. Line
247 aurelien 1
package org.tela_botanica.client.vues.observation;
2
 
2553 mathias 3
import org.tela_botanica.client.cel2;
247 aurelien 4
import org.tela_botanica.client.interfaces.Rafraichissable;
989 aurelien 5
import org.tela_botanica.client.modeles.objets.EntiteGeographiqueObservation;
247 aurelien 6
import org.tela_botanica.client.observation.ObservationMediateur;
965 aurelien 7
import org.tela_botanica.client.util.Util;
247 aurelien 8
 
268 aurelien 9
import com.google.gwt.user.client.Timer;
247 aurelien 10
import com.google.gwt.user.client.Window;
11
import com.gwtext.client.core.EventCallback;
12
import com.gwtext.client.core.EventObject;
13
import com.gwtext.client.core.Ext;
14
import com.gwtext.client.core.ExtElement;
15
import com.gwtext.client.widgets.Container;
16
import com.gwtext.client.widgets.Panel;
17
import com.gwtext.client.widgets.event.ContainerListenerAdapter;
18
import com.gwtext.client.widgets.form.Label;
19
import com.gwtext.client.widgets.layout.FitLayout;
20
import com.gwtext.client.widgets.layout.RowLayout;
21
import com.gwtext.client.widgets.layout.RowLayoutData;
1958 aurelien 22
import com.google.maps.gwt.client.GoogleMap;
23
import com.google.maps.gwt.client.GoogleMap.ClickHandler;
24
import com.google.maps.gwt.client.GoogleMap.ZoomChangedHandler;
25
import com.google.maps.gwt.client.InfoWindow;
26
import com.google.maps.gwt.client.InfoWindowOptions;
27
import com.google.maps.gwt.client.LatLng;
28
import com.google.maps.gwt.client.MapOptions;
29
import com.google.maps.gwt.client.MapTypeId;
30
import com.google.maps.gwt.client.Marker;
31
import com.google.maps.gwt.client.MarkerOptions;
32
import com.google.maps.gwt.client.MouseEvent;
247 aurelien 33
 
34
public class CartographieObservationVue extends Panel implements Rafraichissable {
35
 
36
	private String id = "cartographie_observation";
37
	private ObservationMediateur oMediateur = null;
38
 
39
	Panel panneauCarto = null;
1958 aurelien 40
	GoogleMap map = null;
41
	Marker nmk = null;
2553 mathias 42
 
247 aurelien 43
	private boolean carteAjoutee = false;
1958 aurelien 44
	private boolean infoOuverte = false;
2553 mathias 45
 
2270 mathias 46
	String valeurCommune = "Bourges"; // j'aurais préféré Bort-les-Orgues
296 aurelien 47
	LatLng centreFrance = null;
299 aurelien 48
	LatLng pointCommune = null;
2558 aurelien 49
	String codeLoc = "";
2553 mathias 50
 
681 aurelien 51
	int niveauZoom = 6;
247 aurelien 52
 
53
	Label panneauExplication = new Label(
54
			"Précisez le lieu de votre observation et cliquez sur OK. Les coordonnées sont données en degrés décimaux, comptés positivement vers le Nord pour la latitude, et vers l'Est pour la longitude");
55
 
56
	private InfoWindow info;
2275 mathias 57
	private boolean zoomerSurRetour = false;
247 aurelien 58
 
59
	public CartographieObservationVue(ObservationMediateur om) {
2275 mathias 60
 
247 aurelien 61
		oMediateur = om;
2553 mathias 62
 
681 aurelien 63
		setHeader(false);
247 aurelien 64
		setAutoWidth(true);
65
		setPaddings(10);
66
 
67
		panneauCarto = new Panel();
68
		panneauCarto.setLayout(new FitLayout());
69
 
70
		setLayout(new RowLayout());
2553 mathias 71
 
1958 aurelien 72
		panneauCarto.setAutoHeight(true);
247 aurelien 73
		add(panneauExplication, new RowLayoutData(12));
74
		add(panneauCarto, new RowLayoutData());
2553 mathias 75
 
247 aurelien 76
		addListener(new ContainerListenerAdapter() {
1292 aurelien 77
			@Override
247 aurelien 78
			public void onAfterLayout(Container c) {
1958 aurelien 79
				initialiserCarte(false);
247 aurelien 80
			}
81
		});
82
	}
2553 mathias 83
 
681 aurelien 84
	public void setTailleCarte(int hauteur, int largeur) {
1958 aurelien 85
		setSize(largeur, hauteur);
681 aurelien 86
	}
2553 mathias 87
 
247 aurelien 88
	public void masquerChargement(boolean masquer) {
654 aurelien 89
 
90
		if(panneauCarto.isVisible()) {
91
			if(masquer) {
92
				panneauCarto.getEl().mask("Recherche des coordonnées et de la commune, veuillez patienter");
93
			} else {
94
				panneauCarto.getEl().unmask();
247 aurelien 95
			}
96
		}
97
	}
98
 
99
	public void obtenirInfosCommunes(LatLng coord) {
2558 aurelien 100
		// Remise à zéro des infos restantes d'une ancienne requete
101
		valeurCommune = "";
102
		codeLoc = "";
103
 
1958 aurelien 104
		oMediateur.obtenirInformationCoord(this, coord.lat(), coord.lng());
247 aurelien 105
	}
106
 
2558 aurelien 107
	public void mettreAJourInfos(final LatLng pointNouvelleCommune, int zoom) {
1958 aurelien 108
		GoogleMap mapNulle = null;
109
		if(nmk != null) {
110
			nmk.setMap(mapNulle);
247 aurelien 111
		}
112
		pointCommune = pointNouvelleCommune;
2558 aurelien 113
		nmk = fabriquerMarqueur(pointCommune);
1958 aurelien 114
		nmk.setMap(map);
2558 aurelien 115
		afficherInfoMarker(nmk, construireContenuInfoMarker(nmk));
1959 aurelien 116
		recentrerCarte(nmk.getPosition());
2275 mathias 117
		map.setZoom(zoom);
247 aurelien 118
	}
119
 
1958 aurelien 120
	private void initialiserCarte(boolean commune) {
2275 mathias 121
 
1958 aurelien 122
		panneauCarto.clear();
2275 mathias 123
 
1958 aurelien 124
		centreFrance = LatLng.create(47.0504, 2.2347);
299 aurelien 125
		pointCommune = centreFrance;
2275 mathias 126
 
127
		MapOptions options = MapOptions.create();
128
	    options.setCenter(centreFrance);
129
	    options.setZoom(6);
1958 aurelien 130
	    options.setMapTypeId(MapTypeId.HYBRID);
131
	    options.setDraggable(true);
132
	    options.setMapTypeControl(true);
2275 mathias 133
	    options.setScaleControl(true);
134
	    options.setScrollwheel(true);
135
 
136
		map = GoogleMap.create(panneauCarto.getElement(), options );
1958 aurelien 137
 
138
		InfoWindowOptions infopts = InfoWindowOptions.create();
139
		info = InfoWindow.create();
140
		info.setOptions(infopts);
141
		info.addCloseClickListener(new InfoWindow.CloseClickHandler() {
142
			@Override
143
			public void handle() {
144
				infoOuverte = false;
145
			}
146
		});
147
 
268 aurelien 148
		if(!commune) {
149
			fabriquerMarqueurIndication();
150
		}
1958 aurelien 151
 
152
		map.addClickListener(new ClickHandler() {
1292 aurelien 153
			@Override
1958 aurelien 154
			public void handle(MouseEvent event) {
391 aurelien 155
				obtenirInfosCommunes(event.getLatLng());
156
			}
157
		});
158
 
1958 aurelien 159
		map.addZoomChangedListener(new ZoomChangedHandler() {
1292 aurelien 160
			@Override
1958 aurelien 161
			public void handle() {
162
				niveauZoom = (int)map.getZoom();
264 aurelien 163
			}
164
		});
1958 aurelien 165
 
166
		panneauCarto.doLayout();
167
		panneauCarto.expand();
168
		panneauCarto.setHeight("100%");
169
		panneauCarto.setWidth("100%");
170
		expand();
247 aurelien 171
	}
268 aurelien 172
 
173
	public void fabriquerMarqueurIndication() {
1958 aurelien 174
		GoogleMap mapNulle = null;
175
		if(nmk != null) {
176
			nmk.setMap(mapNulle);
177
		}
2558 aurelien 178
		nmk = fabriquerMarqueur(pointCommune);
1958 aurelien 179
		nmk.setMap(map);
180
		info.setContent("<div id=\"info_contenu\">"+
268 aurelien 181
				"Déplacez ce marqueur pour localiser votre observation (commune et coordonnées)<br />"
182
				+"longitude="
2276 mathias 183
				+ Util.tronquerNombrePourAffichage("" + nmk.getPosition().lng(), 5) + "<br />latitude="
184
				+ Util.tronquerNombrePourAffichage("" + nmk.getPosition().lat(), 5) + "</div>");
1958 aurelien 185
		infoOuverte = true;
186
		info.open(map,nmk);
268 aurelien 187
	}
247 aurelien 188
 
189
	public void AfficherMessageAucuneInfos() {
2558 aurelien 190
		mettreAJourInfos(centreFrance, niveauZoom);
247 aurelien 191
		Window.alert("Aucune information pour le nom donné");
192
	}
193
 
194
	public int getNiveauZoom() {
195
		return niveauZoom;
196
	}
197
 
268 aurelien 198
	private void ajouterListenerBouton(final String idBouton) {
199
 
200
		final ExtElement bouton = Ext.get(idBouton);
201
		if(bouton == null) {
202
			Timer t = new Timer() {
1292 aurelien 203
				@Override
268 aurelien 204
				public void run() {
205
					ajouterListenerBouton(idBouton);
206
				}
207
			};
208
			t.schedule(500);
209
			return;
1958 aurelien 210
		} else {
211
			bouton.addListener("click", new EventCallback() {
212
				@Override
213
				public void execute(EventObject e) {
2558 aurelien 214
					EntiteGeographiqueObservation infosCommune = new EntiteGeographiqueObservation(codeLoc,valeurCommune,null,null);
215
					// TODO: ajouter pays à tout Ceci
1958 aurelien 216
					infosCommune.setLat(pointCommune.lat()+"");
217
					infosCommune.setLon(pointCommune.lng()+"");
218
					oMediateur.rafraichirSaisieCommuneObservation(infosCommune);
219
				}
220
			});
221
 
222
			bouton.focus();
268 aurelien 223
		}
247 aurelien 224
	}
225
 
1292 aurelien 226
	@Override
2275 mathias 227
	public void rafraichir(Object nouvelleDonnees, boolean repandreRaffraichissement) {
2553 mathias 228
		// rétablissement du curseur, parfois mis sur "attente" par le processus chargeant la carte
229
		Util.curseurParDefaut();
2275 mathias 230
 
247 aurelien 231
		if (nouvelleDonnees instanceof String) {
232
 
233
			if(isVisible()) {
234
				Window.alert((String)nouvelleDonnees);
235
			}
236
		}
237
 
268 aurelien 238
		if (nouvelleDonnees instanceof EntiteGeographiqueObservation) {
247 aurelien 239
 
681 aurelien 240
			oMediateur.afficherFenetreCarto();
268 aurelien 241
			EntiteGeographiqueObservation infos = (EntiteGeographiqueObservation) nouvelleDonnees;
654 aurelien 242
			Double lat = 0.0;
243
			Double lon = 0.0;
244
 
245
			try {
246
				lat = Double.parseDouble(infos.getLat());
247
				lon = Double.parseDouble(infos.getLon());
248
			} catch (NumberFormatException nbe) {
249
 
250
			}
1958 aurelien 251
			LatLng coord = LatLng.create(lat, lon);
2275 mathias 252
			// si on a un point, on zoome fort dès le début
253
			if (zoomerSurRetour) {
254
				niveauZoom = 16;
255
				zoomerSurRetour = false;
2553 mathias 256
			} else if (infos.getZoom() > 0) {
257
				niveauZoom = infos.getZoom();
2275 mathias 258
			}
2270 mathias 259
 
260
			String nouvelleValeurCommune = infos.getZoneGeo();
261
			valeurCommune = nouvelleValeurCommune;
2558 aurelien 262
			codeLoc = "";
263
			if(infos.getIdZoneGeo() != null) {
264
				if (infos.getPays().equals("FR")) {
265
					codeLoc = Util.convertirChaineZoneGeoVersDepartement(infos.getIdZoneGeo());
266
				} else {
267
					codeLoc = infos.getPays();
268
				}
2553 mathias 269
			}
268 aurelien 270
			pointCommune = coord;
247 aurelien 271
 
2558 aurelien 272
			mettreAJourInfos(coord, niveauZoom);
247 aurelien 273
		}
274
 
275
		masquerChargement(false);
276
	}
277
 
2558 aurelien 278
	public Marker fabriquerMarqueur(LatLng pointMarqueur) {
247 aurelien 279
		// ajout de marqueurs déplacables, qui affichent une bulle d'information
1958 aurelien 280
		MarkerOptions options = MarkerOptions.create();
247 aurelien 281
		options.setDraggable(true);
1958 aurelien 282
		options.setPosition(pointMarqueur);
283
		final Marker marker = Marker.create(options);
247 aurelien 284
 
1958 aurelien 285
		marker.addDragEndListener(new Marker.DragEndHandler() {
286
 
1292 aurelien 287
			@Override
1958 aurelien 288
			public void handle(MouseEvent event) {
2558 aurelien 289
				afficherInfoMarker(marker, construireContenuPartielInfoMarker(marker));
1958 aurelien 290
				obtenirInfosCommunes(event.getLatLng());
247 aurelien 291
			}
292
		});
1958 aurelien 293
 
294
		marker.addDragStartListener(new Marker.DragStartHandler() {
295
 
1292 aurelien 296
			@Override
1958 aurelien 297
			public void handle(MouseEvent event) {
298
				info.close();
247 aurelien 299
			}
300
		});
301
 
1958 aurelien 302
		marker.addClickListener(new Marker.ClickHandler() {
303
 
1292 aurelien 304
			@Override
1958 aurelien 305
			public void handle(MouseEvent event) {
306
				if(infoOuverte) {
247 aurelien 307
					info.close();
1958 aurelien 308
					infoOuverte = false;
247 aurelien 309
				} else {
2558 aurelien 310
					afficherInfoMarker(marker, construireContenuPartielInfoMarker(marker));
1958 aurelien 311
					infoOuverte = true;
247 aurelien 312
				}
1958 aurelien 313
			}
247 aurelien 314
		});
315
		return marker;
316
	}
1959 aurelien 317
 
318
	public void recentrerCarte() {
319
		if(nmk != null && nmk.getVisible()) {
320
			recentrerCarte(nmk.getPosition());
321
		}
322
	}
247 aurelien 323
 
1959 aurelien 324
	public void recentrerCarte(LatLng point) {
1958 aurelien 325
		if(this.getWidth() > 12 && this.getHeight() > 24) {
323 aurelien 326
			panneauCarto.setSize((this.getWidth() - 12)+"px" , (this.getHeight() - 12)+"px");
327
		}
1958 aurelien 328
		panneauCarto.doLayout();
329
		panneauCarto.expand();
330
		panneauCarto.setHeight("100%");
331
		panneauCarto.setWidth("100%");
332
		expand();
1959 aurelien 333
		map.setCenter(point);
247 aurelien 334
	}
335
 
336
	public boolean isCarteAjoutee() {
337
		return carteAjoutee;
338
	}
339
 
2558 aurelien 340
	public String construireContenuInfoMarker(Marker marker) {
247 aurelien 341
		final String htmlBoutonOk = "<br /><button id=\"okMap\" class=\"x-btn-text\" type=\"button\">OK</button>";
342
 
276 aurelien 343
		String contenuMarker = "";
344
 
345
		if(valeurCommune == null || valeurCommune.trim().equals("")) {
1958 aurelien 346
			if(marker.getPosition().lng() == 0 && marker.getPosition().lat() == 0) {
681 aurelien 347
				LatLng point = centreFrance;
1958 aurelien 348
				marker.setPosition(point);
681 aurelien 349
				niveauZoom = 6;
350
			}
2558 aurelien 351
			contenuMarker =
352
			"<div id=\"info_contenu\">"+
353
				"Aucun nom de lieu trouvé à cet emplacement <br />"
354
				+"Déplacez ce marqueur pour localiser votre observation (nom de lieu et coordonnées)<br />"
355
				+"longitude="+Util.tronquerNombrePourAffichage(""+marker.getPosition().lng(), 5)+"<br />"
356
				+"latitude="+Util.tronquerNombrePourAffichage(""+marker.getPosition().lat(), 5)+
357
			"</div>";
276 aurelien 358
		} else {
2558 aurelien 359
			String chaineCommune = valeurCommune + ((codeLoc.isEmpty()) ? "" : " ("+codeLoc+")");
360
			contenuMarker =
361
			"<div id=\"info_contenu\">"+
362
					chaineCommune+"<br />"
363
					+"longitude="+Util.tronquerNombrePourAffichage(""+marker.getPosition().lng(), 5)+"<br />"
364
					+"latitude="+Util.tronquerNombrePourAffichage(""+marker.getPosition().lat(), 5)+
365
			"</div>";
276 aurelien 366
		}
367
 
2558 aurelien 368
		return contenuMarker+ htmlBoutonOk;
369
	}
370
 
371
	public String construireContenuPartielInfoMarker(Marker marker) {
372
		final String htmlBoutonOk = "<br /><button id=\"okMap\" class=\"x-btn-text\" type=\"button\">OK</button>";
373
 
374
		String contenuMarker = "";
268 aurelien 375
 
2558 aurelien 376
		contenuMarker =
377
			"<div id=\"info_contenu\">"+
378
					"<div class=\"recherche_infos_zone_geo\">"+
379
						"<div class=\"recherche_infos_zone_geo_en_cours\">"+
380
							"<img src=\"chargement_carte.gif\" />"+
381
							"<div>"+
382
								"Recherche du nom de lieu en cours... <br />"+
383
								"Cela peut prendre un moment"+
384
							"</div>"+
385
							"<hr class=\"nettoyage\" />"+
386
						"</div>"+
387
						"Si vous ne désirez que les coordonnées, vous pouvez cliquer dès maintenant sur OK"+
388
					"</div>"+
389
					"longitude="+Util.tronquerNombrePourAffichage("" + marker.getPosition().lng(), 5)+"<br />"+
390
					"latitude="+Util.tronquerNombrePourAffichage("" + marker.getPosition().lat(), 5)+
391
			"</div>";
392
 
393
		return contenuMarker+ htmlBoutonOk;
394
	}
395
 
396
	public void afficherInfoMarker(Marker marker, String contenuInfo) {
397
 
398
		info.setContent(contenuInfo);
399
		info.open(map, marker);
400
 
401
		pointCommune = marker.getPosition();
402
 
1958 aurelien 403
		info.addDomReadyListenerOnce(new InfoWindow.DomReadyHandler() {
404
			@Override
405
			public void handle() {
406
				ajouterListenerBouton("okMap");
407
			}
408
		});
409
 
1959 aurelien 410
		recentrerCarte(marker.getPosition());
247 aurelien 411
	}
2558 aurelien 412
 
413
	public void afficherCarteEnAttenteAvecMarker(double lat, double lon) {
414
		pointCommune = LatLng.create(lat, lon);
415
		nmk.setPosition(pointCommune);
416
		afficherInfoMarker(nmk, construireContenuPartielInfoMarker(nmk));
417
	}
1958 aurelien 418
 
419
	public double[] getCoordsCentreFrance() {
420
		double[] coords = {47.0504, 2.2347};
421
		return coords;
654 aurelien 422
	}
2275 mathias 423
 
424
	public void setDoitZoomerSurRetour(boolean b) {
425
		zoomerSurRetour  = b;
426
	}
247 aurelien 427
}