Subversion Repositories eFlore/Applications.del

Rev

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

package org.tela_botanica.del.client.composants.moteurrecherche;

import org.tela_botanica.del.client.cache.CacheClient;
import org.tela_botanica.del.client.composants.formulaires.AutoCompletionComboBoxPresenteur;
import org.tela_botanica.del.client.composants.presenteur.Presenteur;
import org.tela_botanica.del.client.modeles.InformationsRecherche;
import org.tela_botanica.del.client.services.UtilitairesAutoCompletionService;

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.http.client.Response;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.IsWidget;

public abstract class MoteurRecherchePresenteur extends Presenteur {

        public abstract interface Vue extends IsWidget {

                public abstract HasClickHandlers getLienRechercheAvancee();

                public abstract void basculerAffichageZoneCache();

                public abstract HasClickHandlers getBoutonRechercheSimple();

                public abstract HasClickHandlers getBoutonFermer();

                public abstract HasClickHandlers getBoutonVider();

                public abstract HasClickHandlers getBoutonRechercheAvancee();

                public abstract HasKeyPressHandlers getChampSaisie();

                public abstract HasClickHandlers getChampSaisieCliquable();

                public abstract String getValeurRechercheSimple();

                public void setValeurRechercheSimple(String valeurRecherche);

                public String getLabelRecherche();

                public HasText getRecherchePrincipale();

                public HasText getDepartement();

                public HasText getCommune();

                public HasWidgets getTaxon();

                public HasText getFamille();

                public HasText getGenre();

                public HasText getTag();

                public HasText getMotCle();

                public HasText getAuteur();

                public HasText getDate();

                public void chargerValeursRecherchePrecedente(InformationsRecherche informationsRecherche);

                public void focusSaisie();

                public void nettoyer();
        }

        private final Vue vue;
        private final boolean pourRechercheImages, pourRechercheObservations;

        private AutoCompletionComboBoxPresenteur autoCompletionNomTaxonsPresenteur;

        public MoteurRecherchePresenteur(Vue vue, boolean pourRechercheImages, boolean pourRechercheObservations) {
                this.vue = vue;
                this.pourRechercheImages = pourRechercheImages;
                this.pourRechercheObservations = pourRechercheObservations;

                autoCompletionNomTaxonsPresenteur = new AutoCompletionComboBoxPresenteur(UtilitairesAutoCompletionService.urlServiceCompletionNomLocale) {

                        protected String effectuerPreTraitementChaineRequete(String requete) {
                                return UtilitairesAutoCompletionService.effectuerPreTraitementChaineRequeteGenreEspeceSlash(requete);
                                // A décommenter lors de l'utilisation des web services eflore
                                // return
                                // RetourAutoCompletionService.effectuerPreTraitementChaineRequeteGenreEspeceEflore(requete);
                        }

                        @Override
                        protected String[] parserResultatRequete(Response response) {
                                return UtilitairesAutoCompletionService.parserRetourSimple(response);
                                // A décommenter lors de l'utilisation des web services eflore
                                // return RetourAutoCompletionService.parserRetourOss(response);
                        }
                };
                gererEvenements();
        }

        @Override
        public void go(HasWidgets composite) {
                afficherRequeteEtLancerRecherche();
                autoCompletionNomTaxonsPresenteur.go(vue.getTaxon());
                composite.add(vue.asWidget());
                vue.focusSaisie();
        }

        @Override
        protected void gererEvenements() {
                vue.getLienRechercheAvancee().addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                                vue.basculerAffichageZoneCache();
                        }
                });

                vue.getBoutonRechercheSimple().addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                                collecterInfosRecherche();
                                afficherRequeteEtLancerRecherche();
                        }
                });

                vue.getBoutonFermer().addClickHandler(new ClickHandler() {

                        @Override
                        public void onClick(ClickEvent event) {
                                vue.basculerAffichageZoneCache();
                        }
                });

                vue.getBoutonVider().addClickHandler(new ClickHandler() {

                        @Override
                        public void onClick(ClickEvent event) {

                                autoCompletionNomTaxonsPresenteur.nettoyer();
                                vue.nettoyer();

                                InformationsRecherche infosRecherche = new InformationsRecherche();
                                if (isPourRechercheImages()) {
                                        CacheClient.getInstance().setInformationsRechercheImage(infosRecherche);
                                } else if (isPourRechercheObservations()) {
                                        CacheClient.getInstance().setInformationsRechercheObservation(infosRecherche);
                                }
                        }
                });

                vue.getBoutonRechercheAvancee().addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                                collecterInfosRecherche();
                                vue.basculerAffichageZoneCache();
                                afficherRequeteEtLancerRecherche();
                        }
                });

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

                        public void onKeyPress(KeyPressEvent event) {
                                if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
                                        collecterInfosRecherche();
                                        afficherRequeteEtLancerRecherche();
                                }
                        }
                });

                vue.getChampSaisieCliquable().addClickHandler(new ClickHandler() {

                        @Override
                        public void onClick(ClickEvent event) {
                                if (vue.getValeurRechercheSimple().equals(vue.getLabelRecherche())) {
                                        vue.setValeurRechercheSimple("");
                                }
                        }
                });
        }

        private void collecterInfosRecherche() {
                InformationsRecherche informationRecherche = new InformationsRecherche();
                informationRecherche.setTaxon(autoCompletionNomTaxonsPresenteur.getValeur());
                informationRecherche.setDepartement(vue.getDepartement().getText());
                informationRecherche.setCommune(vue.getCommune().getText());
                informationRecherche.setFamille(vue.getFamille().getText());
                informationRecherche.setGenre(vue.getGenre().getText());
                informationRecherche.setTag(vue.getTag().getText());
                informationRecherche.setMotClef(vue.getMotCle().getText());
                informationRecherche.setAuteur(vue.getAuteur().getText());
                informationRecherche.setDate(vue.getDate().getText());
                informationRecherche.setRechercheLibre(vue.getRecherchePrincipale().getText());

                if (isPourRechercheImages()) {
                        CacheClient.getInstance().setInformationsRechercheImage(informationRecherche);
                } else if (isPourRechercheObservations()) {
                        CacheClient.getInstance().setInformationsRechercheObservation(informationRecherche);
                }
        }

        private InformationsRecherche getInformationsRechercheEnCache() {
                if (isPourRechercheImages()) {
                        return CacheClient.getInstance().getInformationsRechercheImage();
                } else if (isPourRechercheObservations()) {
                        return CacheClient.getInstance().getInformationsRechercheObservation();
                }
                return null;
        }

        public void afficherRequeteEtLancerRecherche() {
                InformationsRecherche informationsRecherche = getInformationsRechercheEnCache();
                if (informationsRecherche != null) {
                        autoCompletionNomTaxonsPresenteur.setValeur(informationsRecherche.getTaxon());
                        vue.chargerValeursRecherchePrecedente(informationsRecherche);
                }
                lancerRecherche();
        }

        public abstract void lancerRecherche();

        public boolean isPourRechercheImages() {
                return pourRechercheImages;
        }

        public boolean isPourRechercheObservations() {
                return pourRechercheObservations;
        }
}