Subversion Repositories eFlore/Applications.del

Rev

Rev 780 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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