Subversion Repositories eFlore/Applications.cel

Rev

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