| 153 | // Need to use fully-qualified names as this class inherits already |
| 154 | // from a pair of inner classes called Request / Response :-/ |
| 155 | public void onResponseReceived(final com.google.gwt.http.client.Request r, |
| 156 | final com.google.gwt.http.client.Response response) { |
| 157 | if (response.getStatusCode() == com.google.gwt.http.client.Response.SC_OK) { |
| 158 | final JSONValue json = JSONParser.parse(response.getText()); |
| 159 | // In case this request returned nothing, we pretend the last |
| 160 | // suggestion ended with the largest character possible, so we |
| 161 | // won't send more requests to the server if the user keeps |
| 162 | // adding extra characters. |
| 163 | last_suggestion = last_query + "\377"; |
| 164 | if (json != null && json.isArray() != null) { |
| 165 | final JSONArray results = json.isArray(); |
| 166 | final int n = Math.min(request.getLimit(), results.size()); |
| 167 | for (int i = 0; i < n; i++) { |
| 168 | final JSONValue suggestion = results.get(i); |
| 169 | if (suggestion == null || suggestion.isString() == null) { |
| 170 | continue; |
| 171 | } |
| 172 | final String suggestionstr = suggestion.isString().stringValue(); |
| 173 | last_suggestion = suggestionstr; |
| 174 | cache.add(suggestionstr); |
| 175 | } |
| 176 | // Is this response still relevant to what the requester wants? |
| 177 | if (requester.getText().startsWith(last_query)) { |
| 178 | cache.requestSuggestions(request, callback); |
| 179 | pending_req = null; |
| 180 | pending_cb = null; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | current = null; // Regardless of what happened above, this is done. |
| 185 | if (pending_req != null) { |
| 186 | final Request req = pending_req; |
| 187 | final Callback cb = pending_cb; |
| 188 | pending_req = null; |
| 189 | pending_cb = null; |
| 190 | requestSuggestions(req, cb); |
| 191 | } |
| 192 | } |
| 193 | }); |
| 194 | } catch (RequestException ignore) { |
| 195 | } |