Subversion Repositories eFlore/Applications.cel

Rev

Rev 654 | 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
 
681 aurelien 57
	int niveauZoom = 6;
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
 
681 aurelien 70
		setHeader(false);
247 aurelien 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
 
681 aurelien 110
	public void setTailleCarte(int hauteur, int largeur) {
111
		map.setSize(largeur+"px", (hauteur+"px"));
112
	}
113
 
247 aurelien 114
	public void masquerChargement(boolean masquer) {
654 aurelien 115
 
116
		if(panneauCarto.isVisible()) {
117
			if(masquer) {
118
				panneauCarto.getEl().mask("Recherche des coordonnées et de la commune, veuillez patienter");
119
			} else {
120
				panneauCarto.getEl().unmask();
247 aurelien 121
			}
122
		}
123
	}
124
 
125
	public void obtenirInfosCommunes(LatLng coord) {
126
		oMediateur.obtenirInformationCoord(this, coord);
127
	}
128
 
129
	public void mettreAJourInfos(final LatLng pointNouvelleCommune, String infosComplementaires, int zoom) {
130
 
131
		if (!carteAjoutee) {
268 aurelien 132
			initialiserCarte(true);
247 aurelien 133
		}
134
 
135
		pointCommune = pointNouvelleCommune;
136
		map.clearOverlays();
137
		Marker nmk = fabriquerMarqueur(pointCommune, infosComplementaires);
138
		map.addOverlay(nmk);
139
		afficherInfoMarker(nmk);
140
		recentrerCarte();
141
	}
142
 
268 aurelien 143
	public void initialiserCarte(boolean commune) {
296 aurelien 144
 
145
		centreFrance = LatLng.newInstance(47.0504, 2.2347);
299 aurelien 146
		pointCommune = centreFrance;
147
 
247 aurelien 148
		map = new MapWidget(pointCommune, niveauZoom);
149
		panneauCarto.add(map);
150
		panneauCarto.doLayout();
151
		info = map.getInfoWindow();
268 aurelien 152
		if(!commune) {
153
			fabriquerMarqueurIndication();
154
		}
681 aurelien 155
 
156
		map.setZoomLevel(6);
247 aurelien 157
		map.checkResizeAndCenter();
158
		map.setUIToDefault();
159
		map.addControl(new LargeMapControl());
160
		map.setCurrentMapType(MapType.getHybridMap());
161
		carteAjoutee = true;
162
		panneauCarto.doLayout();
264 aurelien 163
 
391 aurelien 164
		map.addMapClickHandler(new MapClickHandler() {
165
 
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
 
175
			public void onZoomEnd(MapZoomEndEvent event) {
176
				niveauZoom = event.getNewZoomLevel();
177
			}
178
 
179
		});
247 aurelien 180
	}
268 aurelien 181
 
182
	public void fabriquerMarqueurIndication() {
183
		Marker marker = fabriquerMarqueur(pointCommune,"");
184
		map.addOverlay(marker);
185
		info.open(marker, new InfoWindowContent("<div id=\"info_contenu\">"+
186
				"Déplacez ce marqueur pour localiser votre observation (commune et coordonnées)<br />"
187
				+"longitude="
188
				+ marker.getLatLng().getLongitude() + "<br />latitude="
189
				+ marker.getLatLng().getLatitude() + "</div>"));
190
	}
247 aurelien 191
 
192
	public void AfficherMessageAucuneInfos() {
193
		mettreAJourInfos(centreFrance,"", niveauZoom);
194
		Window.alert("Aucune information pour le nom donné");
195
	}
196
 
197
	public int getNiveauZoom() {
198
		return niveauZoom;
199
	}
200
 
268 aurelien 201
	private void ajouterListenerBouton(final String idBouton) {
202
 
203
		final ExtElement bouton = Ext.get(idBouton);
204
 
205
		if(bouton == null) {
206
			Timer t = new Timer() {
207
				public void run() {
208
					ajouterListenerBouton(idBouton);
209
				}
210
 
211
			};
212
			t.schedule(500);
213
			return;
214
		}
215
 
247 aurelien 216
		bouton.addListener("click", new EventCallback() {
217
			public void execute(EventObject e) {
264 aurelien 218
				EntiteGeographiqueObservation infosCommune = new EntiteGeographiqueObservation(codeCommune,valeurCommune,null,null);
247 aurelien 219
				infosCommune.setLat(pointCommune.getLatitude()+"");
220
				infosCommune.setLon(pointCommune.getLongitude()+"");
221
				oMediateur.rafraichirSaisieCommuneObservation(infosCommune);
222
			}
268 aurelien 223
		});
224
 
225
		bouton.focus();
247 aurelien 226
	}
227
 
228
	public void rafraichir(Object nouvelleDonnees,
229
			boolean repandreRaffraichissement) {
230
		if (nouvelleDonnees instanceof String) {
231
 
232
			if(isVisible()) {
233
				Window.alert((String)nouvelleDonnees);
234
			}
235
		}
236
 
268 aurelien 237
		if (nouvelleDonnees instanceof EntiteGeographiqueObservation) {
247 aurelien 238
 
681 aurelien 239
			oMediateur.afficherFenetreCarto();
654 aurelien 240
 
268 aurelien 241
			EntiteGeographiqueObservation infos = (EntiteGeographiqueObservation) nouvelleDonnees;
247 aurelien 242
 
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
			}
252
 
253
			LatLng coord = LatLng.newInstance(lat, lon);
254
 
268 aurelien 255
			valeurCommune = infos.getCommune();
256
			codeCommune = infos.getIdLocalite();
257
			pointCommune = coord;
247 aurelien 258
 
259
			mettreAJourInfos(coord, "", niveauZoom);
260
		}
261
 
262
		masquerChargement(false);
263
	}
264
 
265
	public Marker fabriquerMarqueur(LatLng pointMarqueur, final String texte) {
266
		// ajout de marqueurs déplacables, qui affichent une bulle d'information
267
		MarkerOptions options = MarkerOptions.newInstance();
268
		options.setDraggable(true);
269
		final Marker marker = new Marker(pointMarqueur, options);
270
 
271
		marker.addMarkerDragEndHandler(new MarkerDragEndHandler() {
272
			public void onDragEnd(MarkerDragEndEvent event) {
654 aurelien 273
				masquerChargement(true);
264 aurelien 274
				obtenirInfosCommunes(marker.getLatLng());
247 aurelien 275
			}
276
 
277
		});
278
 
279
		marker.addMarkerDragStartHandler(new MarkerDragStartHandler() {
280
			public void onDragStart(MarkerDragStartEvent event) {
281
				info.setVisible(false);
282
			}
283
		});
284
 
285
		marker.addMarkerClickHandler(new MarkerClickHandler() {
286
 
287
			public void onClick(MarkerClickEvent event) {
288
				if(info.isVisible()) {
289
					info.close();
290
				} else {
291
					afficherInfoMarker(marker);
292
				}
293
			}
294
		});
295
 
296
		return marker;
297
	}
298
 
299
	public void recentrerCarte() {
323 aurelien 300
 
301
		if(this.getWidth() > 12 && this.getHeight() > 24) {
302
			map.setSize((this.getWidth() - 12)+"px" , (this.getHeight() - 24)+"px");
303
			panneauCarto.setSize((this.getWidth() - 12)+"px" , (this.getHeight() - 12)+"px");
304
		}
247 aurelien 305
		map.setCenter(pointCommune, niveauZoom);
306
		map.checkResizeAndCenter();
307
	}
308
 
309
	public boolean isCarteAjoutee() {
310
		return carteAjoutee;
311
	}
312
 
313
	public void afficherInfoMarker(Marker marker) {
268 aurelien 314
 
247 aurelien 315
		final String htmlBoutonOk = "<br /><button id=\"okMap\" class=\"x-btn-text\" type=\"button\">OK</button>";
316
 
276 aurelien 317
		String contenuMarker = "";
318
 
319
		if(valeurCommune == null || valeurCommune.trim().equals("")) {
681 aurelien 320
			if(marker.getLatLng().getLongitude() == 0 && marker.getLatLng().getLatitude() == 0) {
321
				LatLng point = centreFrance;
322
				marker.setLatLng(point);
323
				niveauZoom = 6;
324
			}
276 aurelien 325
			contenuMarker = "<div id=\"info_contenu\">"+
326
			"Aucune commune française trouvée à cet emplacement <br />"
327
			+"Déplacez ce marqueur pour localiser votre observation (commune et coordonnées)<br />"
328
			+"longitude="
329
			+ marker.getLatLng().getLongitude() + "<br />latitude="
330
			+ marker.getLatLng().getLatitude() + "</div>";
331
		} else {
332
			contenuMarker = ""
333
			+valeurCommune+" ("+codeCommune+")<br />"
334
			+"longitude="
335
			+ marker.getLatLng().getLongitude() + "<br />latitude="
336
			+ marker.getLatLng().getLatitude() ;
337
		}
338
 
247 aurelien 339
		info.open(marker, new InfoWindowContent(""
276 aurelien 340
				+ contenuMarker
341
				+ htmlBoutonOk));
268 aurelien 342
 
343
		recentrerCarte();
247 aurelien 344
		ajouterListenerBouton("okMap");
345
	}
654 aurelien 346
 
347
	public LatLng getCentreFrance() {
348
		return LatLng.newInstance(47.0504, 2.2347);
349
	}
247 aurelien 350
 
351
}