Subversion Repositories eFlore/Applications.cel

Rev

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