Subversion Repositories eFlore/Applications.cel

Rev

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