Subversion Repositories eFlore/Applications.cel

Rev

Rev 2267 | 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;
989 aurelien 4
import org.tela_botanica.client.modeles.objets.EntiteGeographiqueObservation;
247 aurelien 5
import org.tela_botanica.client.observation.ObservationMediateur;
965 aurelien 6
import org.tela_botanica.client.util.Util;
247 aurelien 7
 
268 aurelien 8
import com.google.gwt.user.client.Timer;
247 aurelien 9
import com.google.gwt.user.client.Window;
10
import com.gwtext.client.core.EventCallback;
11
import com.gwtext.client.core.EventObject;
12
import com.gwtext.client.core.Ext;
13
import com.gwtext.client.core.ExtElement;
14
import com.gwtext.client.widgets.Container;
15
import com.gwtext.client.widgets.Panel;
16
import com.gwtext.client.widgets.event.ContainerListenerAdapter;
17
import com.gwtext.client.widgets.form.Label;
18
import com.gwtext.client.widgets.layout.FitLayout;
19
import com.gwtext.client.widgets.layout.RowLayout;
20
import com.gwtext.client.widgets.layout.RowLayoutData;
1958 aurelien 21
import com.google.maps.gwt.client.GoogleMap;
22
import com.google.maps.gwt.client.GoogleMap.ClickHandler;
23
import com.google.maps.gwt.client.GoogleMap.ZoomChangedHandler;
24
import com.google.maps.gwt.client.InfoWindow;
25
import com.google.maps.gwt.client.InfoWindowOptions;
26
import com.google.maps.gwt.client.LatLng;
27
import com.google.maps.gwt.client.MapOptions;
28
import com.google.maps.gwt.client.MapTypeId;
29
import com.google.maps.gwt.client.Marker;
30
import com.google.maps.gwt.client.MarkerOptions;
31
import com.google.maps.gwt.client.MouseEvent;
247 aurelien 32
 
33
public class CartographieObservationVue extends Panel implements Rafraichissable {
34
 
35
	private String id = "cartographie_observation";
36
 
37
	private ObservationMediateur oMediateur = null;
38
 
39
	Panel panneauCarto = null;
1958 aurelien 40
	GoogleMap map = null;
41
	Marker nmk = null;
247 aurelien 42
 
43
	private boolean carteAjoutee = false;
1958 aurelien 44
	private boolean infoOuverte = false;
247 aurelien 45
 
2270 mathias 46
	String valeurCommune = "Bourges"; // j'aurais préféré Bort-les-Orgues
296 aurelien 47
	LatLng centreFrance = null;
299 aurelien 48
	LatLng pointCommune = null;
247 aurelien 49
	String codeCommune = "";
50
 
681 aurelien 51
	int niveauZoom = 6;
247 aurelien 52
 
53
	Label panneauExplication = new Label(
54
			"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");
55
 
56
	private InfoWindow info;
57
 
58
	public CartographieObservationVue(ObservationMediateur om) {
407 aurelien 59
 
247 aurelien 60
		oMediateur = om;
61
 
681 aurelien 62
		setHeader(false);
247 aurelien 63
		setAutoWidth(true);
64
		setPaddings(10);
65
 
66
		panneauCarto = new Panel();
67
		panneauCarto.setLayout(new FitLayout());
68
 
69
		setLayout(new RowLayout());
70
 
1958 aurelien 71
		panneauCarto.setAutoHeight(true);
247 aurelien 72
		add(panneauExplication, new RowLayoutData(12));
73
		add(panneauCarto, new RowLayoutData());
1958 aurelien 74
 
247 aurelien 75
		addListener(new ContainerListenerAdapter() {
430 aurelien 76
 
1292 aurelien 77
			@Override
247 aurelien 78
			public void onAfterLayout(Container c) {
1958 aurelien 79
				initialiserCarte(false);
247 aurelien 80
			}
81
		});
82
	}
83
 
681 aurelien 84
	public void setTailleCarte(int hauteur, int largeur) {
1958 aurelien 85
		setSize(largeur, hauteur);
681 aurelien 86
	}
87
 
247 aurelien 88
	public void masquerChargement(boolean masquer) {
654 aurelien 89
 
90
		if(panneauCarto.isVisible()) {
91
			if(masquer) {
92
				panneauCarto.getEl().mask("Recherche des coordonnées et de la commune, veuillez patienter");
93
			} else {
94
				panneauCarto.getEl().unmask();
247 aurelien 95
			}
96
		}
97
	}
98
 
99
	public void obtenirInfosCommunes(LatLng coord) {
1958 aurelien 100
		oMediateur.obtenirInformationCoord(this, coord.lat(), coord.lng());
247 aurelien 101
	}
102
 
103
	public void mettreAJourInfos(final LatLng pointNouvelleCommune, String infosComplementaires, int zoom) {
1958 aurelien 104
		GoogleMap mapNulle = null;
105
		if(nmk != null) {
106
			nmk.setMap(mapNulle);
247 aurelien 107
		}
108
		pointCommune = pointNouvelleCommune;
1958 aurelien 109
		nmk = fabriquerMarqueur(pointCommune, infosComplementaires);
110
		nmk.setMap(map);
247 aurelien 111
		afficherInfoMarker(nmk);
1959 aurelien 112
		recentrerCarte(nmk.getPosition());
247 aurelien 113
	}
114
 
1958 aurelien 115
	private void initialiserCarte(boolean commune) {
296 aurelien 116
 
1958 aurelien 117
		panneauCarto.clear();
118
 
119
		centreFrance = LatLng.create(47.0504, 2.2347);
299 aurelien 120
		pointCommune = centreFrance;
121
 
1958 aurelien 122
		MapOptions options  = MapOptions.create();
123
	    options.setCenter(centreFrance);
124
	    options.setZoom(6) ;
125
	    options.setMapTypeId(MapTypeId.HYBRID);
126
	    options.setDraggable(true);
127
	    options.setMapTypeControl(true);
128
	    options.setScaleControl(true) ;
129
	    options.setScrollwheel(true) ;
130
 
131
		map = GoogleMap.create(panneauCarto.getElement(), options ) ;
132
 
133
		InfoWindowOptions infopts = InfoWindowOptions.create();
134
		info = InfoWindow.create();
135
		info.setOptions(infopts);
136
		info.addCloseClickListener(new InfoWindow.CloseClickHandler() {
137
			@Override
138
			public void handle() {
139
				infoOuverte = false;
140
			}
141
		});
142
 
268 aurelien 143
		if(!commune) {
144
			fabriquerMarqueurIndication();
145
		}
1958 aurelien 146
 
147
		map.addClickListener(new ClickHandler() {
1292 aurelien 148
			@Override
1958 aurelien 149
			public void handle(MouseEvent event) {
391 aurelien 150
				obtenirInfosCommunes(event.getLatLng());
151
			}
152
		});
153
 
1958 aurelien 154
		map.addZoomChangedListener(new ZoomChangedHandler() {
1292 aurelien 155
			@Override
1958 aurelien 156
			public void handle() {
157
				niveauZoom = (int)map.getZoom();
264 aurelien 158
			}
159
		});
1958 aurelien 160
 
161
		panneauCarto.doLayout();
162
		panneauCarto.expand();
163
		panneauCarto.setHeight("100%");
164
		panneauCarto.setWidth("100%");
165
		expand();
247 aurelien 166
	}
268 aurelien 167
 
168
	public void fabriquerMarqueurIndication() {
1958 aurelien 169
		GoogleMap mapNulle = null;
170
		if(nmk != null) {
171
			nmk.setMap(mapNulle);
172
		}
173
		nmk = fabriquerMarqueur(pointCommune,"");
174
		nmk.setMap(map);
175
		info.setContent("<div id=\"info_contenu\">"+
268 aurelien 176
				"Déplacez ce marqueur pour localiser votre observation (commune et coordonnées)<br />"
177
				+"longitude="
1958 aurelien 178
				+ nmk.getPosition().lng() + "<br />latitude="
179
				+ nmk.getPosition().lat() + "</div>");
180
		infoOuverte = true;
181
		info.open(map,nmk);
268 aurelien 182
	}
247 aurelien 183
 
184
	public void AfficherMessageAucuneInfos() {
185
		mettreAJourInfos(centreFrance,"", niveauZoom);
186
		Window.alert("Aucune information pour le nom donné");
187
	}
188
 
189
	public int getNiveauZoom() {
190
		return niveauZoom;
191
	}
192
 
268 aurelien 193
	private void ajouterListenerBouton(final String idBouton) {
194
 
195
		final ExtElement bouton = Ext.get(idBouton);
196
		if(bouton == null) {
197
			Timer t = new Timer() {
1292 aurelien 198
				@Override
268 aurelien 199
				public void run() {
200
					ajouterListenerBouton(idBouton);
201
				}
202
			};
203
			t.schedule(500);
204
			return;
1958 aurelien 205
		} else {
206
			bouton.addListener("click", new EventCallback() {
207
				@Override
208
				public void execute(EventObject e) {
209
					EntiteGeographiqueObservation infosCommune = new EntiteGeographiqueObservation(codeCommune,valeurCommune,null,null);
210
					infosCommune.setLat(pointCommune.lat()+"");
211
					infosCommune.setLon(pointCommune.lng()+"");
212
					oMediateur.rafraichirSaisieCommuneObservation(infosCommune);
213
				}
214
			});
215
 
216
			bouton.focus();
268 aurelien 217
		}
247 aurelien 218
	}
219
 
1292 aurelien 220
	@Override
247 aurelien 221
	public void rafraichir(Object nouvelleDonnees,
222
			boolean repandreRaffraichissement) {
223
		if (nouvelleDonnees instanceof String) {
224
 
225
			if(isVisible()) {
226
				Window.alert((String)nouvelleDonnees);
227
			}
228
		}
229
 
268 aurelien 230
		if (nouvelleDonnees instanceof EntiteGeographiqueObservation) {
247 aurelien 231
 
681 aurelien 232
			oMediateur.afficherFenetreCarto();
654 aurelien 233
 
268 aurelien 234
			EntiteGeographiqueObservation infos = (EntiteGeographiqueObservation) nouvelleDonnees;
247 aurelien 235
 
654 aurelien 236
			Double lat = 0.0;
237
			Double lon = 0.0;
238
 
239
			try {
240
				lat = Double.parseDouble(infos.getLat());
241
				lon = Double.parseDouble(infos.getLon());
242
			} catch (NumberFormatException nbe) {
243
 
244
			}
245
 
1958 aurelien 246
			LatLng coord = LatLng.create(lat, lon);
2270 mathias 247
 
248
			String nouvelleValeurCommune = infos.getZoneGeo();
249
			valeurCommune = nouvelleValeurCommune;
965 aurelien 250
			codeCommune = Util.convertirChaineZoneGeoVersDepartement(infos.getIdZoneGeo());
268 aurelien 251
			pointCommune = coord;
247 aurelien 252
 
253
			mettreAJourInfos(coord, "", niveauZoom);
254
		}
255
 
256
		masquerChargement(false);
257
	}
258
 
259
	public Marker fabriquerMarqueur(LatLng pointMarqueur, final String texte) {
260
		// ajout de marqueurs déplacables, qui affichent une bulle d'information
1958 aurelien 261
		MarkerOptions options = MarkerOptions.create();
247 aurelien 262
		options.setDraggable(true);
1958 aurelien 263
		options.setPosition(pointMarqueur);
264
		final Marker marker = Marker.create(options);
247 aurelien 265
 
1958 aurelien 266
		marker.addDragEndListener(new Marker.DragEndHandler() {
267
 
1292 aurelien 268
			@Override
1958 aurelien 269
			public void handle(MouseEvent event) {
270
				obtenirInfosCommunes(event.getLatLng());
247 aurelien 271
			}
272
		});
1958 aurelien 273
 
274
		marker.addDragStartListener(new Marker.DragStartHandler() {
275
 
1292 aurelien 276
			@Override
1958 aurelien 277
			public void handle(MouseEvent event) {
278
				info.close();
247 aurelien 279
			}
280
		});
281
 
1958 aurelien 282
		marker.addClickListener(new Marker.ClickHandler() {
283
 
1292 aurelien 284
			@Override
1958 aurelien 285
			public void handle(MouseEvent event) {
286
				if(infoOuverte) {
247 aurelien 287
					info.close();
1958 aurelien 288
					infoOuverte = false;
247 aurelien 289
				} else {
290
					afficherInfoMarker(marker);
1958 aurelien 291
					infoOuverte = true;
247 aurelien 292
				}
1958 aurelien 293
			}
247 aurelien 294
		});
295
		return marker;
296
	}
1959 aurelien 297
 
298
	public void recentrerCarte() {
299
		if(nmk != null && nmk.getVisible()) {
300
			recentrerCarte(nmk.getPosition());
301
		}
302
	}
247 aurelien 303
 
1959 aurelien 304
	public void recentrerCarte(LatLng point) {
1958 aurelien 305
		if(this.getWidth() > 12 && this.getHeight() > 24) {
323 aurelien 306
			panneauCarto.setSize((this.getWidth() - 12)+"px" , (this.getHeight() - 12)+"px");
307
		}
1958 aurelien 308
		panneauCarto.doLayout();
309
		panneauCarto.expand();
310
		panneauCarto.setHeight("100%");
311
		panneauCarto.setWidth("100%");
312
		expand();
1959 aurelien 313
		map.setCenter(point);
247 aurelien 314
	}
315
 
316
	public boolean isCarteAjoutee() {
317
		return carteAjoutee;
318
	}
319
 
1958 aurelien 320
	public void afficherInfoMarker(Marker marker) {
247 aurelien 321
		final String htmlBoutonOk = "<br /><button id=\"okMap\" class=\"x-btn-text\" type=\"button\">OK</button>";
322
 
276 aurelien 323
		String contenuMarker = "";
324
 
325
		if(valeurCommune == null || valeurCommune.trim().equals("")) {
1958 aurelien 326
			if(marker.getPosition().lng() == 0 && marker.getPosition().lat() == 0) {
681 aurelien 327
				LatLng point = centreFrance;
1958 aurelien 328
				marker.setPosition(point);
681 aurelien 329
				niveauZoom = 6;
330
			}
276 aurelien 331
			contenuMarker = "<div id=\"info_contenu\">"+
2270 mathias 332
			"Aucun nom de lieu trouvé à cet emplacement <br />"
333
			+"Déplacez ce marqueur pour localiser votre observation (nom de lieu et coordonnées)<br />"
276 aurelien 334
			+"longitude="
1958 aurelien 335
			+ marker.getPosition().lng() + "<br />latitude="
336
			+ marker.getPosition().lat() + "</div>";
276 aurelien 337
		} else {
338
			contenuMarker = ""
339
			+valeurCommune+" ("+codeCommune+")<br />"
340
			+"longitude="
1958 aurelien 341
			+ marker.getPosition().lng() + "<br />latitude="
342
			+ marker.getPosition().lat() ;
276 aurelien 343
		}
344
 
1958 aurelien 345
		info.setContent(contenuMarker+ htmlBoutonOk);
346
		info.open(map, marker);
268 aurelien 347
 
1958 aurelien 348
		info.addDomReadyListenerOnce(new InfoWindow.DomReadyHandler() {
349
			@Override
350
			public void handle() {
351
				ajouterListenerBouton("okMap");
352
			}
353
		});
354
 
1959 aurelien 355
		recentrerCarte(marker.getPosition());
247 aurelien 356
	}
1958 aurelien 357
 
358
	public double[] getCoordsCentreFrance() {
359
		double[] coords = {47.0504, 2.2347};
360
		return coords;
654 aurelien 361
	}
247 aurelien 362
}