Subversion Repositories eFlore/Applications.del

Rev

Rev 261 | Blame | Last modification | View Log | RSS feed

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

import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
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.Timer;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.SuggestOracle;

public abstract class AutoCompletionComboBoxPresenteur {
        
        // Complétion de taxons :
        // http://www.tela-botanica.org/eflore/cel2/jrest/NameSearch/
                
        private AutoCompletionComboBoxVue autoCompletionComboBoxVue = new AutoCompletionComboBoxVue();
        
        SuggestOracle.Request requeteOracle;
    SuggestOracle.Callback callBackOracle;
    
        Timer suggestionTimer = null;
    int tempsAvantRequete = 500;
    
    String urlRequete = "";
        
        public AutoCompletionComboBoxPresenteur(String urlRequete) {
                this.urlRequete = urlRequete;
        }
        
        public void go(HasWidgets composite) {
                composite.add(autoCompletionComboBoxVue);
                gererEvenements();
        }
        
        public void gererEvenements() {
                autoCompletionComboBoxVue.getSuggestBox().addKeyPressHandler(new KeyPressHandler() {
                        public void onKeyPress(KeyPressEvent event) {
                                
                                if(event.getUnicodeCharCode() != 0) {
                                
                                        if(suggestionTimer == null) {
                                                suggestionTimer = new Timer() {
                                                        public void run() {
                                                                requeteSuggestions(autoCompletionComboBoxVue.getSuggestBox().getValue());
                                                        }       
                                                };                                      
                                                suggestionTimer.schedule(tempsAvantRequete);
                                        } else {
                                                suggestionTimer.schedule(tempsAvantRequete);
                                        }
                                }
                        }
                        
                });
                
                requeteOracle = new SuggestOracle.Request();
                callBackOracle = new SuggestOracle.Callback() {

                        public void onSuggestionsReady(
                                        com.google.gwt.user.client.ui.SuggestOracle.Request request,
                                        com.google.gwt.user.client.ui.SuggestOracle.Response response) {
                                autoCompletionComboBoxVue.getSuggestBox().showSuggestionList(); 
                        }
            };
        }
        
        public void setTempsAvantRequete(int temps) {
                tempsAvantRequete = temps;
        }
        
        public int getTempsAvantRequete(int temps) {
                return tempsAvantRequete;
        }
        
        public String getValeur() {
                return autoCompletionComboBoxVue.getSuggestBox().getValue();
        }
        
        private void requeteSuggestions(final String requete) {
                
                String requeteAjax = requete;
                String[] parties = requete.split(" ", 2);
                
                if(parties.length == 2) {
                        if(parties[1].trim().isEmpty()) {
                                parties[1] = "*";
                        }
                        
                        requeteAjax = parties[0]+"/"+parties[1];
                }
                
                String urlProxy = "proxy.php?url="+URL.encode(urlRequete+requeteAjax);
                
                RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, urlProxy) ;
                
                try {
                        
                        rb.sendRequest(null, new RequestCallback() {

                                public void onError(final Request request, final Throwable exception) {
                                        // TODO Auto-generated method stub
                                        
                                }

                                public void onResponseReceived(final Request request,
                                                final Response response) {
                                        
                                        String[] valeurs = parserResultatRequete(response);
                                            
                                        autoCompletionComboBoxVue.remplirListe(valeurs);
                                        requeteOracle.setQuery(requete);
                                        autoCompletionComboBoxVue.afficherSuggestions(requeteOracle, callBackOracle);   
                                }
                        }) ;

                } catch (RequestException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
        
        protected abstract String[] parserResultatRequete(Response response);
}