Subversion Repositories eFlore/Applications.del

Rev

Rev 407 | Rev 420 | 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.json.client.JSONArray;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
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 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();
        }

        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.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.setAuteur(vue.getAuteur().getText());
                informationRecherche.setCommune(vue.getCommune().getText());
                informationRecherche.setDate(vue.getDate().getText());
                informationRecherche.setDepartement(vue.getDepartement().getText());
                informationRecherche.setFamille(vue.getFamille().getText());
                informationRecherche.setGenre(vue.getGenre().getText());
                informationRecherche.setMotClef(vue.getMotCle().getText());

                if (!vue.getRecherchePrincipale().getText().equals(vue.getLabelRecherche())) {
                        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) {
                        vue.chargerValeursRecherchePrecedente(informationsRecherche);
                }
                lancerRecherche();
        }

        public abstract void lancerRecherche();

        public boolean isPourRechercheImages() {
                return pourRechercheImages;
        }

        public boolean isPourRechercheObservations() {
                return pourRechercheObservations;
        }
}