Subversion Repositories eFlore/Applications.del

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
261 aurelien 1
package org.tela_botanica.del.client.composants.formulaires;
2
 
3
import com.google.gwt.event.dom.client.KeyPressEvent;
4
import com.google.gwt.event.dom.client.KeyPressHandler;
5
import com.google.gwt.http.client.Request;
6
import com.google.gwt.http.client.RequestBuilder;
7
import com.google.gwt.http.client.RequestCallback;
8
import com.google.gwt.http.client.RequestException;
9
import com.google.gwt.http.client.Response;
10
import com.google.gwt.http.client.URL;
11
import com.google.gwt.json.client.JSONArray;
12
import com.google.gwt.json.client.JSONParser;
13
import com.google.gwt.json.client.JSONValue;
14
import com.google.gwt.user.client.Timer;
15
import com.google.gwt.user.client.ui.HasWidgets;
16
import com.google.gwt.user.client.ui.SuggestOracle;
17
 
268 aurelien 18
public abstract class AutoCompletionComboBoxPresenteur {
19
 
20
	// Complétion de taxons :
21
	// http://www.tela-botanica.org/eflore/cel2/jrest/NameSearch/
261 aurelien 22
 
23
	private AutoCompletionComboBoxVue autoCompletionComboBoxVue = new AutoCompletionComboBoxVue();
24
 
25
	SuggestOracle.Request requeteOracle;
26
    SuggestOracle.Callback callBackOracle;
27
 
28
	Timer suggestionTimer = null;
29
    int tempsAvantRequete = 500;
268 aurelien 30
 
31
    String urlRequete = "";
261 aurelien 32
 
268 aurelien 33
	public AutoCompletionComboBoxPresenteur(String urlRequete) {
34
		this.urlRequete = urlRequete;
261 aurelien 35
	}
36
 
37
	public void go(HasWidgets composite) {
38
		composite.add(autoCompletionComboBoxVue);
39
		gererEvenements();
40
	}
41
 
42
	public void gererEvenements() {
43
		autoCompletionComboBoxVue.getSuggestBox().addKeyPressHandler(new KeyPressHandler() {
44
			public void onKeyPress(KeyPressEvent event) {
45
 
46
				if(event.getUnicodeCharCode() != 0) {
47
 
48
					if(suggestionTimer == null) {
49
						suggestionTimer = new Timer() {
50
							public void run() {
51
								requeteSuggestions(autoCompletionComboBoxVue.getSuggestBox().getValue());
52
							}
53
						};
54
						suggestionTimer.schedule(tempsAvantRequete);
55
					} else {
56
						suggestionTimer.schedule(tempsAvantRequete);
57
					}
58
				}
59
			}
60
 
61
		});
62
 
63
		requeteOracle = new SuggestOracle.Request();
64
		callBackOracle = new SuggestOracle.Callback() {
65
 
66
			public void onSuggestionsReady(
67
					com.google.gwt.user.client.ui.SuggestOracle.Request request,
68
					com.google.gwt.user.client.ui.SuggestOracle.Response response) {
69
				autoCompletionComboBoxVue.getSuggestBox().showSuggestionList();
70
			}
71
	    };
72
	}
73
 
74
	public void setTempsAvantRequete(int temps) {
75
		tempsAvantRequete = temps;
76
	}
77
 
78
	public int getTempsAvantRequete(int temps) {
79
		return tempsAvantRequete;
80
	}
81
 
82
	public String getValeur() {
83
		return autoCompletionComboBoxVue.getSuggestBox().getValue();
84
	}
85
 
86
	private void requeteSuggestions(final String requete) {
87
 
88
		String requeteAjax = requete;
89
		String[] parties = requete.split(" ", 2);
90
 
91
		if(parties.length == 2) {
92
			if(parties[1].trim().isEmpty()) {
93
				parties[1] = "*";
94
			}
95
 
96
			requeteAjax = parties[0]+"/"+parties[1];
97
		}
98
 
268 aurelien 99
		String urlProxy = "proxy.php?url="+URL.encode(urlRequete+requeteAjax);
261 aurelien 100
 
101
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, urlProxy) ;
102
 
103
		try {
104
 
105
			rb.sendRequest(null, new RequestCallback() {
106
 
107
				public void onError(final Request request, final Throwable exception) {
108
					// TODO Auto-generated method stub
109
 
110
				}
111
 
112
				public void onResponseReceived(final Request request,
113
						final Response response) {
114
 
268 aurelien 115
					String[] valeurs = parserResultatRequete(response);
261 aurelien 116
 
268 aurelien 117
					autoCompletionComboBoxVue.remplirListe(valeurs);
118
					requeteOracle.setQuery(requete);
119
					autoCompletionComboBoxVue.afficherSuggestions(requeteOracle, callBackOracle);
261 aurelien 120
				}
121
			}) ;
122
 
123
		} catch (RequestException e) {
124
			// TODO Auto-generated catch block
125
			e.printStackTrace();
126
		}
127
	}
268 aurelien 128
 
129
	protected abstract String[] parserResultatRequete(Response response);
261 aurelien 130
}