use of org.apache.pivot.wtk.SuggestionPopupCloseListener in project pivot by apache.
the class SuggestionPopupTest method startup.
@Override
public void startup(final Display display, final Map<String, String> properties) throws Exception {
BXMLSerializer bxmlSerializer = new BXMLSerializer();
window = (Window) bxmlSerializer.readObject(SuggestionPopupTest.class, "suggestion_popup_test.bxml");
bxmlSerializer.bind(this);
textInput.getTextInputContentListeners().add(new TextInputContentListener() {
@Override
public void textInserted(final TextInput textInputArgument, final int index, final int count) {
ArrayList<String> suggestions = new ArrayList<>("One", "Two", "Three", "Four", "Five");
suggestionPopup.setSuggestionData(suggestions);
suggestionPopup.open(textInputArgument, new SuggestionPopupCloseListener() {
@Override
public void suggestionPopupClosed(final SuggestionPopup suggestionPopupArgument) {
if (suggestionPopupArgument.getResult()) {
selectedIndexLabel.setText("You selected suggestion number " + suggestionPopupArgument.getSelectedIndex() + ".");
} else {
selectedIndexLabel.setText("You didn't select anything.");
}
}
});
}
@Override
public void textRemoved(final TextInput textInputArgument, final int index, final int count) {
suggestionPopup.close();
}
});
window.open(display);
}
use of org.apache.pivot.wtk.SuggestionPopupCloseListener in project pivot by apache.
the class SuggestionDemo method getSuggestions.
private void getSuggestions() {
if (suggestionQuery != null && suggestionQuery.isPending()) {
suggestionQuery.abort();
}
// Get the query text
String text;
try {
text = URLEncoder.encode(textInput.getText(), "UTF-8");
} catch (UnsupportedEncodingException exception) {
throw new RuntimeException(exception);
}
// Create query
suggestionQuery = new GetQuery("search.yahooapis.com", "/WebSearchService/V1/relatedSuggestion");
suggestionQuery.getParameters().put("appid", getClass().getName());
suggestionQuery.getParameters().put("query", text);
suggestionQuery.getParameters().put("output", "json");
suggestionQuery.execute(new TaskAdapter<>(new TaskListener<Object>() {
@Override
public void taskExecuted(Task<Object> task) {
if (task == suggestionQuery) {
List<?> suggestions = null;
Object result = JSON.get(task.getResult(), "ResultSet.Result");
if (result instanceof List<?>) {
suggestions = (List<?>) result;
}
if (suggestions == null || suggestions.getLength() == 0) {
suggestionPopup.close();
} else {
suggestionPopup.setSuggestionData(suggestions);
suggestionPopup.open(textInput, new SuggestionPopupCloseListener() {
@Override
public void suggestionPopupClosed(SuggestionPopup suggestionPopupArgument) {
if (suggestionPopupArgument.getResult()) {
String textLocal;
try {
textLocal = URLEncoder.encode(textInput.getText(), "UTF-8");
} catch (UnsupportedEncodingException exception) {
throw new RuntimeException(exception);
}
String location = "http://search.yahoo.com/search?p=" + textLocal;
try {
Desktop.getDesktop().browse(new URI(location));
} catch (IOException exception) {
System.err.println(exception);
} catch (URISyntaxException exception) {
System.err.println(exception);
}
}
}
});
}
activityIndicator.setActive(false);
suggestionQuery = null;
}
}
@Override
public void executeFailed(Task<Object> task) {
if (task == suggestionQuery) {
System.err.println(task.getFault());
activityIndicator.setActive(false);
suggestionQuery = null;
}
}
}));
activityIndicator.setActive(true);
}
Aggregations