261 |
aurelien |
1 |
package org.tela_botanica.del.client.composants.formulaires;
|
|
|
2 |
|
450 |
aurelien |
3 |
import com.google.gwt.event.dom.client.HasKeyPressHandlers;
|
261 |
aurelien |
4 |
import com.google.gwt.event.dom.client.KeyPressEvent;
|
|
|
5 |
import com.google.gwt.event.dom.client.KeyPressHandler;
|
457 |
aurelien |
6 |
import com.google.gwt.event.logical.shared.HasSelectionHandlers;
|
|
|
7 |
import com.google.gwt.event.logical.shared.SelectionEvent;
|
|
|
8 |
import com.google.gwt.event.logical.shared.SelectionHandler;
|
261 |
aurelien |
9 |
import com.google.gwt.http.client.Request;
|
|
|
10 |
import com.google.gwt.http.client.RequestBuilder;
|
|
|
11 |
import com.google.gwt.http.client.RequestCallback;
|
|
|
12 |
import com.google.gwt.http.client.RequestException;
|
|
|
13 |
import com.google.gwt.http.client.Response;
|
|
|
14 |
import com.google.gwt.http.client.URL;
|
|
|
15 |
import com.google.gwt.user.client.Timer;
|
|
|
16 |
import com.google.gwt.user.client.ui.HasWidgets;
|
450 |
aurelien |
17 |
import com.google.gwt.user.client.ui.IsWidget;
|
261 |
aurelien |
18 |
import com.google.gwt.user.client.ui.SuggestOracle;
|
457 |
aurelien |
19 |
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
|
261 |
aurelien |
20 |
|
268 |
aurelien |
21 |
public abstract class AutoCompletionComboBoxPresenteur {
|
261 |
aurelien |
22 |
|
450 |
aurelien |
23 |
public interface Vue extends IsWidget {
|
|
|
24 |
public void ajouterVue(HasWidgets composite);
|
|
|
25 |
public HasKeyPressHandlers getSuggestBoxHasKeyPressHandlers();
|
|
|
26 |
public String getValeur();
|
|
|
27 |
public void remplirListe(String[] suggestions);
|
|
|
28 |
public void afficherSuggestions(SuggestOracle.Request request,SuggestOracle.Callback response);
|
|
|
29 |
public void afficherListeDeroulante();
|
|
|
30 |
public void nettoyer();
|
|
|
31 |
public void setValeur(String valeur);
|
457 |
aurelien |
32 |
HasSelectionHandlers<Suggestion> getSuggestBoxHasSelectionChangeHandlers();
|
450 |
aurelien |
33 |
}
|
|
|
34 |
private Vue autoCompletionComboBoxVue;
|
|
|
35 |
|
261 |
aurelien |
36 |
SuggestOracle.Request requeteOracle;
|
|
|
37 |
SuggestOracle.Callback callBackOracle;
|
|
|
38 |
|
|
|
39 |
Timer suggestionTimer = null;
|
|
|
40 |
int tempsAvantRequete = 500;
|
268 |
aurelien |
41 |
|
|
|
42 |
String urlRequete = "";
|
261 |
aurelien |
43 |
|
450 |
aurelien |
44 |
public AutoCompletionComboBoxPresenteur(Vue vue, String urlRequete) {
|
|
|
45 |
this.autoCompletionComboBoxVue = vue;
|
268 |
aurelien |
46 |
this.urlRequete = urlRequete;
|
450 |
aurelien |
47 |
gererEvenements();
|
261 |
aurelien |
48 |
}
|
|
|
49 |
|
|
|
50 |
public void go(HasWidgets composite) {
|
450 |
aurelien |
51 |
autoCompletionComboBoxVue.ajouterVue(composite);
|
261 |
aurelien |
52 |
}
|
|
|
53 |
|
|
|
54 |
public void gererEvenements() {
|
450 |
aurelien |
55 |
autoCompletionComboBoxVue.getSuggestBoxHasKeyPressHandlers().addKeyPressHandler(new KeyPressHandler() {
|
|
|
56 |
public void onKeyPress(KeyPressEvent event) {
|
261 |
aurelien |
57 |
if(event.getUnicodeCharCode() != 0) {
|
450 |
aurelien |
58 |
gererTimer();
|
261 |
aurelien |
59 |
}
|
|
|
60 |
}
|
|
|
61 |
});
|
|
|
62 |
|
|
|
63 |
requeteOracle = new SuggestOracle.Request();
|
|
|
64 |
callBackOracle = new SuggestOracle.Callback() {
|
|
|
65 |
|
450 |
aurelien |
66 |
public void onSuggestionsReady(SuggestOracle.Request request, SuggestOracle.Response response) {
|
|
|
67 |
autoCompletionComboBoxVue.afficherListeDeroulante();
|
261 |
aurelien |
68 |
}
|
|
|
69 |
};
|
457 |
aurelien |
70 |
|
|
|
71 |
autoCompletionComboBoxVue.getSuggestBoxHasSelectionChangeHandlers().addSelectionHandler(new SelectionHandler<SuggestOracle.Suggestion>(){
|
|
|
72 |
@Override
|
|
|
73 |
public void onSelection(SelectionEvent<Suggestion> event) {
|
|
|
74 |
surSelectionSuggestion(event.getSelectedItem().getReplacementString());
|
|
|
75 |
}
|
|
|
76 |
});
|
261 |
aurelien |
77 |
}
|
|
|
78 |
|
450 |
aurelien |
79 |
public void gererTimer() {
|
|
|
80 |
if(suggestionTimer == null) {
|
|
|
81 |
suggestionTimer = new Timer() {
|
|
|
82 |
public void run() {
|
|
|
83 |
requeteSuggestions();
|
|
|
84 |
}
|
|
|
85 |
};
|
|
|
86 |
suggestionTimer.schedule(tempsAvantRequete);
|
|
|
87 |
} else {
|
|
|
88 |
suggestionTimer.schedule(tempsAvantRequete);
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
|
261 |
aurelien |
92 |
public void setTempsAvantRequete(int temps) {
|
|
|
93 |
tempsAvantRequete = temps;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
public int getTempsAvantRequete(int temps) {
|
|
|
97 |
return tempsAvantRequete;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
public String getValeur() {
|
450 |
aurelien |
101 |
return autoCompletionComboBoxVue.getValeur();
|
261 |
aurelien |
102 |
}
|
|
|
103 |
|
415 |
gduche |
104 |
public void setValeur(String taxon) {
|
|
|
105 |
autoCompletionComboBoxVue.setValeur(taxon);
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
public void nettoyer() {
|
|
|
109 |
autoCompletionComboBoxVue.nettoyer();
|
|
|
110 |
}
|
|
|
111 |
|
450 |
aurelien |
112 |
public void requeteSuggestions() {
|
261 |
aurelien |
113 |
|
450 |
aurelien |
114 |
final String requete = autoCompletionComboBoxVue.getValeur();
|
328 |
aurelien |
115 |
String requeteAjax = effectuerPreTraitementChaineRequete(requete);
|
375 |
aurelien |
116 |
String urlProxy = URL.encode(urlRequete+requeteAjax);
|
261 |
aurelien |
117 |
|
|
|
118 |
RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, urlProxy) ;
|
450 |
aurelien |
119 |
try {
|
261 |
aurelien |
120 |
rb.sendRequest(null, new RequestCallback() {
|
|
|
121 |
public void onError(final Request request, final Throwable exception) {
|
|
|
122 |
// TODO Auto-generated method stub
|
|
|
123 |
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
public void onResponseReceived(final Request request,
|
|
|
127 |
final Response response) {
|
450 |
aurelien |
128 |
surReponseRequete(requete, response);
|
261 |
aurelien |
129 |
}
|
|
|
130 |
}) ;
|
|
|
131 |
|
|
|
132 |
} catch (RequestException e) {
|
|
|
133 |
// TODO Auto-generated catch block
|
|
|
134 |
e.printStackTrace();
|
|
|
135 |
}
|
|
|
136 |
}
|
268 |
aurelien |
137 |
|
450 |
aurelien |
138 |
public void surReponseRequete(String requete, Response response) {
|
|
|
139 |
String[] valeurs = parserResultatRequete(response);
|
|
|
140 |
|
|
|
141 |
autoCompletionComboBoxVue.remplirListe(valeurs);
|
|
|
142 |
requeteOracle.setQuery(requete);
|
|
|
143 |
autoCompletionComboBoxVue.afficherSuggestions(requeteOracle, callBackOracle);
|
|
|
144 |
}
|
|
|
145 |
|
457 |
aurelien |
146 |
protected void surSelectionSuggestion(String suggestion) {
|
|
|
147 |
return;
|
|
|
148 |
}
|
|
|
149 |
|
328 |
aurelien |
150 |
/**
|
|
|
151 |
* Permet d'intervenir sur la chaine tapée par l'utilisateur avant que celle-ci ne soit passée
|
|
|
152 |
* à la requête asynchrone pour pouvoir la formater (pour mettre en minuscule, séparer des termes)
|
|
|
153 |
* par défaut renvoie la chaine telle quelle, à surcharger au besoin
|
|
|
154 |
* @param String requete chaine de recherche contenant ce que l'utilisateur à tapé
|
|
|
155 |
* @return String une chaine transformée
|
|
|
156 |
*/
|
|
|
157 |
protected String effectuerPreTraitementChaineRequete(String requete) {
|
|
|
158 |
return requete;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* Parse les résultats de la requete afin de pouvoir les utiliser à implémenter par le
|
|
|
163 |
* composants qui crée la combobox selon le contexte et le web service appelé.
|
|
|
164 |
* @param Response la réponse à la requête asynchrone
|
|
|
165 |
* @return String[] le tableau des suggestions qui sera affiché dans la combobox
|
|
|
166 |
*/
|
268 |
aurelien |
167 |
protected abstract String[] parserResultatRequete(Response response);
|
564 |
benjamin |
168 |
|
|
|
169 |
public Vue getAutoCompletionComboBoxVue() {
|
|
|
170 |
return autoCompletionComboBoxVue;
|
|
|
171 |
}
|
261 |
aurelien |
172 |
}
|