From 1a34a24df0c38220e2444d9253a69df30259dc36 Mon Sep 17 00:00:00 2001 From: bjorn Date: Tue, 28 Oct 2008 12:37:52 +0000 Subject: [PATCH] GWT completion: only return request.getLimit() results to the SuggestBox. Re-request completions from the server if there is only one completion. --- .../cs/gf/gwt/client/CompletionOracle.java | 19 +++++----- .../se/chalmers/cs/gf/gwt/client/SubList.java | 37 +++++++++++++++++++ 2 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 src/server/gwt/src/se/chalmers/cs/gf/gwt/client/SubList.java diff --git a/src/server/gwt/src/se/chalmers/cs/gf/gwt/client/CompletionOracle.java b/src/server/gwt/src/se/chalmers/cs/gf/gwt/client/CompletionOracle.java index c95c85604..9a9b5080d 100644 --- a/src/server/gwt/src/se/chalmers/cs/gf/gwt/client/CompletionOracle.java +++ b/src/server/gwt/src/se/chalmers/cs/gf/gwt/client/CompletionOracle.java @@ -9,7 +9,7 @@ import com.google.gwt.user.client.ui.SuggestOracle; public class CompletionOracle extends SuggestOracle { private static final int LIMIT_SCALE_FACTOR = 10; - + private PGF pgf; private ErrorHandler errorHandler; @@ -17,9 +17,9 @@ public class CompletionOracle extends SuggestOracle { private List inputLangs = null; private JSONRequest jsonRequest = null; - + private String oldQuery = null; - + private List oldSuggestions = Collections.emptyList(); @@ -78,7 +78,7 @@ public class CompletionOracle extends SuggestOracle { retrieveSuggestions(request, callback); } } - + /** Filters old suggestions and checks if we still have enough suggestions. */ private List filterOldSuggestions(SuggestOracle.Request request) { List suggestions = new ArrayList(); @@ -87,16 +87,14 @@ public class CompletionOracle extends SuggestOracle { if (c.getReplacementString().startsWith(request.getQuery())) { suggestions.add(c); } - } - // Use filtered old suggestions, if there are enough of them, - // or if the old ones were already fewer than the limit. - if (suggestions.size() > 0 && (suggestions.size() >= request.getLimit() || oldSuggestions.size() < request.getLimit())) { + } + if (suggestions.size() > 1 && (suggestions.size() >= request.getLimit() || oldSuggestions.size() < request.getLimit())) { return suggestions; } } return null; } - + private void retrieveSuggestions(final SuggestOracle.Request request, final SuggestOracle.Callback callback) { // hack: first report no completions, to hide suggestions until we get the new completions callback.onSuggestionsReady(request, new SuggestOracle.Response(Collections.emptyList())); @@ -117,10 +115,11 @@ public class CompletionOracle extends SuggestOracle { }); } - + private void suggestionsReady(SuggestOracle.Request request, SuggestOracle.Callback callback, List suggestions) { this.oldQuery = request.getQuery(); this.oldSuggestions = suggestions; + suggestions = suggestions.size() <= request.getLimit() ? suggestions : SubList.makeSubList(suggestions, 0, request.getLimit()); callback.onSuggestionsReady(request, new SuggestOracle.Response(suggestions)); } diff --git a/src/server/gwt/src/se/chalmers/cs/gf/gwt/client/SubList.java b/src/server/gwt/src/se/chalmers/cs/gf/gwt/client/SubList.java new file mode 100644 index 000000000..64f72706f --- /dev/null +++ b/src/server/gwt/src/se/chalmers/cs/gf/gwt/client/SubList.java @@ -0,0 +1,37 @@ +package se.chalmers.cs.gf.gwt.client; + +import java.util.AbstractList; +import java.util.List; + +/** Work-around for missing List.subList() method in GWT JRE API emulation. */ +public class SubList extends AbstractList { + + private List list; + + private int fromIndex; + + private int toIndex; + + public SubList(List list, int fromIndex, int toIndex) { + this.list = list; + this.fromIndex = fromIndex; + this.toIndex = toIndex; + if (fromIndex < 0 || toIndex > list.size()) + throw new IndexOutOfBoundsException("Endpoint index value out of range"); + if (fromIndex > toIndex) + throw new IllegalArgumentException("Endpoint indices out of order"); + } + + public T get(int index) { + return list.get(fromIndex + index); + } + + public int size() { + return toIndex - fromIndex; + } + + public static SubList makeSubList(List list, int fromIndex, int toIndex) { + return new SubList(list, fromIndex, toIndex); + } + +}