Subversion Repositories eFlore/Applications.cel

Rev

Rev 430 | 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
		if(!carteAjoutee) {
80
			Maps.loadMapsApi(Configuration.getCleGoogleMaps(), "2", false, new Runnable() {
81
				public void run() {
82
					initialiserCarte(false);
83
					recentrerCarte();
84
				}
85
			});
86
		}
459 aurelien 87
 
247 aurelien 88
		addListener(new ContainerListenerAdapter() {
430 aurelien 89
 
247 aurelien 90
			public void onAfterLayout(Container c) {
91
 
430 aurelien 92
				if(carteAjoutee) {
93
 
94
					if((panneauCarto.getHeight() - 20) > map.getSize().getHeight()) {
95
						map.setSize(map.getSize().getWidth()+"px", (panneauCarto.getHeight() - 20)+"px");
96
					}
97
					panneauCarto.setHeight("100%");
98
					panneauCarto.doLayout();
247 aurelien 99
				}
100
			}
101
		});
102
 
103
		panneauCarto.setAutoHeight(true);
104
	}
105
 
106
	public void masquerChargement(boolean masquer) {
107
		if (masquer) {
108
			ExtElement masked = Ext.get(id);
109
 
110
			if (masked != null) {
111
				masked.mask("Chargement");
112
			}
113
		} else {
114
			ExtElement masked = Ext.get(id);
115
 
116
			if (masked != null) {
117
				masked.unmask();
118
			}
119
		}
120
	}
121
 
122
	public void obtenirInfosCommunes(LatLng coord) {
123
		oMediateur.obtenirInformationCoord(this, coord);
124
	}
125
 
126
	public void mettreAJourInfos(final LatLng pointNouvelleCommune, String infosComplementaires, int zoom) {
127
 
128
		if (!carteAjoutee) {
268 aurelien 129
			initialiserCarte(true);
247 aurelien 130
		}
131
 
132
		pointCommune = pointNouvelleCommune;
133
		map.clearOverlays();
134
		Marker nmk = fabriquerMarqueur(pointCommune, infosComplementaires);
135
		map.addOverlay(nmk);
136
		afficherInfoMarker(nmk);
137
		recentrerCarte();
138
	}
139
 
268 aurelien 140
	public void initialiserCarte(boolean commune) {
296 aurelien 141
 
142
		centreFrance = LatLng.newInstance(47.0504, 2.2347);
299 aurelien 143
		pointCommune = centreFrance;
144
 
247 aurelien 145
		map = new MapWidget(pointCommune, niveauZoom);
146
		panneauCarto.add(map);
147
		panneauCarto.doLayout();
148
		info = map.getInfoWindow();
268 aurelien 149
		if(!commune) {
150
			fabriquerMarqueurIndication();
151
		}
247 aurelien 152
		map.checkResizeAndCenter();
153
		map.setUIToDefault();
154
		map.addControl(new LargeMapControl());
155
		map.setCurrentMapType(MapType.getHybridMap());
156
		carteAjoutee = true;
157
		panneauCarto.doLayout();
264 aurelien 158
 
391 aurelien 159
		map.addMapClickHandler(new MapClickHandler() {
160
 
161
			public void onClick(MapClickEvent event) {
162
				//TODO : quoi faire du double clic ?
163
				obtenirInfosCommunes(event.getLatLng());
164
			}
165
 
166
		});
167
 
264 aurelien 168
		map.addMapZoomEndHandler(new MapZoomEndHandler() {
169
 
170
			public void onZoomEnd(MapZoomEndEvent event) {
171
				niveauZoom = event.getNewZoomLevel();
172
			}
173
 
174
		});
247 aurelien 175
	}
268 aurelien 176
 
177
	public void fabriquerMarqueurIndication() {
178
		Marker marker = fabriquerMarqueur(pointCommune,"");
179
		map.addOverlay(marker);
180
		info.open(marker, new InfoWindowContent("<div id=\"info_contenu\">"+
181
				"Déplacez ce marqueur pour localiser votre observation (commune et coordonnées)<br />"
182
				+"longitude="
183
				+ marker.getLatLng().getLongitude() + "<br />latitude="
184
				+ marker.getLatLng().getLatitude() + "</div>"));
185
		map.setZoomLevel(3);
186
	}
247 aurelien 187
 
188
	public void AfficherMessageAucuneInfos() {
189
		mettreAJourInfos(centreFrance,"", niveauZoom);
190
		Window.alert("Aucune information pour le nom donné");
191
	}
192
 
193
	public int getNiveauZoom() {
194
		return niveauZoom;
195
	}
196
 
268 aurelien 197
	private void ajouterListenerBouton(final String idBouton) {
198
 
199
		final ExtElement bouton = Ext.get(idBouton);
200
 
201
		if(bouton == null) {
202
			Timer t = new Timer() {
203
				public void run() {
204
					ajouterListenerBouton(idBouton);
205
				}
206
 
207
			};
208
			t.schedule(500);
209
			return;
210
		}
211
 
247 aurelien 212
		bouton.addListener("click", new EventCallback() {
213
			public void execute(EventObject e) {
264 aurelien 214
				EntiteGeographiqueObservation infosCommune = new EntiteGeographiqueObservation(codeCommune,valeurCommune,null,null);
247 aurelien 215
				infosCommune.setLat(pointCommune.getLatitude()+"");
216
				infosCommune.setLon(pointCommune.getLongitude()+"");
217
				oMediateur.rafraichirSaisieCommuneObservation(infosCommune);
218
			}
268 aurelien 219
		});
220
 
221
		bouton.focus();
247 aurelien 222
	}
223
 
224
	public void rafraichir(Object nouvelleDonnees,
225
			boolean repandreRaffraichissement) {
226
		if (nouvelleDonnees instanceof String) {
227
 
228
			if(isVisible()) {
229
				Window.alert((String)nouvelleDonnees);
230
			}
231
		}
232
 
268 aurelien 233
		if (nouvelleDonnees instanceof EntiteGeographiqueObservation) {
247 aurelien 234
 
268 aurelien 235
			EntiteGeographiqueObservation infos = (EntiteGeographiqueObservation) nouvelleDonnees;
236
			LatLng coord = LatLng.newInstance(Double.parseDouble(infos.getLat()),
237
					Double.parseDouble(infos.getLon()));
247 aurelien 238
 
268 aurelien 239
			valeurCommune = infos.getCommune();
240
			codeCommune = infos.getIdLocalite();
241
			pointCommune = coord;
247 aurelien 242
 
243
			mettreAJourInfos(coord, "", niveauZoom);
244
		}
245
 
246
		masquerChargement(false);
247
	}
248
 
249
	public Marker fabriquerMarqueur(LatLng pointMarqueur, final String texte) {
250
		// ajout de marqueurs déplacables, qui affichent une bulle d'information
251
		MarkerOptions options = MarkerOptions.newInstance();
252
		options.setDraggable(true);
253
		final Marker marker = new Marker(pointMarqueur, options);
254
 
255
		marker.addMarkerDragEndHandler(new MarkerDragEndHandler() {
256
			public void onDragEnd(MarkerDragEndEvent event) {
264 aurelien 257
				obtenirInfosCommunes(marker.getLatLng());
247 aurelien 258
			}
259
 
260
		});
261
 
262
		marker.addMarkerDragStartHandler(new MarkerDragStartHandler() {
263
			public void onDragStart(MarkerDragStartEvent event) {
264
				info.setVisible(false);
265
			}
266
		});
267
 
268
		marker.addMarkerClickHandler(new MarkerClickHandler() {
269
 
270
			public void onClick(MarkerClickEvent event) {
271
				if(info.isVisible()) {
272
					info.close();
273
				} else {
274
					afficherInfoMarker(marker);
275
				}
276
			}
277
		});
278
 
279
		return marker;
280
	}
281
 
282
	public void recentrerCarte() {
323 aurelien 283
 
284
		if(this.getWidth() > 12 && this.getHeight() > 24) {
285
			map.setSize((this.getWidth() - 12)+"px" , (this.getHeight() - 24)+"px");
286
			panneauCarto.setSize((this.getWidth() - 12)+"px" , (this.getHeight() - 12)+"px");
287
		}
247 aurelien 288
		map.setCenter(pointCommune, niveauZoom);
289
		map.checkResizeAndCenter();
290
	}
291
 
292
	public boolean isCarteAjoutee() {
293
		return carteAjoutee;
294
	}
295
 
296
	public void afficherInfoMarker(Marker marker) {
268 aurelien 297
 
247 aurelien 298
		final String htmlBoutonOk = "<br /><button id=\"okMap\" class=\"x-btn-text\" type=\"button\">OK</button>";
299
 
276 aurelien 300
		String contenuMarker = "";
301
 
302
		if(valeurCommune == null || valeurCommune.trim().equals("")) {
303
			contenuMarker = "<div id=\"info_contenu\">"+
304
			"Aucune commune française trouvée à cet emplacement <br />"
305
			+"Déplacez ce marqueur pour localiser votre observation (commune et coordonnées)<br />"
306
			+"longitude="
307
			+ marker.getLatLng().getLongitude() + "<br />latitude="
308
			+ marker.getLatLng().getLatitude() + "</div>";
309
		} else {
310
			contenuMarker = ""
311
			+valeurCommune+" ("+codeCommune+")<br />"
312
			+"longitude="
313
			+ marker.getLatLng().getLongitude() + "<br />latitude="
314
			+ marker.getLatLng().getLatitude() ;
315
		}
316
 
247 aurelien 317
		info.open(marker, new InfoWindowContent(""
276 aurelien 318
				+ contenuMarker
319
				+ htmlBoutonOk));
268 aurelien 320
 
321
		recentrerCarte();
247 aurelien 322
		ajouterListenerBouton("okMap");
323
	}
324
 
325
}