Subversion Repositories eFlore/Applications.del

Rev

Rev 1203 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1196 gduche 1
package org.tela_botanica.del.client.vues.pictoflora.resultats;
2
 
3
import java.util.List;
4
 
5
import org.tela_botanica.del.client.cache.CacheClient;
6
import org.tela_botanica.del.client.composants.pagination.PaginationPresenteur;
7
import org.tela_botanica.del.client.composants.pagination.PaginationVue;
8
import org.tela_botanica.del.client.modeles.Image;
9
import org.tela_botanica.del.client.modeles.ImageServiceResultat;
10
import org.tela_botanica.del.client.modeles.InformationsRecherche;
11
import org.tela_botanica.del.client.modeles.ModeTri;
12
import org.tela_botanica.del.client.modeles.Protocole;
13
import org.tela_botanica.del.client.modeles.ProtocoleServiceResultat;
14
import org.tela_botanica.del.client.navigation.evenement.BusEvenementiel;
15
import org.tela_botanica.del.client.navigation.evenement.changementprotocole.EvenementChangementProtocole;
16
import org.tela_botanica.del.client.navigation.evenement.changementprotocole.GestionnaireEvenementChangementProtocole;
17
import org.tela_botanica.del.client.services.rest.ImageService;
18
import org.tela_botanica.del.client.services.rest.ProtocoleService;
19
import org.tela_botanica.del.client.services.rest.async.ImagesParTaxonCallback;
20
import org.tela_botanica.del.client.services.rest.async.ProtocolesCallback;
21
import org.tela_botanica.del.client.vues.pictoflora.resultats.images.ImagePresenteur;
22
import org.tela_botanica.del.client.vues.pictoflora.resultats.images.ImageVue;
23
 
24
import com.google.gwt.event.dom.client.ChangeEvent;
25
import com.google.gwt.event.dom.client.ChangeHandler;
26
import com.google.gwt.event.dom.client.ClickEvent;
27
import com.google.gwt.event.dom.client.ClickHandler;
28
import com.google.gwt.event.dom.client.HasChangeHandlers;
29
import com.google.gwt.event.dom.client.HasClickHandlers;
30
import com.google.gwt.user.client.Window;
31
import com.google.gwt.user.client.ui.HTMLPanel;
32
import com.google.gwt.user.client.ui.HasWidgets;
33
import com.google.gwt.user.client.ui.IsWidget;
34
import com.google.gwt.user.client.ui.ListBox;
35
 
36
public class ResultatPictofloraPresenteur {
37
	public interface Vue extends IsWidget {
38
		public void startChargement();
39
 
40
		public void nettoyer();
41
 
42
		public void creerPanneauxObservations(int size);
43
 
44
		public void stopChargement();
45
 
46
		public List<HasWidgets> getPanneauxImages();
47
 
48
		public HasWidgets getPanneauPagination();
49
 
50
		public HasWidgets getPanneauPaginationHaut();
51
 
52
		public HasClickHandlers getTriParNbVotesAscendant();
53
 
54
		public HasClickHandlers getTriParNbVotesDescendant();
55
 
56
		public HasClickHandlers getTriParDateAscendant();
57
 
58
		public HasClickHandlers getTriParDateDescendant();
59
 
60
		public HasClickHandlers getTriParNbTagsAscendant();
61
 
62
		public HasClickHandlers getTriParNbTagsDescendant();
63
 
64
		public void afficherElementsAucunResultatTrouve();
65
 
66
		public void afficherElementsResultatsTrouve();
67
 
68
		public HTMLPanel getImageTable();
69
 
70
		public String getNomProtocoleSelectionne();
71
 
72
		public int getIdProtocoleSelectionne();
73
 
74
		public void setListeProtocoles(ListBox listeProtocoles);
75
 
76
		public HasChangeHandlers getListeProtocoles();
77
 
78
		public void ajouterProtocoles(List<Protocole> protocoles);
79
 
80
		public void selectionnerProtocole(int index);
81
 
82
		public HasClickHandlers getLabelVote();
83
 
84
		public void masquerVoteAscendant();
85
 
86
		public void masquerVoteDescendant();
87
 
88
		public void afficherVoteAscendant();
89
 
90
		public void afficherVoteDescendant();
91
 
92
		public HasClickHandlers getLabelDate();
93
 
94
		public HasClickHandlers getLabelTag();
95
 
96
		public void masquerDateAscendant();
97
 
98
		public void masquerDateDescendant();
99
 
100
		public void afficherDateAscendant();
101
 
102
		public void afficherDateDescendant();
103
 
104
		public void masquerTagAscendant();
105
 
106
		public void afficherTagDescendant();
107
 
108
		public void masquerTagDescendant();
109
 
110
		public void afficherTagAscendant();
111
 
112
		public void mettreAJourDescriptionProtocoleCourant(
113
				Protocole protocoleCourant);
114
	}
115
 
116
	private Vue vue;
117
	private ImageService imageService;
118
	private final ProtocoleService protocoleService;
119
	private List<Protocole> protocoles;
120
	private ModeTri triCourantVote = ModeTri.TRI_ASCENDANT;
121
	private ModeTri triCourantDate = ModeTri.TRI_DESCENDANT;
122
	private ModeTri triCourantTag = ModeTri.TRI_DESCENDANT;
123
 
124
	public ResultatPictofloraPresenteur(ImageService imageService, ProtocoleService protocoleService, Vue vue) {
125
		this.vue = vue;
126
		this.imageService = imageService;
127
		this.protocoleService=protocoleService;
128
	}
129
 
130
	public void go(HasWidgets composite) {
131
		composite.add(vue.asWidget());
132
		vue.startChargement();
133
		rechercherImagesEtCreerWidgetPagination();
134
		gererEvenements();
135
		chargerProtocoles();
136
		vue.masquerVoteDescendant();
137
		vue.masquerVoteAscendant();
138
		// vue.masquerDateDescendant();
139
		vue.masquerDateAscendant();
140
		vue.masquerTagAscendant();
141
		vue.masquerTagDescendant();
142
	}
143
 
144
	public void gererEvenements() {
145
		vue.getListeProtocoles().addChangeHandler(new ChangeHandler() {
146
			@Override
147
			public void onChange(ChangeEvent event) {
148
				surChangementProtocole();
149
			}
150
		});
151
 
152
		ClickHandler surClicTriVote = new ClickHandler() {
153
			@Override
154
			public void onClick(ClickEvent event) {
155
				surClicTriVote();
156
			}
157
		};
158
 
159
		vue.getLabelVote().addClickHandler(surClicTriVote);
160
		vue.getTriParNbVotesAscendant().addClickHandler(surClicTriVote);
161
		vue.getTriParNbVotesDescendant().addClickHandler(surClicTriVote);
162
 
163
		ClickHandler surClicTriDate = new ClickHandler() {
164
			@Override
165
			public void onClick(ClickEvent event) {
166
				surClicTriDate();
167
			}
168
		};
169
 
170
		vue.getLabelDate().addClickHandler(surClicTriDate);
171
		vue.getTriParDateAscendant().addClickHandler(surClicTriDate);
172
		vue.getTriParDateDescendant().addClickHandler(surClicTriDate);
173
 
174
		ClickHandler surClicTriTag = new ClickHandler() {
175
			@Override
176
			public void onClick(ClickEvent event) {
177
				surClicTriTag();
178
			}
179
		};
180
 
181
		vue.getLabelTag().addClickHandler(surClicTriTag);
182
		vue.getTriParNbTagsAscendant().addClickHandler(surClicTriTag);
183
		vue.getTriParNbTagsDescendant().addClickHandler(surClicTriTag);
184
 
185
		BusEvenementiel.getInstance().addHandler(EvenementChangementProtocole.TYPE, new GestionnaireEvenementChangementProtocole() {
186
			@Override
187
			public void onChangementProtocole(EvenementChangementProtocole event) {
188
				InformationsRecherche informationsRechercheImages = CacheClient.getInstance().getInformationsRechercheImage();
189
				if (informationsRechercheImages.getTriParNbVotes() != ModeTri.PAS_DE_TRI) {
190
					informationsRechercheImages.setIdProtocoleSelectionne(event.getProtocole().getId() + "");
191
					chargerEtAfficherImagesPageEnCours();
192
				}
193
			}
194
		});
195
	}
196
 
197
	protected void surClicTriTag() {
198
		vue.masquerDateDescendant();
199
		vue.masquerDateAscendant();
200
		vue.masquerVoteDescendant();
201
		vue.masquerVoteAscendant();
202
		if (triCourantTag == ModeTri.TRI_ASCENDANT) {
203
			triCourantTag = ModeTri.TRI_DESCENDANT;
204
			vue.masquerTagAscendant();
205
			vue.afficherTagDescendant();
206
		} else {
207
			triCourantTag = ModeTri.TRI_ASCENDANT;
208
			vue.masquerTagDescendant();
209
			vue.afficherTagAscendant();
210
		}
211
		InformationsRecherche informationsRechercheImages = CacheClient.getInstance().getInformationsRechercheImage();
212
		informationsRechercheImages.setTriParNbTags(triCourantTag);
213
		informationsRechercheImages.setTriParDate(ModeTri.PAS_DE_TRI);
214
		informationsRechercheImages.setTriParNbVotes(ModeTri.PAS_DE_TRI);
215
		chargerEtAfficherImagesPageEnCours();
216
	}
217
 
218
	public void surClicTriVote() {
219
		vue.masquerDateDescendant();
220
		vue.masquerDateAscendant();
221
		vue.masquerTagAscendant();
222
		vue.masquerTagDescendant();
223
		if (triCourantVote == ModeTri.TRI_ASCENDANT) {
224
			triCourantVote = ModeTri.TRI_DESCENDANT;
225
			vue.masquerVoteAscendant();
226
			vue.afficherVoteDescendant();
227
		} else {
228
			triCourantVote = ModeTri.TRI_ASCENDANT;
229
			vue.masquerVoteDescendant();
230
			vue.afficherVoteAscendant();
231
		}
232
		InformationsRecherche informationsRechercheImages = CacheClient.getInstance().getInformationsRechercheImage();
233
		int IdProtocole = CacheClient.getInstance().getProtocoleCourant().getId();
234
		informationsRechercheImages.setTriParNbVotes(triCourantVote);
235
		informationsRechercheImages.setIdProtocoleSelectionne(IdProtocole + "");
236
		informationsRechercheImages.setTriParDate(ModeTri.PAS_DE_TRI);
237
		informationsRechercheImages.setTriParNbTags(ModeTri.PAS_DE_TRI);
238
		chargerEtAfficherImagesPageEnCours();
239
	}
240
 
241
	public void surClicTriDate() {
242
		vue.masquerVoteDescendant();
243
		vue.masquerVoteAscendant();
244
		if (triCourantDate == ModeTri.TRI_ASCENDANT) {
245
			triCourantDate = ModeTri.TRI_DESCENDANT;
246
			vue.masquerDateAscendant();
247
			vue.afficherDateDescendant();
248
		} else {
249
			triCourantDate = ModeTri.TRI_ASCENDANT;
250
			vue.masquerDateDescendant();
251
			vue.afficherDateAscendant();
252
		}
253
		InformationsRecherche informationsRechercheImages = CacheClient.getInstance().getInformationsRechercheImage();
254
		informationsRechercheImages.setTriParDate(triCourantDate);
255
		informationsRechercheImages.setTriParNbVotes(ModeTri.PAS_DE_TRI);
256
		informationsRechercheImages.setTriParNbTags(ModeTri.PAS_DE_TRI);
257
		chargerEtAfficherImagesPageEnCours();
258
	}
259
 
260
	public void surChangementProtocole() {
261
		Protocole protocoleCourant = null;
262
		for (Protocole protocole : protocoles) {
263
			if (protocole.getId() == vue.getIdProtocoleSelectionne()) {
264
				protocoleCourant = protocole;
265
			}
266
		}
267
		vue.mettreAJourDescriptionProtocoleCourant(protocoleCourant);
268
		CacheClient.getInstance().setProtocoleCourant(protocoleCourant);
269
		EvenementChangementProtocole evenement = new EvenementChangementProtocole(protocoleCourant);
270
		BusEvenementiel.getInstance().fireEvent(evenement);
271
	}
272
 
273
	private void chargerProtocoles() {
274
		// test pour ne pas charger les protocoles déjà chargés
275
		// TODO: faire un systeme de cache gérés par les web service eux même
276
		if (CacheClient.getInstance().getListeProtocoles() == null) {
277
			protocoleService.getProtocoles(new ProtocolesCallback() {
278
 
279
				@Override
280
				public void surRetour(ProtocoleServiceResultat protocolesRecus) {
281
					protocoles = protocolesRecus.getProtocoles();
282
					remplirListeProtocole(protocoles);
283
 
284
				}
285
			});
286
		} else {
287
			protocoles = CacheClient.getInstance().getListeProtocoles();
288
			remplirListeProtocole(CacheClient.getInstance().getListeProtocoles());
289
		}
290
	}
291
 
292
	private void remplirListeProtocole(List<Protocole> protocoles) {
293
		vue.ajouterProtocoles(protocoles);
294
		if (CacheClient.getInstance().getProtocoleCourant() == null) {
295
			vue.selectionnerProtocole(0);
296
			vue.mettreAJourDescriptionProtocoleCourant(protocoles.get(0));
297
		} else {
298
			vue.mettreAJourDescriptionProtocoleCourant(CacheClient.getInstance().getProtocoleCourant());
299
		}
300
	}
301
 
302
	public void rechercherImagesEtCreerWidgetPagination() {
303
		// appel du service d'image pour avoir le nb total d'elements pour la
304
		// pagination
305
		ImagesParTaxonCallback callback = new ImagesParTaxonCallback() {
306
 
307
			@Override
308
			public void surRetour(ImageServiceResultat imagesRecues) {
309
				creerWidgetPagination(imagesRecues.getNbTotalImagesPourLaRecherche());
310
				afficherImages(imagesRecues);
311
 
312
			}
313
 
314
			@Override
315
			public void surErreur(String messageErreur) {
316
				Window.alert(messageErreur);
317
 
318
			}
319
		};
320
		final int debut = (CacheClient.getInstance().getPageCouranteRechercheImage() - 1) * CacheClient.getInstance().getPasPagination();
321
		final int fin = (CacheClient.getInstance().getPageCouranteRechercheImage()) * CacheClient.getInstance().getPasPagination();
322
		imageService.getImagesParTaxon(CacheClient.getInstance().getInformationsRechercheImage(), debut, fin, callback);
323
	}
324
 
325
	private void chargerEtAfficherImages(final int premier, final int dernier) {
326
		vue.startChargement();
327
		vue.nettoyer();
328
		ImagesParTaxonCallback callback = new ImagesParTaxonCallback() {
329
 
330
			@Override
331
			public void surRetour(ImageServiceResultat imagesRecues) {
332
				afficherImages(imagesRecues);
333
			}
334
 
335
			@Override
336
			public void surErreur(String messageErreur) {
337
				Window.alert(messageErreur);
338
			}
339
 
340
		};
341
		imageService.getImagesParTaxon(CacheClient.getInstance().getInformationsRechercheImage(), premier, dernier, callback);
342
	}
343
 
344
	public void afficherImages(ImageServiceResultat imageServiceResult) {
345
		List<Image> images = imageServiceResult.getImages();
346
		if (images == null || images.size() == 0) {
347
			vue.afficherElementsAucunResultatTrouve();
348
		} else {
349
			vue.afficherElementsResultatsTrouve();
350
			for (Image image : images) {
351
				ImagePresenteur imagePresenteur = new ImagePresenteur(image, CacheClient.getInstance().getProtocoleCourant(), new ImageVue());
352
				imagePresenteur.go(vue.getImageTable());
353
			}
354
		}
355
		vue.stopChargement();
356
	}
357
 
358
	private void creerWidgetPagination(int nbImages) {
359
		PaginationPresenteur imagesPaginationPresenteurHaut = creerPresenteurPagination(nbImages);
360
		PaginationPresenteur imagesPaginationPresenteurBas = creerPresenteurPagination(nbImages);
361
		imagesPaginationPresenteurHaut.setGroupePagination("pagination_images");
362
		imagesPaginationPresenteurBas.setGroupePagination("pagination_images");
363
		imagesPaginationPresenteurHaut.go(vue.getPanneauPaginationHaut());
364
		imagesPaginationPresenteurBas.go(vue.getPanneauPagination());
365
	}
366
 
367
	public PaginationPresenteur creerPresenteurPagination(int nbImages) {
368
		PaginationPresenteur imagesPaginationPresenteur = new PaginationPresenteur(new PaginationVue(), nbImages, CacheClient.getInstance().getPasPagination(), CacheClient.getInstance().getPageCouranteRechercheImage()) {
369
			@Override
370
			public void chargerElements(int debut, int fin) {
371
				chargerEtAfficherImages(debut, fin);
372
				CacheClient.getInstance().setPageCouranteRechercheImages(getPageCourante());
373
			}
374
 
375
			@Override
376
			public void actualiserPasCache(int pas) {
377
				CacheClient.getInstance().setPasPagination(pas);
378
			}
379
		};
380
		return imagesPaginationPresenteur;
381
	}
382
 
383
	public void chargerEtAfficherImagesPageEnCours() {
384
		final int debut = (CacheClient.getInstance().getPageCouranteRechercheImage() - 1) * CacheClient.getInstance().getPasPagination();
385
		final int fin = (CacheClient.getInstance().getPageCouranteRechercheImage()) * CacheClient.getInstance().getPasPagination();
386
		chargerEtAfficherImages(debut, fin);
387
	}
388
 
389
	public Vue getVue() {
390
		return vue;
391
	}
392
}