Subversion Repositories eFlore/Applications.del

Rev

Rev 161 | Rev 200 | 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.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.HasWidgets;
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 {

        private List<Observation> observations;

        public RechercheObservationsPresenteur() {
                super(new RechercheObservationsVue());
        }

        public void go(HasWidgets composite) {
                if (composite == null) {
                        composite = RootPanel.get();
                }
                composite.add(this.getVue());
                handleEvents();

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

        protected void handleEvents() {

                RechercheObservationsVue vue = (RechercheObservationsVue) getVue();

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

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

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

                        public void onClick(ClickEvent event) {
                                Panel rechercheAvancee = ((RechercheObservationsVue) getVue()).rechercheAvancee;
                                rechercheAvancee.setVisible(!rechercheAvancee.isVisible());
                        }
                });

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

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

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

                vue.recherchePrincipale.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() {
                RechercheObservationsVue vue = (RechercheObservationsVue) getVue();

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

                if (!vue.recherchePrincipale.getText().equals("")) {
                        champsRecherche.put("search", vue.recherchePrincipale.getText());
                }
                if (!vue.departement.getText().equals("")) {
                        champsRecherche.put("dept", vue.departement.getText());
                }
                if (!vue.commune.getText().equals("")) {
                        champsRecherche.put("com", vue.commune.getText());
                }
                if (!vue.taxon.getText().equals("")) {
                        champsRecherche.put("taxon", vue.taxon.getText());
                }
                if (!vue.famille.getText().equals("")) {
                        champsRecherche.put("fam", vue.famille.getText());
                }
                if (!vue.genre.getText().equals("")) {
                        champsRecherche.put("gen", vue.genre.getText());
                }
                if (!vue.tag.getText().equals("")) {
                        champsRecherche.put("tag", vue.tag.getText());
                }
                if (!vue.motCle.getText().equals("")) {
                        champsRecherche.put("motCle", vue.motCle.getText());
                }
                if (!vue.auteur.getText().equals("")) {
                        champsRecherche.put("auteur", vue.auteur.getText());
                }
                if (!vue.date.getText().equals("")) {
                        champsRecherche.put("date", vue.date.getText());
                }
                return champsRecherche;
        }

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

                RechercheObservationsVue vue = (RechercheObservationsVue) getVue();
                vue.rechercheAvancee.setVisible(false);
                this.observations = MockDatasource.getInstance().getObservations(champsRecherche);
                vue.recherchePrecedente.setText(getChaineRecherche(champsRecherche));
        }

        public void afficherObservations() {
                RechercheObservationsVue vue = (RechercheObservationsVue) getVue();

                for (Observation observation : observations) {
                        ObservationPresenteur presenteur = new ObservationPresenteur(observation);
                        presenteur.go(vue.zoneObservations);
                }
        }

        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;
        }
}