Subversion Repositories eFlore/Applications.cel

Rev

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