Search in sources :

Example 1 with SuggestionPopupCloseListener

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);
}
Also used : TextInputContentListener(org.apache.pivot.wtk.TextInputContentListener) SuggestionPopupCloseListener(org.apache.pivot.wtk.SuggestionPopupCloseListener) SuggestionPopup(org.apache.pivot.wtk.SuggestionPopup) ArrayList(org.apache.pivot.collections.ArrayList) TextInput(org.apache.pivot.wtk.TextInput) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Example 2 with SuggestionPopupCloseListener

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);
}
Also used : Task(org.apache.pivot.util.concurrent.Task) SuggestionPopupCloseListener(org.apache.pivot.wtk.SuggestionPopupCloseListener) SuggestionPopup(org.apache.pivot.wtk.SuggestionPopup) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) GetQuery(org.apache.pivot.web.GetQuery) TaskListener(org.apache.pivot.util.concurrent.TaskListener) List(org.apache.pivot.collections.List)

Aggregations

SuggestionPopup (org.apache.pivot.wtk.SuggestionPopup)2 SuggestionPopupCloseListener (org.apache.pivot.wtk.SuggestionPopupCloseListener)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)1 ArrayList (org.apache.pivot.collections.ArrayList)1 List (org.apache.pivot.collections.List)1 Task (org.apache.pivot.util.concurrent.Task)1 TaskListener (org.apache.pivot.util.concurrent.TaskListener)1 GetQuery (org.apache.pivot.web.GetQuery)1 TextInput (org.apache.pivot.wtk.TextInput)1 TextInputContentListener (org.apache.pivot.wtk.TextInputContentListener)1