Subversion Repositories eFlore/Applications.cel

Rev

Rev 2275 | 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
	private ObservationMediateur oMediateur = null;
37
 
38
	Panel panneauCarto = null;
1958 aurelien 39
	GoogleMap map = null;
40
	Marker nmk = null;
247 aurelien 41
 
42
	private boolean carteAjoutee = false;
1958 aurelien 43
	private boolean infoOuverte = false;
247 aurelien 44
 
2270 mathias 45
	String valeurCommune = "Bourges"; // j'aurais préféré Bort-les-Orgues
296 aurelien 46
	LatLng centreFrance = null;
299 aurelien 47
	LatLng pointCommune = null;
247 aurelien 48
	String codeCommune = "";
49
 
681 aurelien 50
	int niveauZoom = 6;
247 aurelien 51
 
52
	Label panneauExplication = new Label(
53
			"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");
54
 
55
	private InfoWindow info;
2275 mathias 56
	private boolean zoomerSurRetour = false;
247 aurelien 57
 
58
	public CartographieObservationVue(ObservationMediateur om) {
2275 mathias 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());
2275 mathias 113
		map.setZoom(zoom);
247 aurelien 114
	}
115
 
1958 aurelien 116
	private void initialiserCarte(boolean commune) {
2275 mathias 117
 
1958 aurelien 118
		panneauCarto.clear();
2275 mathias 119
 
1958 aurelien 120
		centreFrance = LatLng.create(47.0504, 2.2347);
299 aurelien 121
		pointCommune = centreFrance;
2275 mathias 122
 
123
		MapOptions options = MapOptions.create();
124
	    options.setCenter(centreFrance);
125
	    options.setZoom(6);
1958 aurelien 126
	    options.setMapTypeId(MapTypeId.HYBRID);
127
	    options.setDraggable(true);
128
	    options.setMapTypeControl(true);
2275 mathias 129
	    options.setScaleControl(true);
130
	    options.setScrollwheel(true);
131
 
132
		map = GoogleMap.create(panneauCarto.getElement(), options );
1958 aurelien 133
 
134
		InfoWindowOptions infopts = InfoWindowOptions.create();
135
		info = InfoWindow.create();
136
		info.setOptions(infopts);
137
		info.addCloseClickListener(new InfoWindow.CloseClickHandler() {
138
			@Override
139
			public void handle() {
140
				infoOuverte = false;
141
			}
142
		});
143
 
268 aurelien 144
		if(!commune) {
145
			fabriquerMarqueurIndication();
146
		}
1958 aurelien 147
 
148
		map.addClickListener(new ClickHandler() {
1292 aurelien 149
			@Override
1958 aurelien 150
			public void handle(MouseEvent event) {
391 aurelien 151
				obtenirInfosCommunes(event.getLatLng());
152
			}
153
		});
154
 
1958 aurelien 155
		map.addZoomChangedListener(new ZoomChangedHandler() {
1292 aurelien 156
			@Override
1958 aurelien 157
			public void handle() {
158
				niveauZoom = (int)map.getZoom();
264 aurelien 159
			}
160
		});
1958 aurelien 161
 
162
		panneauCarto.doLayout();
163
		panneauCarto.expand();
164
		panneauCarto.setHeight("100%");
165
		panneauCarto.setWidth("100%");
166
		expand();
247 aurelien 167
	}
268 aurelien 168
 
169
	public void fabriquerMarqueurIndication() {
1958 aurelien 170
		GoogleMap mapNulle = null;
171
		if(nmk != null) {
172
			nmk.setMap(mapNulle);
173
		}
174
		nmk = fabriquerMarqueur(pointCommune,"");
175
		nmk.setMap(map);
176
		info.setContent("<div id=\"info_contenu\">"+
268 aurelien 177
				"Déplacez ce marqueur pour localiser votre observation (commune et coordonnées)<br />"
178
				+"longitude="
2276 mathias 179
				+ Util.tronquerNombrePourAffichage("" + nmk.getPosition().lng(), 5) + "<br />latitude="
180
				+ Util.tronquerNombrePourAffichage("" + nmk.getPosition().lat(), 5) + "</div>");
1958 aurelien 181
		infoOuverte = true;
182
		info.open(map,nmk);
268 aurelien 183
	}
247 aurelien 184
 
185
	public void AfficherMessageAucuneInfos() {
186
		mettreAJourInfos(centreFrance,"", niveauZoom);
187
		Window.alert("Aucune information pour le nom donné");
188
	}
189
 
190
	public int getNiveauZoom() {
191
		return niveauZoom;
192
	}
193
 
268 aurelien 194
	private void ajouterListenerBouton(final String idBouton) {
195
 
196
		final ExtElement bouton = Ext.get(idBouton);
197
		if(bouton == null) {
198
			Timer t = new Timer() {
1292 aurelien 199
				@Override
268 aurelien 200
				public void run() {
201
					ajouterListenerBouton(idBouton);
202
				}
203
			};
204
			t.schedule(500);
205
			return;
1958 aurelien 206
		} else {
207
			bouton.addListener("click", new EventCallback() {
208
				@Override
209
				public void execute(EventObject e) {
210
					EntiteGeographiqueObservation infosCommune = new EntiteGeographiqueObservation(codeCommune,valeurCommune,null,null);
211
					infosCommune.setLat(pointCommune.lat()+"");
212
					infosCommune.setLon(pointCommune.lng()+"");
213
					oMediateur.rafraichirSaisieCommuneObservation(infosCommune);
214
				}
215
			});
216
 
217
			bouton.focus();
268 aurelien 218
		}
247 aurelien 219
	}
220
 
1292 aurelien 221
	@Override
2275 mathias 222
	public void rafraichir(Object nouvelleDonnees, boolean repandreRaffraichissement) {
223
 
247 aurelien 224
		if (nouvelleDonnees instanceof String) {
225
 
226
			if(isVisible()) {
227
				Window.alert((String)nouvelleDonnees);
228
			}
229
		}
230
 
268 aurelien 231
		if (nouvelleDonnees instanceof EntiteGeographiqueObservation) {
247 aurelien 232
 
681 aurelien 233
			oMediateur.afficherFenetreCarto();
268 aurelien 234
			EntiteGeographiqueObservation infos = (EntiteGeographiqueObservation) nouvelleDonnees;
654 aurelien 235
			Double lat = 0.0;
236
			Double lon = 0.0;
237
 
238
			try {
239
				lat = Double.parseDouble(infos.getLat());
240
				lon = Double.parseDouble(infos.getLon());
241
			} catch (NumberFormatException nbe) {
242
 
243
			}
1958 aurelien 244
			LatLng coord = LatLng.create(lat, lon);
2275 mathias 245
			// si on a un point, on zoome fort dès le début
246
			if (zoomerSurRetour) {
247
				niveauZoom = 16;
248
				zoomerSurRetour = false;
249
			}
2270 mathias 250
 
251
			String nouvelleValeurCommune = infos.getZoneGeo();
252
			valeurCommune = nouvelleValeurCommune;
965 aurelien 253
			codeCommune = Util.convertirChaineZoneGeoVersDepartement(infos.getIdZoneGeo());
268 aurelien 254
			pointCommune = coord;
247 aurelien 255
 
256
			mettreAJourInfos(coord, "", niveauZoom);
257
		}
258
 
259
		masquerChargement(false);
260
	}
261
 
262
	public Marker fabriquerMarqueur(LatLng pointMarqueur, final String texte) {
263
		// ajout de marqueurs déplacables, qui affichent une bulle d'information
1958 aurelien 264
		MarkerOptions options = MarkerOptions.create();
247 aurelien 265
		options.setDraggable(true);
1958 aurelien 266
		options.setPosition(pointMarqueur);
267
		final Marker marker = Marker.create(options);
247 aurelien 268
 
1958 aurelien 269
		marker.addDragEndListener(new Marker.DragEndHandler() {
270
 
1292 aurelien 271
			@Override
1958 aurelien 272
			public void handle(MouseEvent event) {
273
				obtenirInfosCommunes(event.getLatLng());
247 aurelien 274
			}
275
		});
1958 aurelien 276
 
277
		marker.addDragStartListener(new Marker.DragStartHandler() {
278
 
1292 aurelien 279
			@Override
1958 aurelien 280
			public void handle(MouseEvent event) {
281
				info.close();
247 aurelien 282
			}
283
		});
284
 
1958 aurelien 285
		marker.addClickListener(new Marker.ClickHandler() {
286
 
1292 aurelien 287
			@Override
1958 aurelien 288
			public void handle(MouseEvent event) {
289
				if(infoOuverte) {
247 aurelien 290
					info.close();
1958 aurelien 291
					infoOuverte = false;
247 aurelien 292
				} else {
293
					afficherInfoMarker(marker);
1958 aurelien 294
					infoOuverte = true;
247 aurelien 295
				}
1958 aurelien 296
			}
247 aurelien 297
		});
298
		return marker;
299
	}
1959 aurelien 300
 
301
	public void recentrerCarte() {
302
		if(nmk != null && nmk.getVisible()) {
303
			recentrerCarte(nmk.getPosition());
304
		}
305
	}
247 aurelien 306
 
1959 aurelien 307
	public void recentrerCarte(LatLng point) {
1958 aurelien 308
		if(this.getWidth() > 12 && this.getHeight() > 24) {
323 aurelien 309
			panneauCarto.setSize((this.getWidth() - 12)+"px" , (this.getHeight() - 12)+"px");
310
		}
1958 aurelien 311
		panneauCarto.doLayout();
312
		panneauCarto.expand();
313
		panneauCarto.setHeight("100%");
314
		panneauCarto.setWidth("100%");
315
		expand();
1959 aurelien 316
		map.setCenter(point);
247 aurelien 317
	}
318
 
319
	public boolean isCarteAjoutee() {
320
		return carteAjoutee;
321
	}
322
 
1958 aurelien 323
	public void afficherInfoMarker(Marker marker) {
247 aurelien 324
		final String htmlBoutonOk = "<br /><button id=\"okMap\" class=\"x-btn-text\" type=\"button\">OK</button>";
325
 
276 aurelien 326
		String contenuMarker = "";
327
 
328
		if(valeurCommune == null || valeurCommune.trim().equals("")) {
1958 aurelien 329
			if(marker.getPosition().lng() == 0 && marker.getPosition().lat() == 0) {
681 aurelien 330
				LatLng point = centreFrance;
1958 aurelien 331
				marker.setPosition(point);
681 aurelien 332
				niveauZoom = 6;
333
			}
276 aurelien 334
			contenuMarker = "<div id=\"info_contenu\">"+
2270 mathias 335
			"Aucun nom de lieu trouvé à cet emplacement <br />"
336
			+"Déplacez ce marqueur pour localiser votre observation (nom de lieu et coordonnées)<br />"
276 aurelien 337
			+"longitude="
2276 mathias 338
			+ Util.tronquerNombrePourAffichage("" + marker.getPosition().lng(), 5) + "<br />latitude="
339
			+ Util.tronquerNombrePourAffichage("" + marker.getPosition().lat(), 5) + "</div>";
276 aurelien 340
		} else {
341
			contenuMarker = ""
342
			+valeurCommune+" ("+codeCommune+")<br />"
343
			+"longitude="
2276 mathias 344
			+ Util.tronquerNombrePourAffichage("" + marker.getPosition().lng(), 5) + "<br />latitude="
345
			+ Util.tronquerNombrePourAffichage("" + marker.getPosition().lat(), 5) ;
276 aurelien 346
		}
347
 
1958 aurelien 348
		info.setContent(contenuMarker+ htmlBoutonOk);
349
		info.open(map, marker);
268 aurelien 350
 
1958 aurelien 351
		info.addDomReadyListenerOnce(new InfoWindow.DomReadyHandler() {
352
			@Override
353
			public void handle() {
354
				ajouterListenerBouton("okMap");
355
			}
356
		});
357
 
1959 aurelien 358
		recentrerCarte(marker.getPosition());
247 aurelien 359
	}
1958 aurelien 360
 
361
	public double[] getCoordsCentreFrance() {
362
		double[] coords = {47.0504, 2.2347};
363
		return coords;
654 aurelien 364
	}
2275 mathias 365
 
366
	public void setDoitZoomerSurRetour(boolean b) {
367
		zoomerSurRetour  = b;
368
	}
247 aurelien 369
}