Subversion Repositories eFlore/Applications.cel

Rev

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