Subversion Repositories eFlore/Applications.cel

Rev

Rev 989 | 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
 
3
import org.tela_botanica.client.interfaces.Rafraichissable;
989 aurelien 4
import org.tela_botanica.client.modeles.objets.Configuration;
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
 
9
import com.google.gwt.maps.client.InfoWindow;
10
import com.google.gwt.maps.client.InfoWindowContent;
11
import com.google.gwt.maps.client.MapType;
12
import com.google.gwt.maps.client.MapWidget;
430 aurelien 13
import com.google.gwt.maps.client.Maps;
247 aurelien 14
import com.google.gwt.maps.client.control.LargeMapControl;
391 aurelien 15
import com.google.gwt.maps.client.event.MapClickHandler;
264 aurelien 16
import com.google.gwt.maps.client.event.MapZoomEndHandler;
247 aurelien 17
import com.google.gwt.maps.client.event.MarkerClickHandler;
18
import com.google.gwt.maps.client.event.MarkerDragEndHandler;
19
import com.google.gwt.maps.client.event.MarkerDragStartHandler;
20
import com.google.gwt.maps.client.geom.LatLng;
21
import com.google.gwt.maps.client.overlay.Marker;
22
import com.google.gwt.maps.client.overlay.MarkerOptions;
268 aurelien 23
import com.google.gwt.user.client.Timer;
247 aurelien 24
import com.google.gwt.user.client.Window;
25
import com.gwtext.client.core.EventCallback;
26
import com.gwtext.client.core.EventObject;
27
import com.gwtext.client.core.Ext;
28
import com.gwtext.client.core.ExtElement;
29
import com.gwtext.client.widgets.Container;
30
import com.gwtext.client.widgets.Panel;
31
import com.gwtext.client.widgets.event.ContainerListenerAdapter;
32
import com.gwtext.client.widgets.form.Label;
33
import com.gwtext.client.widgets.layout.FitLayout;
34
import com.gwtext.client.widgets.layout.RowLayout;
35
import com.gwtext.client.widgets.layout.RowLayoutData;
36
 
37
public class CartographieObservationVue extends Panel implements Rafraichissable {
38
 
39
	private String id = "cartographie_observation";
40
 
41
	private ObservationMediateur oMediateur = null;
42
 
43
	Panel panneauCarto = null;
44
	private MapWidget map;
45
 
46
	private boolean carteAjoutee = false;
47
	private boolean requeteEnCours = false;
48
 
49
	String valeurCommune = "Bourges";
296 aurelien 50
	LatLng centreFrance = null;
299 aurelien 51
	LatLng pointCommune = null;
247 aurelien 52
	String codeCommune = "";
53
 
681 aurelien 54
	int niveauZoom = 6;
247 aurelien 55
 
56
	Label panneauExplication = new Label(
57
			"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");
58
 
59
	private InfoWindow info;
60
 
268 aurelien 61
	private boolean listenerInitialise = false;
62
 
247 aurelien 63
	public CartographieObservationVue(ObservationMediateur om) {
407 aurelien 64
 
247 aurelien 65
		oMediateur = om;
66
 
681 aurelien 67
		setHeader(false);
247 aurelien 68
		setAutoWidth(true);
69
		setPaddings(10);
70
 
71
		panneauCarto = new Panel();
72
		panneauCarto.setLayout(new FitLayout());
73
 
74
		setLayout(new RowLayout());
75
 
76
 
77
		add(panneauExplication, new RowLayoutData(12));
78
		add(panneauCarto, new RowLayoutData());
79
 
430 aurelien 80
		if(!carteAjoutee) {
654 aurelien 81
 
430 aurelien 82
			Maps.loadMapsApi(Configuration.getCleGoogleMaps(), "2", false, new Runnable() {
1292 aurelien 83
				@Override
430 aurelien 84
				public void run() {
85
					initialiserCarte(false);
86
				}
87
			});
88
		}
459 aurelien 89
 
247 aurelien 90
		addListener(new ContainerListenerAdapter() {
430 aurelien 91
 
1292 aurelien 92
			@Override
247 aurelien 93
			public void onAfterLayout(Container c) {
94
 
430 aurelien 95
				if(carteAjoutee) {
654 aurelien 96
					panneauCarto.setHeight("100%");
430 aurelien 97
					if((panneauCarto.getHeight() - 20) > map.getSize().getHeight()) {
98
						map.setSize(map.getSize().getWidth()+"px", (panneauCarto.getHeight() - 20)+"px");
99
					}
100
					panneauCarto.doLayout();
654 aurelien 101
					panneauCarto.expand();
247 aurelien 102
				}
103
			}
104
		});
105
 
106
		panneauCarto.setAutoHeight(true);
107
	}
108
 
681 aurelien 109
	public void setTailleCarte(int hauteur, int largeur) {
110
		map.setSize(largeur+"px", (hauteur+"px"));
111
	}
112
 
247 aurelien 113
	public void masquerChargement(boolean masquer) {
654 aurelien 114
 
115
		if(panneauCarto.isVisible()) {
116
			if(masquer) {
117
				panneauCarto.getEl().mask("Recherche des coordonnées et de la commune, veuillez patienter");
118
			} else {
119
				panneauCarto.getEl().unmask();
247 aurelien 120
			}
121
		}
122
	}
123
 
124
	public void obtenirInfosCommunes(LatLng coord) {
125
		oMediateur.obtenirInformationCoord(this, coord);
126
	}
127
 
128
	public void mettreAJourInfos(final LatLng pointNouvelleCommune, String infosComplementaires, int zoom) {
129
 
130
		if (!carteAjoutee) {
268 aurelien 131
			initialiserCarte(true);
247 aurelien 132
		}
133
 
134
		pointCommune = pointNouvelleCommune;
135
		map.clearOverlays();
136
		Marker nmk = fabriquerMarqueur(pointCommune, infosComplementaires);
137
		map.addOverlay(nmk);
138
		afficherInfoMarker(nmk);
139
		recentrerCarte();
140
	}
141
 
268 aurelien 142
	public void initialiserCarte(boolean commune) {
296 aurelien 143
 
144
		centreFrance = LatLng.newInstance(47.0504, 2.2347);
299 aurelien 145
		pointCommune = centreFrance;
146
 
247 aurelien 147
		map = new MapWidget(pointCommune, niveauZoom);
148
		panneauCarto.add(map);
149
		panneauCarto.doLayout();
150
		info = map.getInfoWindow();
268 aurelien 151
		if(!commune) {
152
			fabriquerMarqueurIndication();
153
		}
681 aurelien 154
 
155
		map.setZoomLevel(6);
247 aurelien 156
		map.checkResizeAndCenter();
157
		map.setUIToDefault();
158
		map.addControl(new LargeMapControl());
159
		map.setCurrentMapType(MapType.getHybridMap());
160
		carteAjoutee = true;
161
		panneauCarto.doLayout();
264 aurelien 162
 
391 aurelien 163
		map.addMapClickHandler(new MapClickHandler() {
164
 
1292 aurelien 165
			@Override
391 aurelien 166
			public void onClick(MapClickEvent event) {
167
				//TODO : quoi faire du double clic ?
168
				obtenirInfosCommunes(event.getLatLng());
169
			}
170
 
171
		});
172
 
264 aurelien 173
		map.addMapZoomEndHandler(new MapZoomEndHandler() {
174
 
1292 aurelien 175
			@Override
264 aurelien 176
			public void onZoomEnd(MapZoomEndEvent event) {
177
				niveauZoom = event.getNewZoomLevel();
178
			}
179
 
180
		});
247 aurelien 181
	}
268 aurelien 182
 
183
	public void fabriquerMarqueurIndication() {
184
		Marker marker = fabriquerMarqueur(pointCommune,"");
185
		map.addOverlay(marker);
186
		info.open(marker, new InfoWindowContent("<div id=\"info_contenu\">"+
187
				"Déplacez ce marqueur pour localiser votre observation (commune et coordonnées)<br />"
188
				+"longitude="
189
				+ marker.getLatLng().getLongitude() + "<br />latitude="
190
				+ marker.getLatLng().getLatitude() + "</div>"));
191
	}
247 aurelien 192
 
193
	public void AfficherMessageAucuneInfos() {
194
		mettreAJourInfos(centreFrance,"", niveauZoom);
195
		Window.alert("Aucune information pour le nom donné");
196
	}
197
 
198
	public int getNiveauZoom() {
199
		return niveauZoom;
200
	}
201
 
268 aurelien 202
	private void ajouterListenerBouton(final String idBouton) {
203
 
204
		final ExtElement bouton = Ext.get(idBouton);
205
 
206
		if(bouton == null) {
207
			Timer t = new Timer() {
1292 aurelien 208
				@Override
268 aurelien 209
				public void run() {
210
					ajouterListenerBouton(idBouton);
211
				}
212
 
213
			};
214
			t.schedule(500);
215
			return;
216
		}
217
 
247 aurelien 218
		bouton.addListener("click", new EventCallback() {
1292 aurelien 219
			@Override
247 aurelien 220
			public void execute(EventObject e) {
264 aurelien 221
				EntiteGeographiqueObservation infosCommune = new EntiteGeographiqueObservation(codeCommune,valeurCommune,null,null);
247 aurelien 222
				infosCommune.setLat(pointCommune.getLatitude()+"");
223
				infosCommune.setLon(pointCommune.getLongitude()+"");
224
				oMediateur.rafraichirSaisieCommuneObservation(infosCommune);
225
			}
268 aurelien 226
		});
227
 
228
		bouton.focus();
247 aurelien 229
	}
230
 
1292 aurelien 231
	@Override
247 aurelien 232
	public void rafraichir(Object nouvelleDonnees,
233
			boolean repandreRaffraichissement) {
234
		if (nouvelleDonnees instanceof String) {
235
 
236
			if(isVisible()) {
237
				Window.alert((String)nouvelleDonnees);
238
			}
239
		}
240
 
268 aurelien 241
		if (nouvelleDonnees instanceof EntiteGeographiqueObservation) {
247 aurelien 242
 
681 aurelien 243
			oMediateur.afficherFenetreCarto();
654 aurelien 244
 
268 aurelien 245
			EntiteGeographiqueObservation infos = (EntiteGeographiqueObservation) nouvelleDonnees;
247 aurelien 246
 
654 aurelien 247
			Double lat = 0.0;
248
			Double lon = 0.0;
249
 
250
			try {
251
				lat = Double.parseDouble(infos.getLat());
252
				lon = Double.parseDouble(infos.getLon());
253
			} catch (NumberFormatException nbe) {
254
 
255
			}
256
 
257
			LatLng coord = LatLng.newInstance(lat, lon);
258
 
965 aurelien 259
			valeurCommune = infos.getZoneGeo();
260
			codeCommune = Util.convertirChaineZoneGeoVersDepartement(infos.getIdZoneGeo());
268 aurelien 261
			pointCommune = coord;
247 aurelien 262
 
263
			mettreAJourInfos(coord, "", niveauZoom);
264
		}
265
 
266
		masquerChargement(false);
267
	}
268
 
269
	public Marker fabriquerMarqueur(LatLng pointMarqueur, final String texte) {
270
		// ajout de marqueurs déplacables, qui affichent une bulle d'information
271
		MarkerOptions options = MarkerOptions.newInstance();
272
		options.setDraggable(true);
273
		final Marker marker = new Marker(pointMarqueur, options);
274
 
275
		marker.addMarkerDragEndHandler(new MarkerDragEndHandler() {
1292 aurelien 276
			@Override
247 aurelien 277
			public void onDragEnd(MarkerDragEndEvent event) {
654 aurelien 278
				masquerChargement(true);
264 aurelien 279
				obtenirInfosCommunes(marker.getLatLng());
247 aurelien 280
			}
281
 
282
		});
283
 
284
		marker.addMarkerDragStartHandler(new MarkerDragStartHandler() {
1292 aurelien 285
			@Override
247 aurelien 286
			public void onDragStart(MarkerDragStartEvent event) {
287
				info.setVisible(false);
288
			}
289
		});
290
 
291
		marker.addMarkerClickHandler(new MarkerClickHandler() {
292
 
1292 aurelien 293
			@Override
247 aurelien 294
			public void onClick(MarkerClickEvent event) {
295
				if(info.isVisible()) {
296
					info.close();
297
				} else {
298
					afficherInfoMarker(marker);
299
				}
300
			}
301
		});
302
 
303
		return marker;
304
	}
305
 
306
	public void recentrerCarte() {
323 aurelien 307
 
308
		if(this.getWidth() > 12 && this.getHeight() > 24) {
309
			map.setSize((this.getWidth() - 12)+"px" , (this.getHeight() - 24)+"px");
310
			panneauCarto.setSize((this.getWidth() - 12)+"px" , (this.getHeight() - 12)+"px");
311
		}
247 aurelien 312
		map.setCenter(pointCommune, niveauZoom);
313
		map.checkResizeAndCenter();
314
	}
315
 
316
	public boolean isCarteAjoutee() {
317
		return carteAjoutee;
318
	}
319
 
320
	public void afficherInfoMarker(Marker marker) {
268 aurelien 321
 
247 aurelien 322
		final String htmlBoutonOk = "<br /><button id=\"okMap\" class=\"x-btn-text\" type=\"button\">OK</button>";
323
 
276 aurelien 324
		String contenuMarker = "";
325
 
326
		if(valeurCommune == null || valeurCommune.trim().equals("")) {
681 aurelien 327
			if(marker.getLatLng().getLongitude() == 0 && marker.getLatLng().getLatitude() == 0) {
328
				LatLng point = centreFrance;
329
				marker.setLatLng(point);
330
				niveauZoom = 6;
331
			}
276 aurelien 332
			contenuMarker = "<div id=\"info_contenu\">"+
333
			"Aucune commune française trouvée à cet emplacement <br />"
334
			+"Déplacez ce marqueur pour localiser votre observation (commune et coordonnées)<br />"
335
			+"longitude="
336
			+ marker.getLatLng().getLongitude() + "<br />latitude="
337
			+ marker.getLatLng().getLatitude() + "</div>";
338
		} else {
339
			contenuMarker = ""
340
			+valeurCommune+" ("+codeCommune+")<br />"
341
			+"longitude="
342
			+ marker.getLatLng().getLongitude() + "<br />latitude="
343
			+ marker.getLatLng().getLatitude() ;
344
		}
345
 
247 aurelien 346
		info.open(marker, new InfoWindowContent(""
276 aurelien 347
				+ contenuMarker
348
				+ htmlBoutonOk));
268 aurelien 349
 
350
		recentrerCarte();
247 aurelien 351
		ajouterListenerBouton("okMap");
352
	}
654 aurelien 353
 
354
	public LatLng getCentreFrance() {
355
		return LatLng.newInstance(47.0504, 2.2347);
356
	}
247 aurelien 357
 
358
}