diff options
| author | bjorn <bjorn@bringert.net> | 2008-10-28 12:37:52 +0000 |
|---|---|---|
| committer | bjorn <bjorn@bringert.net> | 2008-10-28 12:37:52 +0000 |
| commit | 1a34a24df0c38220e2444d9253a69df30259dc36 (patch) | |
| tree | 2c6622e1e2c26a500a12e5cc5f9425425b14fc0c /src/server/gwt | |
| parent | 50b2b453f3028ba5547f6bc7b25e03d1f5f10e0b (diff) | |
GWT completion: only return request.getLimit() results to the SuggestBox. Re-request completions from the server if there is only one completion.
Diffstat (limited to 'src/server/gwt')
| -rw-r--r-- | src/server/gwt/src/se/chalmers/cs/gf/gwt/client/CompletionOracle.java | 19 | ||||
| -rw-r--r-- | src/server/gwt/src/se/chalmers/cs/gf/gwt/client/SubList.java | 37 |
2 files changed, 46 insertions, 10 deletions
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<String> inputLangs = null; private JSONRequest jsonRequest = null; - + private String oldQuery = null; - + private List<CompletionSuggestion> 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<CompletionSuggestion> filterOldSuggestions(SuggestOracle.Request request) { List<CompletionSuggestion> suggestions = new ArrayList<CompletionSuggestion>(); @@ -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.<CompletionSuggestion>emptyList())); @@ -117,10 +115,11 @@ public class CompletionOracle extends SuggestOracle { }); } - + private void suggestionsReady(SuggestOracle.Request request, SuggestOracle.Callback callback, List<CompletionSuggestion> 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<T> extends AbstractList<T> { + + private List<T> list; + + private int fromIndex; + + private int toIndex; + + public SubList(List<T> 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 <T> SubList<T> makeSubList(List<T> list, int fromIndex, int toIndex) { + return new SubList<T>(list, fromIndex, toIndex); + } + +} |
