Subversion Repositories eFlore/Applications.cel

Rev

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