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 class AutoCompletionComboBoxPresenteur {
private AutoCompletionComboBoxVue autoCompletionComboBoxVue = new AutoCompletionComboBoxVue();
SuggestOracle.Request requeteOracle;
SuggestOracle.Callback callBackOracle;
Timer suggestionTimer = null;
int tempsAvantRequete = 500;
public AutoCompletionComboBoxPresenteur() {
}
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 altUrl = "http://www.tela-botanica.org/eflore/cel2/jrest/NameSearch/";
String urlProxy = "proxy.php?url="+URL.encode(altUrl+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) {
final JSONValue responseValue = JSONParser.parseStrict(response.getText());
JSONArray noms;
if ((noms=responseValue.isArray()) != null) {
final int taillemax = noms.size();
String[] valeurs = new String[taillemax];
for (int i = 0; i < taillemax; i++) {
valeurs[i] = (noms.get(i).isArray().get(0).isString().stringValue());
}
autoCompletionComboBoxVue.remplirListe(valeurs);
requeteOracle.setQuery(requete);
autoCompletionComboBoxVue.afficherSuggestions(requeteOracle, callBackOracle);
}
}
}) ;
} catch (RequestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}