Merge "Implement GroupSuggestOracle"
This commit is contained in:
@@ -64,6 +64,7 @@ java_test(
|
|||||||
deps = [
|
deps = [
|
||||||
':client',
|
':client',
|
||||||
'//lib:junit',
|
'//lib:junit',
|
||||||
|
'//lib/gwt:user',
|
||||||
'//lib/jgit:jgit',
|
'//lib/jgit:jgit',
|
||||||
],
|
],
|
||||||
source_under_test = [':client'],
|
source_under_test = [':client'],
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
// Copyright (C) 2015 The Android Open Source Project
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package com.google.gerrit.client.ui;
|
||||||
|
|
||||||
|
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
|
||||||
|
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
|
||||||
|
|
||||||
|
/** A {@code Suggestion} with highlights. */
|
||||||
|
public class HighlightSuggestion implements Suggestion {
|
||||||
|
|
||||||
|
private final String keyword;
|
||||||
|
private final String value;
|
||||||
|
|
||||||
|
public HighlightSuggestion(String keyword, String value) {
|
||||||
|
this.keyword = keyword;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDisplayString() {
|
||||||
|
int start = 0;
|
||||||
|
int keyLen = keyword.length();
|
||||||
|
SafeHtmlBuilder builder = new SafeHtmlBuilder();
|
||||||
|
for (;;) {
|
||||||
|
int index = value.indexOf(keyword, start);
|
||||||
|
if (index == -1) {
|
||||||
|
builder.appendEscaped(value.substring(start));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
builder.appendEscaped(value.substring(start, index));
|
||||||
|
builder.appendHtmlConstant("<strong>");
|
||||||
|
start = index + keyLen;
|
||||||
|
builder.appendEscaped(value.substring(index, start));
|
||||||
|
builder.appendHtmlConstant("</strong>");
|
||||||
|
}
|
||||||
|
return builder.toSafeHtml().asString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getReplacementString() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
// Copyright (C) 2015 The Android Open Source Project
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package com.google.gerrit.client.ui;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class HighlightSuggestionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void singleHighlight() throws Exception {
|
||||||
|
String keyword = "key";
|
||||||
|
String value = "somethingkeysomething";
|
||||||
|
HighlightSuggestion suggestion = new HighlightSuggestion(keyword, value);
|
||||||
|
assertEquals(
|
||||||
|
"something<strong>key</strong>something",
|
||||||
|
suggestion.getDisplayString());
|
||||||
|
assertEquals(value, suggestion.getReplacementString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void noHighlight() throws Exception {
|
||||||
|
String keyword = "key";
|
||||||
|
String value = "something";
|
||||||
|
HighlightSuggestion suggestion = new HighlightSuggestion(keyword, value);
|
||||||
|
assertEquals(value, suggestion.getDisplayString());
|
||||||
|
assertEquals(value, suggestion.getReplacementString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void doubleHighlight() throws Exception {
|
||||||
|
String keyword = "key";
|
||||||
|
String value = "somethingkeysomethingkeysomething";
|
||||||
|
HighlightSuggestion suggestion = new HighlightSuggestion(keyword, value);
|
||||||
|
assertEquals(
|
||||||
|
"something<strong>key</strong>something<strong>key</strong>something",
|
||||||
|
suggestion.getDisplayString());
|
||||||
|
assertEquals(value, suggestion.getReplacementString());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
// Copyright (C) 2015 The Android Open Source Project
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package com.google.gerrit.plugin.client.ui;
|
||||||
|
|
||||||
|
import com.google.gerrit.client.rpc.NativeMap;
|
||||||
|
import com.google.gerrit.client.ui.HighlightSuggestion;
|
||||||
|
import com.google.gerrit.plugin.client.rpc.RestApi;
|
||||||
|
import com.google.gwt.core.client.JavaScriptObject;
|
||||||
|
import com.google.gwt.user.client.rpc.AsyncCallback;
|
||||||
|
import com.google.gwt.user.client.ui.SuggestOracle;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/** A {@code SuggestOracle} for groups. */
|
||||||
|
public class GroupSuggestOracle extends SuggestOracle {
|
||||||
|
|
||||||
|
private final int chars;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param chars minimum chars to start suggesting.
|
||||||
|
*/
|
||||||
|
public GroupSuggestOracle(int chars) {
|
||||||
|
this.chars = chars;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isDisplayStringHTML() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void requestSuggestions(final Request req, final Callback done) {
|
||||||
|
if (req.getQuery().length() < chars) {
|
||||||
|
responseEmptySuggestion(req, done);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
RestApi rest = new RestApi("/groups/").addParameter("suggest", req.getQuery());
|
||||||
|
if (req.getLimit() > 0) {
|
||||||
|
rest.addParameter("n", req.getLimit());
|
||||||
|
}
|
||||||
|
rest.get(new AsyncCallback<NativeMap<GroupInfo>>() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(NativeMap<GroupInfo> result) {
|
||||||
|
List<String> keys = result.sortedKeys();
|
||||||
|
List<Suggestion> suggestions = new ArrayList<>(keys.size());
|
||||||
|
for (String g : keys) {
|
||||||
|
suggestions.add(new HighlightSuggestion(req.getQuery(), g));
|
||||||
|
}
|
||||||
|
done.onSuggestionsReady(req, new Response(suggestions));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(Throwable caught) {
|
||||||
|
responseEmptySuggestion(req, done);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void responseEmptySuggestion(Request req, Callback done) {
|
||||||
|
List<Suggestion> empty = Collections.emptyList();
|
||||||
|
done.onSuggestionsReady(req, new Response(empty));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class GroupInfo extends JavaScriptObject {
|
||||||
|
// Dummy class, does nothing.
|
||||||
|
|
||||||
|
protected GroupInfo() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user