private void onKey(int key) { if (key == KeyCodes.KEY_ESCAPE) { setText(prevText); Widget p = getParent(); if (p instanceof SuggestBox) { // Since the text was changed, ensure that the SuggestBox is // aware of this change so that it will refresh properly on // the next keystroke. Without this, if the first keystroke // recreates the same string as before ESC was pressed, the // SuggestBox will think that the string has not changed, and // it will not yet provide any Suggestions. ((SuggestBox) p).showSuggestionList(); // The suggestion list lingers if we don't hide it. ((DefaultSuggestionDisplay) ((SuggestBox) p).getSuggestionDisplay()).hideSuggestions(); } setFocus(false); } }
@UiHandler("searchBox") void searchBoxEnter(KeyDownEvent event) { if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) { SuggestionDisplay suggestionDisplay = searchBox.getSuggestionDisplay(); // This should always be true unless GWT changes the type of the suggestion generated by the // SuggestBox. It is too complicated and nasty to switch out the SuggestBox suggestion display // factory, so we're left with this type safety check and broken functionality if GWT changes. Preconditions.checkState(suggestionDisplay instanceof DefaultSuggestionDisplay); // At this point this should always be true. if (suggestionDisplay instanceof DefaultSuggestionDisplay) { DefaultSuggestionDisplay suggestions = (DefaultSuggestionDisplay) suggestionDisplay; if (!suggestions.isSuggestionListShowing()) { presenter.handleSearch(searchBox.getValue()); } } } }
public RemoteSuggestBox(SuggestOracle oracle) { remoteSuggestOracle = new RemoteSuggestOracle(oracle); remoteSuggestOracle.setServeSuggestions(true); display = new DefaultSuggestionDisplay(); textBox = new HintTextBox(); textBox.addKeyDownHandler( new KeyDownHandler() { @Override public void onKeyDown(KeyDownEvent e) { submitOnSelection = false; if (e.getNativeKeyCode() == KeyCodes.KEY_ESCAPE) { CloseEvent.fire(RemoteSuggestBox.this, RemoteSuggestBox.this); } else if (e.getNativeKeyCode() == KeyCodes.KEY_ENTER) { if (display.isSuggestionListShowing()) { if (textBox.getValue().equals(remoteSuggestOracle.getLast())) { submitOnSelection = true; } else { display.hideSuggestions(); } } else { SelectionEvent.fire(RemoteSuggestBox.this, getText()); } } } }); suggestBox = new SuggestBox(remoteSuggestOracle, textBox, display); suggestBox.addSelectionHandler( new SelectionHandler<Suggestion>() { @Override public void onSelection(SelectionEvent<Suggestion> event) { if (submitOnSelection) { SelectionEvent.fire(RemoteSuggestBox.this, getText()); } remoteSuggestOracle.cancelOutstandingRequest(); display.hideSuggestions(); } }); initWidget(suggestBox); }