Subversion Repositories eFlore/Applications.del

Rev

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

Rev Author Line No. Line
148 benjamin 1
package org.tela_botanica.del.client.vues.rechercheimages;
9 benjamin 2
 
3
import java.util.List;
4
 
148 benjamin 5
import org.tela_botanica.del.client.cache.CacheClient;
6
import org.tela_botanica.del.client.modeles.Image;
178 benjamin 7
import org.tela_botanica.del.client.modeles.Protocole;
148 benjamin 8
import org.tela_botanica.del.client.navigation.evenement.BusEvenementiel;
178 benjamin 9
import org.tela_botanica.del.client.navigation.evenement.changementprotocole.EvenementChangementProtocole;
148 benjamin 10
import org.tela_botanica.del.client.navigation.evenement.rechercheimage.EvenementRechercheImage;
289 benjamin 11
import org.tela_botanica.del.client.services.rest.ImageService;
291 benjamin 12
import org.tela_botanica.del.client.services.rest.ProtocoleService;
9 benjamin 13
import org.tela_botanica.del.client.utils.MockDatasource;
14
 
178 benjamin 15
import com.google.gwt.event.dom.client.ChangeEvent;
16
import com.google.gwt.event.dom.client.ChangeHandler;
9 benjamin 17
import com.google.gwt.event.dom.client.ClickEvent;
18
import com.google.gwt.event.dom.client.ClickHandler;
277 gduche 19
import com.google.gwt.event.dom.client.HasChangeHandlers;
20
import com.google.gwt.event.dom.client.HasClickHandlers;
21
import com.google.gwt.event.dom.client.HasKeyPressHandlers;
204 gduche 22
import com.google.gwt.event.dom.client.KeyCodes;
23
import com.google.gwt.event.dom.client.KeyPressEvent;
24
import com.google.gwt.event.dom.client.KeyPressHandler;
9 benjamin 25
import com.google.gwt.user.client.ui.HasWidgets;
277 gduche 26
import com.google.gwt.user.client.ui.IsWidget;
9 benjamin 27
 
178 benjamin 28
public class RechercheImagePresenteur {
9 benjamin 29
 
277 gduche 30
	public interface Vue extends IsWidget {
31
 
32
		public abstract void ajouterProtocole(String protocole);
33
		public abstract void selectionnerProtocole(int index);
34
		public abstract HasClickHandlers getBoutonChercher();
35
		public abstract HasKeyPressHandlers getChampEspece();
36
		public abstract HasChangeHandlers getListeProtocoles();
278 gduche 37
		public abstract HasWidgets getZoneResultats();
277 gduche 38
	}
39
 
40
	private Vue vue;
41
 
291 benjamin 42
	private final ImageService imageService = MockDatasource.getInstance();
43
	private final ProtocoleService protocoleService = MockDatasource.getInstance();
178 benjamin 44
	private String protocoleParDefaut = Protocole.IDENTIFICATION_AUTOMATIQUE;
45
	private List<Protocole> protocoles;
9 benjamin 46
 
277 gduche 47
	/**
48
	 * Constructeur
49
	 * */
50
	public RechercheImagePresenteur(Vue vue) {
51
		this.vue = vue;
52
 
178 benjamin 53
		if (CacheClient.getInstance().getProtocoleCourant() == null) {
291 benjamin 54
			CacheClient.getInstance().setProtocoleCourant(protocoleService.getProtocole(protocoleParDefaut));
178 benjamin 55
		}
9 benjamin 56
	}
57
 
58
	public void go(HasWidgets composite) {
277 gduche 59
		composite.add(vue.asWidget());
26 gduche 60
		gererEvenements();
178 benjamin 61
		chargerProtocoles();
62
	}
148 benjamin 63
 
178 benjamin 64
	private void chargerProtocoles() {
291 benjamin 65
		protocoles = protocoleService.getProtocoles();
178 benjamin 66
		for (Protocole protocole : protocoles) {
277 gduche 67
			vue.ajouterProtocole(protocole.getNom());
148 benjamin 68
		}
277 gduche 69
		vue.selectionnerProtocole(protocoles.indexOf(CacheClient.getInstance().getProtocoleCourant()));
9 benjamin 70
	}
71
 
26 gduche 72
	public void gererEvenements() {
9 benjamin 73
 
277 gduche 74
		vue.getBoutonChercher().addClickHandler(new ClickHandler() {
26 gduche 75
 
9 benjamin 76
			@Override
77
			public void onClick(ClickEvent event) {
279 gduche 78
				chargerImages(0, CacheClient.getInstance().getPaginationPasRechercheImage());
9 benjamin 79
			}
80
		});
81
 
277 gduche 82
		vue.getChampEspece().addKeyPressHandler(new KeyPressHandler() {
9 benjamin 83
 
204 gduche 84
			public void onKeyPress(KeyPressEvent event) {
85
				if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
279 gduche 86
					chargerImages(0, CacheClient.getInstance().getPaginationPasRechercheImage());
204 gduche 87
				}
9 benjamin 88
			}
89
		});
90
 
277 gduche 91
		vue.getListeProtocoles().addChangeHandler(new ChangeHandler() {
9 benjamin 92
 
124 gduche 93
			@Override
178 benjamin 94
			public void onChange(ChangeEvent event) {
277 gduche 95
 
96
				Protocole protocoleCourant = (Protocole) event.getSource();
178 benjamin 97
				EvenementChangementProtocole evenement = new EvenementChangementProtocole(protocoleCourant);
98
				BusEvenementiel.getInstance().fireEvent(evenement);
124 gduche 99
			}
178 benjamin 100
		});
9 benjamin 101
	}
102
 
279 gduche 103
	private void chargerImages(int debut, int fin) {
291 benjamin 104
		List<Image> imagesFromDatabase = imageService.getImages(debut, fin);
178 benjamin 105
		EvenementRechercheImage evenementRechercheImage = new EvenementRechercheImage(imagesFromDatabase);
106
		BusEvenementiel.getInstance().fireEvent(evenementRechercheImage);
9 benjamin 107
	}
277 gduche 108
 
278 gduche 109
	public HasWidgets getZoneResultats() {
110
		return vue.getZoneResultats();
9 benjamin 111
	}
112
}