Subversion Repositories eFlore/Applications.del

Rev

Rev 220 | Rev 330 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.tela_botanica.del.client.vues.rechercheobservations;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.tela_botanica.del.client.composants.presenteur.Presenteur;
import org.tela_botanica.del.client.modeles.Observation;
import org.tela_botanica.del.client.utils.MockDatasource;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasKeyPressHandlers;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasVisibility;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

public class RechercheObservationsPresenteur extends Presenteur {

        public abstract interface Vue extends IsWidget {
                public HasClickHandlers getRecherchePrincipaleHasClickHandler();
                public HasKeyPressHandlers getRecherchePrincipaleHasKeyPressHandlers();
                public HasText getRecherchePrincipaleHasText();
                public HasText getDepartement();
                public HasText getCommune();
                public HasText getTaxon();
                public HasText getFamille();
                public HasText getGenre();
                public HasText getTag();
                public HasText getMotCle();
                public HasText getAuteur();
                public HasText getDate();
                public HasClickHandlers getBoutonRecherche();
                public HasClickHandlers getBoutonRechercheAvancee();
                public HasClickHandlers getLienRechercheAvancee();
                public HasText getRecherchePrecedente();
                public HasWidgets getRechercheAvanceeHasWidget();
                public HasVisibility getRechercheAvanceeHasVisibility();
                public HasWidgets getZoneObservations();
                public HasWidgets getZonePagination();
        }
        private Vue vue;
        
        private List<Observation> observations;

        public RechercheObservationsPresenteur(Vue vue) {
                this.vue = vue;
        }

        public void go(HasWidgets composite) {
                if (composite == null) {
                        composite = RootPanel.get();
                }
                composite.add(vue.asWidget());
                gererEvenements();

                // On commence par afficher la totalité des observations
                chercherObservations(null);
                afficherObservations();
        }

        protected void gererEvenements() {

                // Gestion du clic dans la zone de saisie
                vue.getRecherchePrincipaleHasClickHandler().addClickHandler(new ClickHandler() {

                        public void onClick(ClickEvent event) {
                                HasText recherchePrincipale = (HasText) event.getSource();
                                if (recherchePrincipale.getText().equals("Recherche libre")) {
                                        recherchePrincipale.setText("");
                                }
                        }
                });

                // Gestion de l'affichage de la recherche Avancée
                vue.getLienRechercheAvancee().addClickHandler(new ClickHandler() {

                        public void onClick(ClickEvent event) {
                                HasVisibility rechercheAvancee = vue.getRechercheAvanceeHasVisibility();
                                rechercheAvancee.setVisible(!rechercheAvancee.isVisible());
                        }
                });

                // Gestion de l'évènement recherche sur le clic ou sur l'appui de la
                // touche Entrée
                vue.getBoutonRecherche().addClickHandler(new ClickHandler() {

                        public void onClick(ClickEvent event) {
                                chercherObservations(collecterFormulaire());
                                afficherObservations();
                        }
                });

                vue.getBoutonRechercheAvancee().addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                                chercherObservations(collecterFormulaire());
                                afficherObservations();
                        }
                });

                vue.getRecherchePrincipaleHasKeyPressHandlers().addKeyPressHandler(new KeyPressHandler() {

                        public void onKeyPress(KeyPressEvent event) {
                                if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
                                        chercherObservations(collecterFormulaire());
                                        afficherObservations();
                                }
                        }
                });
        }

        // Gestion de la recherche d'observations :
        public HashMap<String, String> collecterFormulaire() {

                HashMap<String, String> champsRecherche = new HashMap<String, String>();

                if (!vue.getRecherchePrincipaleHasText().getText().equals("")) {
                        champsRecherche.put("search", vue.getRecherchePrincipaleHasText().getText());
                }
                if (!vue.getDepartement().getText().equals("")) {
                        champsRecherche.put("dept", vue.getDepartement().getText());
                }
                if (!vue.getCommune().getText().equals("")) {
                        champsRecherche.put("com", vue.getCommune().getText());
                }
                if (!vue.getTaxon().getText().equals("")) {
                        champsRecherche.put("taxon", vue.getTaxon().getText());
                }
                if (!vue.getFamille().getText().equals("")) {
                        champsRecherche.put("fam", vue.getFamille().getText());
                }
                if (!vue.getGenre().getText().equals("")) {
                        champsRecherche.put("gen", vue.getGenre().getText());
                }
                if (!vue.getTag().getText().equals("")) {
                        champsRecherche.put("tag", vue.getTag().getText());
                }
                if (!vue.getMotCle().getText().equals("")) {
                        champsRecherche.put("motCle", vue.getMotCle().getText());
                }
                if (!vue.getAuteur().getText().equals("")) {
                        champsRecherche.put("auteur", vue.getAuteur().getText());
                }
                if (!vue.getDate().getText().equals("")) {
                        champsRecherche.put("date", vue.getDate().getText());
                }
                return champsRecherche;
        }

        public void chercherObservations(HashMap<String, String> champsRecherche) {

                vue.getRechercheAvanceeHasVisibility().setVisible(false);
                this.observations = MockDatasource.getInstance().getObservations(champsRecherche);
                vue.getRecherchePrecedente().setText(getChaineRecherche(champsRecherche));
        }

        public void afficherObservations() {

                vue.getZoneObservations().clear();
                for (Observation observation : observations) {
                        ObservationPresenteur presenteur = new ObservationPresenteur(new ObservationVue(), observation);
                        presenteur.go(vue.getZoneObservations());
                }
        }

        private String getChaineRecherche(HashMap<String, String> valeursRecherche) {
                String chaineRecherche = "";
                if (valeursRecherche != null) {
                        Iterator<String> itCles = valeursRecherche.keySet().iterator();
                        while (itCles.hasNext()) {
                                String cle = itCles.next();
                                String valeur = valeursRecherche.get(cle);
                                if (valeur != "") {
                                        chaineRecherche += cle + ":=" + valeur + " ";
                                }
                        }
                }
                return chaineRecherche;
        }
}