Subversion Repositories eFlore/Applications.del

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.tela_botanica.del.client.utils;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle.MultiWordSuggestion;
import com.google.gwt.user.client.ui.SuggestOracle;

/**
 * Un Oracle à suggestions qui retourne les résultats en respectant au mieux l'ordre de la
 * liste de suggestions fournies
 *  
 * @author mathias
 */
public class TBSuggestOracle extends SuggestOracle {

        // On devrait gérer des objets qui contiennent le nn plutôt que de se baser uniquement sur des Strings
        protected ArrayList<String> possibilites;

        public TBSuggestOracle() {
                this.possibilites = new ArrayList<String>();
        }

        public void clear() {
                this.possibilites.clear();
        }

        public void add(String suggestion) {
                this.possibilites.add(suggestion);
        }

        public void requestSuggestions(Request request, Callback callback) {
                String query = request.getQuery().toLowerCase();
                int limit = request.getLimit();
                List<MultiWordSuggestion> retenus = new ArrayList<MultiWordSuggestion>();

                for (String possibilite : this.possibilites) {
                        String possibiliteHtml = possibilite;
                        if (possibilite.toLowerCase().startsWith(query)) {
                                MultiWordSuggestion muWoSug = new MultiWordSuggestion(possibilite, possibiliteHtml);
                                retenus.add(muWoSug);
                        }
                }

                while (retenus.size() > limit) {
                        retenus.remove(retenus.size() - 1);
                }

                Response reponse = new Response(retenus);
                callback.onSuggestionsReady(request, reponse);
        }
}