Subversion Repositories eFlore/Applications.cel

Rev

Rev 2627 | 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
 
2627 mathias 3
import org.tela_botanica.client.i18n.Msg;
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
 
2568 aurelien 46
	String valeurLocalite = "Bourges"; // j'aurais préféré Bort-les-Orgues
296 aurelien 47
	LatLng centreFrance = null;
2568 aurelien 48
	LatLng pointLocalite = null;
2558 aurelien 49
	String codeLoc = "";
2568 aurelien 50
	String pays = "";
2553 mathias 51
 
681 aurelien 52
	int niveauZoom = 6;
247 aurelien 53
 
2627 mathias 54
	Label panneauExplication = new Label(Msg.get("explication-panneau-carto"));
247 aurelien 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) {
2629 mathias 92
				panneauCarto.getEl().mask(Msg.get("recherche-coordonnees-localite"));
654 aurelien 93
			} else {
94
				panneauCarto.getEl().unmask();
247 aurelien 95
			}
96
		}
97
	}
98
 
2568 aurelien 99
	public void obtenirInfosLocalites(LatLng coord) {
2558 aurelien 100
		// Remise à zéro des infos restantes d'une ancienne requete
2568 aurelien 101
		valeurLocalite = "";
2558 aurelien 102
		codeLoc = "";
2568 aurelien 103
		pays = "";
2558 aurelien 104
 
1958 aurelien 105
		oMediateur.obtenirInformationCoord(this, coord.lat(), coord.lng());
247 aurelien 106
	}
107
 
2568 aurelien 108
	public void mettreAJourInfos(final LatLng pointNouvelleLocalite, int zoom) {
1958 aurelien 109
		GoogleMap mapNulle = null;
110
		if(nmk != null) {
111
			nmk.setMap(mapNulle);
247 aurelien 112
		}
2568 aurelien 113
		pointLocalite = pointNouvelleLocalite;
114
		nmk = fabriquerMarqueur(pointLocalite);
1958 aurelien 115
		nmk.setMap(map);
2558 aurelien 116
		afficherInfoMarker(nmk, construireContenuInfoMarker(nmk));
1959 aurelien 117
		recentrerCarte(nmk.getPosition());
2275 mathias 118
		map.setZoom(zoom);
247 aurelien 119
	}
120
 
2568 aurelien 121
	private void initialiserCarte(boolean localite) {
2275 mathias 122
 
1958 aurelien 123
		panneauCarto.clear();
2275 mathias 124
 
1958 aurelien 125
		centreFrance = LatLng.create(47.0504, 2.2347);
2568 aurelien 126
		pointLocalite = centreFrance;
2275 mathias 127
 
128
		MapOptions options = MapOptions.create();
129
	    options.setCenter(centreFrance);
130
	    options.setZoom(6);
1958 aurelien 131
	    options.setMapTypeId(MapTypeId.HYBRID);
132
	    options.setDraggable(true);
133
	    options.setMapTypeControl(true);
2275 mathias 134
	    options.setScaleControl(true);
135
	    options.setScrollwheel(true);
136
 
137
		map = GoogleMap.create(panneauCarto.getElement(), options );
1958 aurelien 138
 
139
		InfoWindowOptions infopts = InfoWindowOptions.create();
140
		info = InfoWindow.create();
141
		info.setOptions(infopts);
142
		info.addCloseClickListener(new InfoWindow.CloseClickHandler() {
143
			@Override
144
			public void handle() {
145
				infoOuverte = false;
146
			}
147
		});
148
 
2568 aurelien 149
		if(!localite) {
268 aurelien 150
			fabriquerMarqueurIndication();
151
		}
1958 aurelien 152
 
153
		map.addClickListener(new ClickHandler() {
1292 aurelien 154
			@Override
1958 aurelien 155
			public void handle(MouseEvent event) {
2568 aurelien 156
				obtenirInfosLocalites(event.getLatLng());
391 aurelien 157
			}
158
		});
159
 
1958 aurelien 160
		map.addZoomChangedListener(new ZoomChangedHandler() {
1292 aurelien 161
			@Override
1958 aurelien 162
			public void handle() {
163
				niveauZoom = (int)map.getZoom();
264 aurelien 164
			}
165
		});
1958 aurelien 166
 
167
		panneauCarto.doLayout();
168
		panneauCarto.expand();
169
		panneauCarto.setHeight("100%");
170
		panneauCarto.setWidth("100%");
171
		expand();
247 aurelien 172
	}
268 aurelien 173
 
174
	public void fabriquerMarqueurIndication() {
1958 aurelien 175
		GoogleMap mapNulle = null;
176
		if(nmk != null) {
177
			nmk.setMap(mapNulle);
178
		}
2568 aurelien 179
		nmk = fabriquerMarqueur(pointLocalite);
1958 aurelien 180
		nmk.setMap(map);
181
		info.setContent("<div id=\"info_contenu\">"+
2629 mathias 182
				Msg.get("deplacez-marqueur-pour-localiser") + "<br/>"
268 aurelien 183
				+"longitude="
2276 mathias 184
				+ Util.tronquerNombrePourAffichage("" + nmk.getPosition().lng(), 5) + "<br />latitude="
185
				+ Util.tronquerNombrePourAffichage("" + nmk.getPosition().lat(), 5) + "</div>");
1958 aurelien 186
		infoOuverte = true;
187
		info.open(map,nmk);
268 aurelien 188
	}
247 aurelien 189
 
190
	public void AfficherMessageAucuneInfos() {
2558 aurelien 191
		mettreAJourInfos(centreFrance, niveauZoom);
2629 mathias 192
		Window.alert(Msg.get("aucune-information"));
247 aurelien 193
	}
194
 
195
	public int getNiveauZoom() {
196
		return niveauZoom;
197
	}
198
 
268 aurelien 199
	private void ajouterListenerBouton(final String idBouton) {
200
 
201
		final ExtElement bouton = Ext.get(idBouton);
202
		if(bouton == null) {
203
			Timer t = new Timer() {
1292 aurelien 204
				@Override
268 aurelien 205
				public void run() {
206
					ajouterListenerBouton(idBouton);
207
				}
208
			};
209
			t.schedule(500);
210
			return;
1958 aurelien 211
		} else {
212
			bouton.addListener("click", new EventCallback() {
213
				@Override
214
				public void execute(EventObject e) {
2568 aurelien 215
					EntiteGeographiqueObservation infosLocalite = new EntiteGeographiqueObservation(codeLoc,valeurLocalite,null,null);
216
					infosLocalite.setPays(pays);
217
					infosLocalite.setLat(pointLocalite.lat()+"");
218
					infosLocalite.setLon(pointLocalite.lng()+"");
219
					oMediateur.rafraichirSaisieLocaliteObservation(infosLocalite);
1958 aurelien 220
				}
221
			});
222
 
223
			bouton.focus();
268 aurelien 224
		}
247 aurelien 225
	}
226
 
1292 aurelien 227
	@Override
2275 mathias 228
	public void rafraichir(Object nouvelleDonnees, boolean repandreRaffraichissement) {
2553 mathias 229
		// rétablissement du curseur, parfois mis sur "attente" par le processus chargeant la carte
230
		Util.curseurParDefaut();
2275 mathias 231
 
247 aurelien 232
		if (nouvelleDonnees instanceof String) {
233
 
234
			if(isVisible()) {
235
				Window.alert((String)nouvelleDonnees);
236
			}
237
		}
238
 
268 aurelien 239
		if (nouvelleDonnees instanceof EntiteGeographiqueObservation) {
247 aurelien 240
 
681 aurelien 241
			oMediateur.afficherFenetreCarto();
268 aurelien 242
			EntiteGeographiqueObservation infos = (EntiteGeographiqueObservation) nouvelleDonnees;
654 aurelien 243
			Double lat = 0.0;
244
			Double lon = 0.0;
245
 
246
			try {
247
				lat = Double.parseDouble(infos.getLat());
248
				lon = Double.parseDouble(infos.getLon());
249
			} catch (NumberFormatException nbe) {
250
 
251
			}
1958 aurelien 252
			LatLng coord = LatLng.create(lat, lon);
2275 mathias 253
			// si on a un point, on zoome fort dès le début
254
			if (zoomerSurRetour) {
255
				niveauZoom = 16;
256
				zoomerSurRetour = false;
2553 mathias 257
			} else if (infos.getZoom() > 0) {
258
				niveauZoom = infos.getZoom();
2275 mathias 259
			}
2270 mathias 260
 
2568 aurelien 261
			String nouvelleValeurLocalite = infos.getZoneGeo();
262
			valeurLocalite = nouvelleValeurLocalite;
2558 aurelien 263
			codeLoc = "";
264
			if(infos.getIdZoneGeo() != null) {
2568 aurelien 265
				// Quoi stocker pour en dehors de la france ?
2558 aurelien 266
				if (infos.getPays().equals("FR")) {
267
					codeLoc = Util.convertirChaineZoneGeoVersDepartement(infos.getIdZoneGeo());
268
				} else {
269
					codeLoc = infos.getPays();
270
				}
2553 mathias 271
			}
2568 aurelien 272
			pays = infos.getPays();
273
			pointLocalite = coord;
247 aurelien 274
 
2558 aurelien 275
			mettreAJourInfos(coord, niveauZoom);
247 aurelien 276
		}
277
 
278
		masquerChargement(false);
279
	}
280
 
2558 aurelien 281
	public Marker fabriquerMarqueur(LatLng pointMarqueur) {
247 aurelien 282
		// ajout de marqueurs déplacables, qui affichent une bulle d'information
1958 aurelien 283
		MarkerOptions options = MarkerOptions.create();
247 aurelien 284
		options.setDraggable(true);
1958 aurelien 285
		options.setPosition(pointMarqueur);
286
		final Marker marker = Marker.create(options);
247 aurelien 287
 
1958 aurelien 288
		marker.addDragEndListener(new Marker.DragEndHandler() {
289
 
1292 aurelien 290
			@Override
1958 aurelien 291
			public void handle(MouseEvent event) {
2558 aurelien 292
				afficherInfoMarker(marker, construireContenuPartielInfoMarker(marker));
2568 aurelien 293
				obtenirInfosLocalites(event.getLatLng());
247 aurelien 294
			}
295
		});
1958 aurelien 296
 
297
		marker.addDragStartListener(new Marker.DragStartHandler() {
298
 
1292 aurelien 299
			@Override
1958 aurelien 300
			public void handle(MouseEvent event) {
301
				info.close();
247 aurelien 302
			}
303
		});
304
 
1958 aurelien 305
		marker.addClickListener(new Marker.ClickHandler() {
306
 
1292 aurelien 307
			@Override
1958 aurelien 308
			public void handle(MouseEvent event) {
309
				if(infoOuverte) {
247 aurelien 310
					info.close();
1958 aurelien 311
					infoOuverte = false;
247 aurelien 312
				} else {
2558 aurelien 313
					afficherInfoMarker(marker, construireContenuPartielInfoMarker(marker));
1958 aurelien 314
					infoOuverte = true;
247 aurelien 315
				}
1958 aurelien 316
			}
247 aurelien 317
		});
318
		return marker;
319
	}
1959 aurelien 320
 
321
	public void recentrerCarte() {
322
		if(nmk != null && nmk.getVisible()) {
323
			recentrerCarte(nmk.getPosition());
324
		}
325
	}
247 aurelien 326
 
1959 aurelien 327
	public void recentrerCarte(LatLng point) {
1958 aurelien 328
		if(this.getWidth() > 12 && this.getHeight() > 24) {
323 aurelien 329
			panneauCarto.setSize((this.getWidth() - 12)+"px" , (this.getHeight() - 12)+"px");
330
		}
1958 aurelien 331
		panneauCarto.doLayout();
332
		panneauCarto.expand();
333
		panneauCarto.setHeight("100%");
334
		panneauCarto.setWidth("100%");
335
		expand();
1959 aurelien 336
		map.setCenter(point);
247 aurelien 337
	}
338
 
339
	public boolean isCarteAjoutee() {
340
		return carteAjoutee;
341
	}
342
 
2558 aurelien 343
	public String construireContenuInfoMarker(Marker marker) {
247 aurelien 344
		final String htmlBoutonOk = "<br /><button id=\"okMap\" class=\"x-btn-text\" type=\"button\">OK</button>";
345
 
276 aurelien 346
		String contenuMarker = "";
347
 
2568 aurelien 348
		if(valeurLocalite == null || valeurLocalite.trim().equals("")) {
1958 aurelien 349
			if(marker.getPosition().lng() == 0 && marker.getPosition().lat() == 0) {
681 aurelien 350
				LatLng point = centreFrance;
1958 aurelien 351
				marker.setPosition(point);
681 aurelien 352
				niveauZoom = 6;
353
			}
2558 aurelien 354
			contenuMarker =
355
			"<div id=\"info_contenu\">"+
2629 mathias 356
				Msg.get("aucun-lieu-trouve") + " <br/>"
357
				+Msg.get("deplacez-marqueur-pour-localiser") + " <br />"
2558 aurelien 358
				+"longitude="+Util.tronquerNombrePourAffichage(""+marker.getPosition().lng(), 5)+"<br />"
359
				+"latitude="+Util.tronquerNombrePourAffichage(""+marker.getPosition().lat(), 5)+
360
			"</div>";
276 aurelien 361
		} else {
2568 aurelien 362
			String chaineLocalite = valeurLocalite + ((codeLoc.isEmpty()) ? "" : " ("+codeLoc+")");
2558 aurelien 363
			contenuMarker =
364
			"<div id=\"info_contenu\">"+
2568 aurelien 365
					chaineLocalite+"<br />"
2558 aurelien 366
					+"longitude="+Util.tronquerNombrePourAffichage(""+marker.getPosition().lng(), 5)+"<br />"
367
					+"latitude="+Util.tronquerNombrePourAffichage(""+marker.getPosition().lat(), 5)+
368
			"</div>";
276 aurelien 369
		}
370
 
2558 aurelien 371
		return contenuMarker+ htmlBoutonOk;
372
	}
373
 
374
	public String construireContenuPartielInfoMarker(Marker marker) {
375
		final String htmlBoutonOk = "<br /><button id=\"okMap\" class=\"x-btn-text\" type=\"button\">OK</button>";
376
 
377
		String contenuMarker = "";
268 aurelien 378
 
2558 aurelien 379
		contenuMarker =
380
			"<div id=\"info_contenu\">"+
381
					"<div class=\"recherche_infos_zone_geo\">"+
382
						"<div class=\"recherche_infos_zone_geo_en_cours\">"+
383
							"<img src=\"chargement_carte.gif\" />"+
384
							"<div>"+
2629 mathias 385
								Msg.get("recherche-nom-lieu") + "<br />"+
386
								Msg.get("prendre-un-moment") +
2558 aurelien 387
							"</div>"+
388
							"<hr class=\"nettoyage\" />"+
389
						"</div>"+
2629 mathias 390
						Msg.get("que-les-coordonnees") +
2558 aurelien 391
					"</div>"+
392
					"longitude="+Util.tronquerNombrePourAffichage("" + marker.getPosition().lng(), 5)+"<br />"+
393
					"latitude="+Util.tronquerNombrePourAffichage("" + marker.getPosition().lat(), 5)+
394
			"</div>";
395
 
396
		return contenuMarker+ htmlBoutonOk;
397
	}
398
 
399
	public void afficherInfoMarker(Marker marker, String contenuInfo) {
400
 
401
		info.setContent(contenuInfo);
402
		info.open(map, marker);
403
 
2568 aurelien 404
		pointLocalite = marker.getPosition();
2558 aurelien 405
 
1958 aurelien 406
		info.addDomReadyListenerOnce(new InfoWindow.DomReadyHandler() {
407
			@Override
408
			public void handle() {
409
				ajouterListenerBouton("okMap");
410
			}
411
		});
412
 
1959 aurelien 413
		recentrerCarte(marker.getPosition());
247 aurelien 414
	}
2558 aurelien 415
 
416
	public void afficherCarteEnAttenteAvecMarker(double lat, double lon) {
2568 aurelien 417
		pointLocalite = LatLng.create(lat, lon);
418
		nmk.setPosition(pointLocalite);
2558 aurelien 419
		afficherInfoMarker(nmk, construireContenuPartielInfoMarker(nmk));
420
	}
1958 aurelien 421
 
422
	public double[] getCoordsCentreFrance() {
423
		double[] coords = {47.0504, 2.2347};
424
		return coords;
654 aurelien 425
	}
2275 mathias 426
 
427
	public void setDoitZoomerSurRetour(boolean b) {
428
		zoomerSurRetour  = b;
429
	}
247 aurelien 430
}