mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 03:32:51 -06:00
GWT completion: only return request.getLimit() results to the SuggestBox. Re-request completions from the server if there is only one completion.
This commit is contained in:
@@ -9,7 +9,7 @@ import com.google.gwt.user.client.ui.SuggestOracle;
|
|||||||
public class CompletionOracle extends SuggestOracle {
|
public class CompletionOracle extends SuggestOracle {
|
||||||
|
|
||||||
private static final int LIMIT_SCALE_FACTOR = 10;
|
private static final int LIMIT_SCALE_FACTOR = 10;
|
||||||
|
|
||||||
private PGF pgf;
|
private PGF pgf;
|
||||||
|
|
||||||
private ErrorHandler errorHandler;
|
private ErrorHandler errorHandler;
|
||||||
@@ -17,9 +17,9 @@ public class CompletionOracle extends SuggestOracle {
|
|||||||
private List<String> inputLangs = null;
|
private List<String> inputLangs = null;
|
||||||
|
|
||||||
private JSONRequest jsonRequest = null;
|
private JSONRequest jsonRequest = null;
|
||||||
|
|
||||||
private String oldQuery = null;
|
private String oldQuery = null;
|
||||||
|
|
||||||
private List<CompletionSuggestion> oldSuggestions = Collections.emptyList();
|
private List<CompletionSuggestion> oldSuggestions = Collections.emptyList();
|
||||||
|
|
||||||
|
|
||||||
@@ -78,7 +78,7 @@ public class CompletionOracle extends SuggestOracle {
|
|||||||
retrieveSuggestions(request, callback);
|
retrieveSuggestions(request, callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Filters old suggestions and checks if we still have enough suggestions. */
|
/** Filters old suggestions and checks if we still have enough suggestions. */
|
||||||
private List<CompletionSuggestion> filterOldSuggestions(SuggestOracle.Request request) {
|
private List<CompletionSuggestion> filterOldSuggestions(SuggestOracle.Request request) {
|
||||||
List<CompletionSuggestion> suggestions = new ArrayList<CompletionSuggestion>();
|
List<CompletionSuggestion> suggestions = new ArrayList<CompletionSuggestion>();
|
||||||
@@ -87,16 +87,14 @@ public class CompletionOracle extends SuggestOracle {
|
|||||||
if (c.getReplacementString().startsWith(request.getQuery())) {
|
if (c.getReplacementString().startsWith(request.getQuery())) {
|
||||||
suggestions.add(c);
|
suggestions.add(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Use filtered old suggestions, if there are enough of them,
|
if (suggestions.size() > 1 && (suggestions.size() >= request.getLimit() || oldSuggestions.size() < request.getLimit())) {
|
||||||
// or if the old ones were already fewer than the limit.
|
|
||||||
if (suggestions.size() > 0 && (suggestions.size() >= request.getLimit() || oldSuggestions.size() < request.getLimit())) {
|
|
||||||
return suggestions;
|
return suggestions;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void retrieveSuggestions(final SuggestOracle.Request request, final SuggestOracle.Callback callback) {
|
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
|
// hack: first report no completions, to hide suggestions until we get the new completions
|
||||||
callback.onSuggestionsReady(request, new SuggestOracle.Response(Collections.<CompletionSuggestion>emptyList()));
|
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) {
|
private void suggestionsReady(SuggestOracle.Request request, SuggestOracle.Callback callback, List<CompletionSuggestion> suggestions) {
|
||||||
this.oldQuery = request.getQuery();
|
this.oldQuery = request.getQuery();
|
||||||
this.oldSuggestions = suggestions;
|
this.oldSuggestions = suggestions;
|
||||||
|
suggestions = suggestions.size() <= request.getLimit() ? suggestions : SubList.makeSubList(suggestions, 0, request.getLimit());
|
||||||
callback.onSuggestionsReady(request, new SuggestOracle.Response(suggestions));
|
callback.onSuggestionsReady(request, new SuggestOracle.Response(suggestions));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
37
src/server/gwt/src/se/chalmers/cs/gf/gwt/client/SubList.java
Normal file
37
src/server/gwt/src/se/chalmers/cs/gf/gwt/client/SubList.java
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user