Subversion Repositories eFlore/Applications.cel

Rev

Rev 2276 | 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;
247 aurelien 49
	String codeCommune = "";
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) {
1958 aurelien 100
		oMediateur.obtenirInformationCoord(this, coord.lat(), coord.lng());
247 aurelien 101
	}
102
 
103
	public void mettreAJourInfos(final LatLng pointNouvelleCommune, String infosComplementaires, int zoom) {
1958 aurelien 104
		GoogleMap mapNulle = null;
105
		if(nmk != null) {
106
			nmk.setMap(mapNulle);
247 aurelien 107
		}
108
		pointCommune = pointNouvelleCommune;
1958 aurelien 109
		nmk = fabriquerMarqueur(pointCommune, infosComplementaires);
110
		nmk.setMap(map);
247 aurelien 111
		afficherInfoMarker(nmk);
1959 aurelien 112
		recentrerCarte(nmk.getPosition());
2275 mathias 113
		map.setZoom(zoom);
247 aurelien 114
	}
115
 
1958 aurelien 116
	private void initialiserCarte(boolean commune) {
2275 mathias 117
 
1958 aurelien 118
		panneauCarto.clear();
2275 mathias 119
 
1958 aurelien 120
		centreFrance = LatLng.create(47.0504, 2.2347);
299 aurelien 121
		pointCommune = centreFrance;
2275 mathias 122
 
123
		MapOptions options = MapOptions.create();
124
	    options.setCenter(centreFrance);
125
	    options.setZoom(6);
1958 aurelien 126
	    options.setMapTypeId(MapTypeId.HYBRID);
127
	    options.setDraggable(true);
128
	    options.setMapTypeControl(true);
2275 mathias 129
	    options.setScaleControl(true);
130
	    options.setScrollwheel(true);
131
 
132
		map = GoogleMap.create(panneauCarto.getElement(), options );
1958 aurelien 133
 
134
		InfoWindowOptions infopts = InfoWindowOptions.create();
135
		info = InfoWindow.create();
136
		info.setOptions(infopts);
137
		info.addCloseClickListener(new InfoWindow.CloseClickHandler() {
138
			@Override
139
			public void handle() {
140
				infoOuverte = false;
141
			}
142
		});
143
 
268 aurelien 144
		if(!commune) {
145
			fabriquerMarqueurIndication();
146
		}
1958 aurelien 147
 
148
		map.addClickListener(new ClickHandler() {
1292 aurelien 149
			@Override
1958 aurelien 150
			public void handle(MouseEvent event) {
391 aurelien 151
				obtenirInfosCommunes(event.getLatLng());
152
			}
153
		});
154
 
1958 aurelien 155
		map.addZoomChangedListener(new ZoomChangedHandler() {
1292 aurelien 156
			@Override
1958 aurelien 157
			public void handle() {
158
				niveauZoom = (int)map.getZoom();
264 aurelien 159
			}
160
		});
1958 aurelien 161
 
162
		panneauCarto.doLayout();
163
		panneauCarto.expand();
164
		panneauCarto.setHeight("100%");
165
		panneauCarto.setWidth("100%");
166
		expand();
247 aurelien 167
	}
268 aurelien 168
 
169
	public void fabriquerMarqueurIndication() {
1958 aurelien 170
		GoogleMap mapNulle = null;
171
		if(nmk != null) {
172
			nmk.setMap(mapNulle);
173
		}
174
		nmk = fabriquerMarqueur(pointCommune,"");
175
		nmk.setMap(map);
176
		info.setContent("<div id=\"info_contenu\">"+
268 aurelien 177
				"Déplacez ce marqueur pour localiser votre observation (commune et coordonnées)<br />"
178
				+"longitude="
2276 mathias 179
				+ Util.tronquerNombrePourAffichage("" + nmk.getPosition().lng(), 5) + "<br />latitude="
180
				+ Util.tronquerNombrePourAffichage("" + nmk.getPosition().lat(), 5) + "</div>");
1958 aurelien 181
		infoOuverte = true;
182
		info.open(map,nmk);
268 aurelien 183
	}
247 aurelien 184
 
185
	public void AfficherMessageAucuneInfos() {
186
		mettreAJourInfos(centreFrance,"", niveauZoom);
187
		Window.alert("Aucune information pour le nom donné");
188
	}
189
 
190
	public int getNiveauZoom() {
191
		return niveauZoom;
192
	}
193
 
268 aurelien 194
	private void ajouterListenerBouton(final String idBouton) {
195
 
196
		final ExtElement bouton = Ext.get(idBouton);
197
		if(bouton == null) {
198
			Timer t = new Timer() {
1292 aurelien 199
				@Override
268 aurelien 200
				public void run() {
201
					ajouterListenerBouton(idBouton);
202
				}
203
			};
204
			t.schedule(500);
205
			return;
1958 aurelien 206
		} else {
207
			bouton.addListener("click", new EventCallback() {
208
				@Override
209
				public void execute(EventObject e) {
210
					EntiteGeographiqueObservation infosCommune = new EntiteGeographiqueObservation(codeCommune,valeurCommune,null,null);
211
					infosCommune.setLat(pointCommune.lat()+"");
212
					infosCommune.setLon(pointCommune.lng()+"");
213
					oMediateur.rafraichirSaisieCommuneObservation(infosCommune);
214
				}
215
			});
216
 
217
			bouton.focus();
268 aurelien 218
		}
247 aurelien 219
	}
220
 
1292 aurelien 221
	@Override
2275 mathias 222
	public void rafraichir(Object nouvelleDonnees, boolean repandreRaffraichissement) {
2553 mathias 223
		// rétablissement du curseur, parfois mis sur "attente" par le processus chargeant la carte
224
		Util.curseurParDefaut();
2275 mathias 225
 
247 aurelien 226
		if (nouvelleDonnees instanceof String) {
227
 
228
			if(isVisible()) {
229
				Window.alert((String)nouvelleDonnees);
230
			}
231
		}
232
 
268 aurelien 233
		if (nouvelleDonnees instanceof EntiteGeographiqueObservation) {
247 aurelien 234
 
681 aurelien 235
			oMediateur.afficherFenetreCarto();
268 aurelien 236
			EntiteGeographiqueObservation infos = (EntiteGeographiqueObservation) nouvelleDonnees;
654 aurelien 237
			Double lat = 0.0;
238
			Double lon = 0.0;
239
 
240
			try {
241
				lat = Double.parseDouble(infos.getLat());
242
				lon = Double.parseDouble(infos.getLon());
243
			} catch (NumberFormatException nbe) {
244
 
245
			}
1958 aurelien 246
			LatLng coord = LatLng.create(lat, lon);
2275 mathias 247
			// si on a un point, on zoome fort dès le début
248
			if (zoomerSurRetour) {
249
				niveauZoom = 16;
250
				zoomerSurRetour = false;
2553 mathias 251
			} else if (infos.getZoom() > 0) {
252
				niveauZoom = infos.getZoom();
2275 mathias 253
			}
2270 mathias 254
 
255
			String nouvelleValeurCommune = infos.getZoneGeo();
256
			valeurCommune = nouvelleValeurCommune;
2553 mathias 257
			if (infos.getIdZoneGeo() != null) {
258
				codeCommune = Util.convertirChaineZoneGeoVersDepartement(infos.getIdZoneGeo());
259
			} else {
260
				codeCommune = "";
261
			}
268 aurelien 262
			pointCommune = coord;
247 aurelien 263
 
264
			mettreAJourInfos(coord, "", niveauZoom);
265
		}
266
 
267
		masquerChargement(false);
268
	}
269
 
270
	public Marker fabriquerMarqueur(LatLng pointMarqueur, final String texte) {
271
		// ajout de marqueurs déplacables, qui affichent une bulle d'information
1958 aurelien 272
		MarkerOptions options = MarkerOptions.create();
247 aurelien 273
		options.setDraggable(true);
1958 aurelien 274
		options.setPosition(pointMarqueur);
275
		final Marker marker = Marker.create(options);
247 aurelien 276
 
1958 aurelien 277
		marker.addDragEndListener(new Marker.DragEndHandler() {
278
 
1292 aurelien 279
			@Override
1958 aurelien 280
			public void handle(MouseEvent event) {
281
				obtenirInfosCommunes(event.getLatLng());
247 aurelien 282
			}
283
		});
1958 aurelien 284
 
285
		marker.addDragStartListener(new Marker.DragStartHandler() {
286
 
1292 aurelien 287
			@Override
1958 aurelien 288
			public void handle(MouseEvent event) {
289
				info.close();
247 aurelien 290
			}
291
		});
292
 
1958 aurelien 293
		marker.addClickListener(new Marker.ClickHandler() {
294
 
1292 aurelien 295
			@Override
1958 aurelien 296
			public void handle(MouseEvent event) {
297
				if(infoOuverte) {
247 aurelien 298
					info.close();
1958 aurelien 299
					infoOuverte = false;
247 aurelien 300
				} else {
301
					afficherInfoMarker(marker);
1958 aurelien 302
					infoOuverte = true;
247 aurelien 303
				}
1958 aurelien 304
			}
247 aurelien 305
		});
306
		return marker;
307
	}
1959 aurelien 308
 
309
	public void recentrerCarte() {
310
		if(nmk != null && nmk.getVisible()) {
311
			recentrerCarte(nmk.getPosition());
312
		}
313
	}
247 aurelien 314
 
1959 aurelien 315
	public void recentrerCarte(LatLng point) {
1958 aurelien 316
		if(this.getWidth() > 12 && this.getHeight() > 24) {
323 aurelien 317
			panneauCarto.setSize((this.getWidth() - 12)+"px" , (this.getHeight() - 12)+"px");
318
		}
1958 aurelien 319
		panneauCarto.doLayout();
320
		panneauCarto.expand();
321
		panneauCarto.setHeight("100%");
322
		panneauCarto.setWidth("100%");
323
		expand();
1959 aurelien 324
		map.setCenter(point);
247 aurelien 325
	}
326
 
327
	public boolean isCarteAjoutee() {
328
		return carteAjoutee;
329
	}
330
 
1958 aurelien 331
	public void afficherInfoMarker(Marker marker) {
247 aurelien 332
		final String htmlBoutonOk = "<br /><button id=\"okMap\" class=\"x-btn-text\" type=\"button\">OK</button>";
333
 
276 aurelien 334
		String contenuMarker = "";
335
 
336
		if(valeurCommune == null || valeurCommune.trim().equals("")) {
1958 aurelien 337
			if(marker.getPosition().lng() == 0 && marker.getPosition().lat() == 0) {
681 aurelien 338
				LatLng point = centreFrance;
1958 aurelien 339
				marker.setPosition(point);
681 aurelien 340
				niveauZoom = 6;
341
			}
276 aurelien 342
			contenuMarker = "<div id=\"info_contenu\">"+
2270 mathias 343
			"Aucun nom de lieu trouvé à cet emplacement <br />"
344
			+"Déplacez ce marqueur pour localiser votre observation (nom de lieu et coordonnées)<br />"
276 aurelien 345
			+"longitude="
2276 mathias 346
			+ Util.tronquerNombrePourAffichage("" + marker.getPosition().lng(), 5) + "<br />latitude="
347
			+ Util.tronquerNombrePourAffichage("" + marker.getPosition().lat(), 5) + "</div>";
276 aurelien 348
		} else {
349
			contenuMarker = ""
350
			+valeurCommune+" ("+codeCommune+")<br />"
351
			+"longitude="
2276 mathias 352
			+ Util.tronquerNombrePourAffichage("" + marker.getPosition().lng(), 5) + "<br />latitude="
353
			+ Util.tronquerNombrePourAffichage("" + marker.getPosition().lat(), 5) ;
276 aurelien 354
		}
355
 
1958 aurelien 356
		info.setContent(contenuMarker+ htmlBoutonOk);
357
		info.open(map, marker);
268 aurelien 358
 
1958 aurelien 359
		info.addDomReadyListenerOnce(new InfoWindow.DomReadyHandler() {
360
			@Override
361
			public void handle() {
362
				ajouterListenerBouton("okMap");
363
			}
364
		});
365
 
1959 aurelien 366
		recentrerCarte(marker.getPosition());
247 aurelien 367
	}
1958 aurelien 368
 
369
	public double[] getCoordsCentreFrance() {
370
		double[] coords = {47.0504, 2.2347};
371
		return coords;
654 aurelien 372
	}
2275 mathias 373
 
374
	public void setDoitZoomerSurRetour(boolean b) {
375
		zoomerSurRetour  = b;
376
	}
247 aurelien 377
}