Subversion Repositories eFlore/Applications.del

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14 benjamin 1
package org.tela_botanica.del.client.vues.rechercheobservations;
9 benjamin 2
 
3
import java.util.ArrayList;
4
import java.util.Iterator;
5
import java.util.List;
6
 
7
import org.tela_botanica.del.client.modeles.Observation;
8
import org.tela_botanica.del.client.utils.KeyboardKeyListener;
9
import org.tela_botanica.del.client.utils.MockDatasource;
14 benjamin 10
import org.tela_botanica.del.client.vues.rechercheobservations.pagination.NumeroPagePresenteur;
9 benjamin 11
 
12
import com.google.gwt.event.dom.client.ClickEvent;
13
import com.google.gwt.event.dom.client.ClickHandler;
14
import com.google.gwt.user.client.ui.HasWidgets;
15
import com.google.gwt.user.client.ui.Panel;
16
import com.google.gwt.user.client.ui.VerticalPanel;
17
 
14 benjamin 18
public class ObservationRecherchePresenteur {
9 benjamin 19
 
20
	private final MockDatasource observationService = MockDatasource
21
			.getInstance();
22
 
14 benjamin 23
	private final ObservationRechercheVue view = new ObservationRechercheVue();
9 benjamin 24
 
14 benjamin 25
	private static ObservationRecherchePresenteur instance;
9 benjamin 26
 
27
	private List<Observation> observations;
28
 
14 benjamin 29
	public ObservationRecherchePresenteur() {
9 benjamin 30
		instance = this;
31
	}
32
 
33
	public void go(HasWidgets composite) {
34
		composite.add(view);
35
		handleEvents();
36
	}
37
 
38
	@SuppressWarnings("deprecation")
39
	public void handleEvents() {
40
		view.getSearchHtml().addClickHandler(new ClickHandler() {
41
 
42
			@Override
43
			public void onClick(ClickEvent event) {
44
				loadObservations();
45
			}
46
		});
47
 
48
		view.getTextBox().addKeyboardListener(new KeyboardKeyListener() {
49
 
50
			@Override
51
			public void onEnterKeyUp() {
52
				loadObservations();
53
			}
54
		});
55
 
56
	}
57
 
58
	private void loadObservations() {
59
 
60
		view.getPaginationPanel().clear();
61
		view.setNumImage(0);
62
		view.getImageTable().clear();
63
		view.getLoadPanel().add(view.getContactingServerHTML());
64
 
65
		// String taxaName = URL.encode(view.getTextBox().getText());
66
 
67
		observations = observationService.getObservations();
68
 
69
		view.getImagePanels().clear();
70
 
71
		List<Panel> imagePanels = new ArrayList<Panel>();
72
		for (int i = 0; i < observations.size(); i++) {
73
			Panel imagePanel = new VerticalPanel();
74
			imagePanels.add(imagePanel);
75
			view.addImagePanel(imagePanel);
76
		}
77
 
78
		view.showImagePanels(0, view.getNbImagesPerPage());
79
		Iterator<Panel> panelIterator = imagePanels.iterator();
80
		for (Observation observation : observations) {
81
			Panel imagePanel = panelIterator.next();
14 benjamin 82
			new ObservationPresenteur(observation).go(imagePanel);
9 benjamin 83
		}
84
 
85
		createPaginationWidget(observations.size());
86
 
87
		view.getLoadPanel().clear();
88
 
89
	}
90
 
91
	private void createPaginationWidget(int nbImages) {
92
 
93
		int nbPages = nbImages / view.getNbImagesPerPage();
94
		if (nbImages % view.getNbImagesPerPage() == 0) {
95
			nbPages--;
96
		}
97
		for (int i = 0; i <= nbPages; i++) {
14 benjamin 98
			new NumeroPagePresenteur(i + 1).go(view.getPaginationPanel());
9 benjamin 99
		}
100
	}
101
 
102
	public void showImagePanels(int first, int last) {
103
		view.showImagePanels(first, last);
104
	}
105
 
14 benjamin 106
	public static ObservationRecherchePresenteur getInstance() {
9 benjamin 107
		return instance;
108
	}
109
 
14 benjamin 110
	public ObservationRechercheVue getView() {
9 benjamin 111
		return view;
112
	}
113
 
114
	public List<Observation> getObservations() {
115
		return observations;
116
	}
117
 
118
}