Search in sources :

Example 1 with SuggestionPopup

use of org.apache.pivot.wtk.SuggestionPopup 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 SuggestionPopup

use of org.apache.pivot.wtk.SuggestionPopup in project pivot by apache.

the class TerraSuggestionPopupSkin method install.

@Override
public void install(Component component) {
    super.install(component);
    SuggestionPopup suggestionPopup = (SuggestionPopup) component;
    suggestionPopup.getSuggestionPopupListeners().add(this);
    suggestionPopup.getSuggestionPopupSelectionListeners().add(this);
    suggestionPopup.getSuggestionPopupStateListeners().add(this);
    suggestionPopup.setContent(listViewBorder);
    listView.setListData(suggestionPopup.getSuggestionData());
    listView.setItemRenderer(suggestionPopup.getSuggestionRenderer());
    if (!themeIsFlat()) {
        // Attach the drop-shadow decorator
        dropShadowDecorator = new DropShadowDecorator();
        suggestionPopup.getDecorators().add(dropShadowDecorator);
    }
}
Also used : SuggestionPopup(org.apache.pivot.wtk.SuggestionPopup) DropShadowDecorator(org.apache.pivot.wtk.effects.DropShadowDecorator)

Example 3 with SuggestionPopup

use of org.apache.pivot.wtk.SuggestionPopup in project pivot by apache.

the class TerraSuggestionPopupSkin method mouseClick.

@Override
public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
    SuggestionPopup suggestionPopup = (SuggestionPopup) getComponent();
    suggestionPopup.close(true);
    return true;
}
Also used : SuggestionPopup(org.apache.pivot.wtk.SuggestionPopup)

Example 4 with SuggestionPopup

use of org.apache.pivot.wtk.SuggestionPopup in project pivot by apache.

the class TerraSuggestionPopupSkin method windowOpened.

@Override
public void windowOpened(Window window) {
    super.windowOpened(window);
    // Adjust for list size
    SuggestionPopup suggestionPopup = (SuggestionPopup) window;
    int listSize = suggestionPopup.getListSize();
    if (listSize == -1) {
        listViewBorder.setPreferredHeight(-1);
    } else {
        if (!listViewBorder.isPreferredHeightSet()) {
            ListView.ItemRenderer itemRenderer = listView.getItemRenderer();
            int borderHeight = itemRenderer.getPreferredHeight(-1) * listSize + 2;
            if (listViewBorder.getPreferredHeight() > borderHeight) {
                listViewBorder.setPreferredHeight(borderHeight);
            } else {
                listViewBorder.setPreferredHeight(-1);
            }
        }
    }
    Display display = window.getDisplay();
    display.getContainerMouseListeners().add(displayMouseListener);
    if (dropShadowDecorator != null) {
        dropShadowDecorator.setShadowOpacity(DropShadowDecorator.DEFAULT_SHADOW_OPACITY);
    }
    returnFocusToTextInput = true;
    TextInput textInput = suggestionPopup.getTextInput();
    textInput.getComponentStateListeners().add(textInputStateListener);
    textInput.getComponentKeyListeners().add(textInputKeyListener);
    // Size and position the popup
    Point location = textInput.mapPointToAncestor(textInput.getDisplay(), 0, 0);
    window.setLocation(location.x, location.y + textInput.getHeight() - 1);
    window.setMinimumWidth(textInput.getWidth());
    window.setMaximumHeight(display.getHeight() - window.getY());
}
Also used : ListView(org.apache.pivot.wtk.ListView) SuggestionPopup(org.apache.pivot.wtk.SuggestionPopup) Point(org.apache.pivot.wtk.Point) TextInput(org.apache.pivot.wtk.TextInput) Point(org.apache.pivot.wtk.Point) Display(org.apache.pivot.wtk.Display)

Example 5 with SuggestionPopup

use of org.apache.pivot.wtk.SuggestionPopup 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)5 SuggestionPopupCloseListener (org.apache.pivot.wtk.SuggestionPopupCloseListener)2 TextInput (org.apache.pivot.wtk.TextInput)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 Display (org.apache.pivot.wtk.Display)1 ListView (org.apache.pivot.wtk.ListView)1 Point (org.apache.pivot.wtk.Point)1 TextInputContentListener (org.apache.pivot.wtk.TextInputContentListener)1 DropShadowDecorator (org.apache.pivot.wtk.effects.DropShadowDecorator)1