Subversion Repositories eFlore/Applications.del

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1782 mathias 1
package org.tela_botanica.del.client.utils;
2
 
3
import java.util.ArrayList;
4
import java.util.List;
5
 
6
import com.google.gwt.user.client.Window;
7
import com.google.gwt.user.client.ui.MultiWordSuggestOracle.MultiWordSuggestion;
8
import com.google.gwt.user.client.ui.SuggestOracle;
9
 
10
/**
11
 * Un Oracle à suggestions qui retourne les résultats en respectant au mieux l'ordre de la
12
 * liste de suggestions fournies
13
 *
14
 * @author mathias
15
 */
16
public class TBSuggestOracle extends SuggestOracle {
17
 
18
	// On devrait gérer des objets qui contiennent le nn plutôt que de se baser uniquement sur des Strings
19
	protected ArrayList<String> possibilites;
20
 
21
	public TBSuggestOracle() {
22
		this.possibilites = new ArrayList<String>();
23
	}
24
 
25
	public void clear() {
26
		this.possibilites.clear();
27
	}
28
 
29
	public void add(String suggestion) {
30
		this.possibilites.add(suggestion);
31
	}
32
 
33
	public void requestSuggestions(Request request, Callback callback) {
34
		String query = request.getQuery().toLowerCase();
35
		int limit = request.getLimit();
36
		List<MultiWordSuggestion> retenus = new ArrayList<MultiWordSuggestion>();
37
 
38
		for (String possibilite : this.possibilites) {
39
			String possibiliteHtml = possibilite;
40
			if (possibilite.toLowerCase().startsWith(query)) {
41
				MultiWordSuggestion muWoSug = new MultiWordSuggestion(possibilite, possibiliteHtml);
42
				retenus.add(muWoSug);
43
			}
44
		}
45
 
46
		while (retenus.size() > limit) {
47
			retenus.remove(retenus.size() - 1);
48
		}
49
 
50
		Response reponse = new Response(retenus);
51
		callback.onSuggestionsReady(request, reponse);
52
	}
53
}