Blame | Last modification | View Log | RSS feed
package org.tela_botanica.del.client.vues.searchobservations;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.tela_botanica.del.client.modeles.Observation;
import org.tela_botanica.del.client.utils.KeyboardKeyListener;
import org.tela_botanica.del.client.utils.MockDatasource;
import org.tela_botanica.del.client.vues.searchobservations.pagination.NumberPagePresenter;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
public class ObservationSearchPresenter {
private final MockDatasource observationService = MockDatasource
.getInstance();
private final ObservationSearchView view = new ObservationSearchView();
private static ObservationSearchPresenter instance;
private List<Observation> observations;
public ObservationSearchPresenter() {
instance = this;
}
public void go(HasWidgets composite) {
composite.add(view);
handleEvents();
}
@SuppressWarnings("deprecation")
public void handleEvents() {
view.getSearchHtml().addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
loadObservations();
}
});
view.getTextBox().addKeyboardListener(new KeyboardKeyListener() {
@Override
public void onEnterKeyUp() {
loadObservations();
}
});
}
private void loadObservations() {
view.getPaginationPanel().clear();
view.setNumImage(0);
view.getImageTable().clear();
view.getLoadPanel().add(view.getContactingServerHTML());
// String taxaName = URL.encode(view.getTextBox().getText());
observations = observationService.getObservations();
view.getImagePanels().clear();
List<Panel> imagePanels = new ArrayList<Panel>();
for (int i = 0; i < observations.size(); i++) {
Panel imagePanel = new VerticalPanel();
imagePanels.add(imagePanel);
view.addImagePanel(imagePanel);
}
view.showImagePanels(0, view.getNbImagesPerPage());
Iterator<Panel> panelIterator = imagePanels.iterator();
for (Observation observation : observations) {
Panel imagePanel = panelIterator.next();
new ObservationPresenter(observation).go(imagePanel);
}
createPaginationWidget(observations.size());
view.getLoadPanel().clear();
}
private void createPaginationWidget(int nbImages) {
int nbPages = nbImages / view.getNbImagesPerPage();
if (nbImages % view.getNbImagesPerPage() == 0) {
nbPages--;
}
for (int i = 0; i <= nbPages; i++) {
new NumberPagePresenter(i + 1).go(view.getPaginationPanel());
}
}
public void showImagePanels(int first, int last) {
view.showImagePanels(first, last);
}
public static ObservationSearchPresenter getInstance() {
return instance;
}
public ObservationSearchView getView() {
return view;
}
public List<Observation> getObservations() {
return observations;
}
}