Revision: 8800
Author: [email protected]
Date: Thu Sep 16 08:59:16 2010
Log: Extending SuggestOracle.Response to pass along "isTruncated" information with the suggestion list.

Review at http://gwt-code-reviews.appspot.com/873801

Review by: [email protected]
http://code.google.com/p/google-web-toolkit/source/detail?r=8800

Modified:
 /trunk/user/src/com/google/gwt/user/client/ui/MultiWordSuggestOracle.java
 /trunk/user/src/com/google/gwt/user/client/ui/SuggestOracle.java

=======================================
--- /trunk/user/src/com/google/gwt/user/client/ui/MultiWordSuggestOracle.java Fri Jan 16 11:50:20 2009 +++ /trunk/user/src/com/google/gwt/user/client/ui/MultiWordSuggestOracle.java Thu Sep 16 08:59:16 2010
@@ -219,9 +219,25 @@

   @Override
   public void requestSuggestions(Request request, Callback callback) {
-    final List<MultiWordSuggestion> suggestions = computeItemsFor(
-        request.getQuery(), request.getLimit());
+    String query = normalizeSearch(request.getQuery());
+    int limit = request.getLimit();
+
+    // Get candidates from search words.
+    List<String> candidates = createCandidatesFromSearch(query);
+
+    // Respect limit for number of choices.
+    int numberTruncated = Math.max(0, candidates.size() - limit);
+    for (int i = candidates.size() - 1; i > limit; i--) {
+      candidates.remove(i);
+    }
+
+    // Convert candidates to suggestions.
+    List<MultiWordSuggestion> suggestions =
+        convertToFormattedSuggestions(query, candidates);
+
     Response response = new Response(suggestions);
+    response.setMoreSuggestionsCount(numberTruncated);
+
     callback.onSuggestionsReady(request, response);
   }

@@ -269,23 +285,6 @@
     String escaped = convertMe.getHTML();
     return escaped;
   }
-
-  /**
-   * Compute the suggestions that are matches for a given query.
-   *
-   * @param query search string
-   * @param limit limit
-   * @return matching suggestions
-   */
- private List<MultiWordSuggestion> computeItemsFor(String query, int limit) {
-    query = normalizeSearch(query);
-
-    // Get candidates from search words.
-    List<String> candidates = createCandidatesFromSearch(query, limit);
-
-    // Convert candidates to suggestions.
-    return convertToFormattedSuggestions(query, candidates);
-  }

   /**
* Returns real suggestions with the given query in <code>strong</code> html
@@ -344,7 +343,7 @@
   /**
* Find the sorted list of candidates that are matches for the given query.
    */
- private List<String> createCandidatesFromSearch(String query, int limit) {
+  private List<String> createCandidatesFromSearch(String query) {
     ArrayList<String> candidates = new ArrayList<String>();

     if (query.length() == 0) {
@@ -381,10 +380,6 @@
     if (candidateSet != null) {
       candidates.addAll(candidateSet);
       Collections.sort(candidates);
-      // Respect limit for number of choices.
-      for (int i = candidates.size() - 1; i > limit; i--) {
-        candidates.remove(i);
-      }
     }
     return candidates;
   }
=======================================
--- /trunk/user/src/com/google/gwt/user/client/ui/SuggestOracle.java Fri Jan 16 11:50:20 2009 +++ /trunk/user/src/com/google/gwt/user/client/ui/SuggestOracle.java Thu Sep 16 08:59:16 2010
@@ -119,10 +119,31 @@

   /**
    * {...@link com.google.gwt.user.client.ui.SuggestOracle} response.
+   *
+ * <p>Can optionally have truncation information provided. To indicate that
+   * there are more results but the number is not known, use:
+   *
+   * <p><code>response.setMoreSuggestions(true);</code>
+   *
+   * <p>Or to indicate more results with an exact number, use:
+   *
+   * <p><code>response.setMoreSuggestionsCount(102);</code>
    */
   public static class Response implements IsSerializable {
     private Collection<? extends Suggestion> suggestions;

+    /**
+ * The response is considered to have "more suggestions" when the number of + * matching suggestions exceeds {...@link Request#getLimit}, so the response
+     * suggestion list is truncated.
+     */
+    private boolean moreSuggestions = false;
+
+    /**
+     * Number of truncated suggestions.
+     */
+    private int numMoreSuggestions = 0;
+
     /**
      * Constructor for {...@link Response}.
      */
@@ -139,6 +160,16 @@
       setSuggestions(suggestions);
     }

+    /**
+     * Gets how many more suggestions there are.
+     *
+ * @return the count. if there no more suggestions or the number of more
+     *         suggestions is unknown, returns 0.
+     */
+    public int getMoreSuggestionsCount() {
+      return this.numMoreSuggestions;
+    }
+
     /**
* Gets the collection of suggestions. Each suggestion must implement the
      * {...@link Suggestion} interface.
@@ -148,6 +179,36 @@
     public Collection<? extends Suggestion> getSuggestions() {
       return this.suggestions;
     }
+
+    /**
+     * Gets whether or not the suggestion list was truncated due to the
+     * {...@request#getlimit}.
+     */
+    public boolean hasMoreSuggestions() {
+      return this.moreSuggestions;
+    }
+
+    /**
+     * Sets whether or not the suggestion list was truncated due to the
+     * {...@request#getlimit}.
+     */
+    public void setMoreSuggestions(boolean moreSuggestions) {
+      this.moreSuggestions = moreSuggestions;
+    }
+
+    /**
+     * Sets whether or not the suggestion list was truncated due to the
+     * {...@request#getlimit}, by providing an exact count of remaining
+     * suggestions.
+     *
+ * @param count number of truncated suggestions. Pass 0 to indicate there
+     *        are no other suggestions, which is equivlent to
+     *        {...@link #setMoreSuggestions(false)}.
+     */
+    public void setMoreSuggestionsCount(int count) {
+      this.numMoreSuggestions = count;
+      this.moreSuggestions = (count > 0);
+    }

     /**
* Sets the suggestions for this response. Each suggestion must implement

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to