@Override protected void moveSelectionDown() { // Make sure that the menu is actually showing and focus the selected item if (suggestionPopup != null && suggestionPopup.isShowing()) { suggestionsTable.focusItemByIndex(suggestionsTable.getSelectedItemIndex()); if (suggestionsTable.getSelectedItemIndex() == 0) { Scheduler.get() .scheduleDeferred( new ScheduledCommand() { @Override public void execute() { suggestionsTable.getElement().getParentElement().setScrollTop(0); } }); } } }
private SuggestCellTable<SearchSuggestion> getSuggestionCellList( final Collection<SearchSuggestion> suggestions, final SuggestBox suggestBox, final PopupPanel suggestionPopup) { this.suggestBox = suggestBox; this.suggestionPopup = suggestionPopup; // Create suggestions table final SuggestCellTable<SearchSuggestion> suggestionsTable = new SuggestCellTable<>( suggestions.size(), (Resources) GWT.create(SuggestionsTableResources.class)); // Create table's column and add it to the table SearchSuggestionColumn<SearchSuggestion> suggestColumn = new SearchSuggestionColumn<SearchSuggestion>() { @Override public SearchSuggestion getValue(SearchSuggestion suggestion) { return suggestion; } }; suggestionsTable.addColumn(suggestColumn); // Create a data provider and bind it to the table suggestionDataProvider = new ListDataProvider<>(); suggestionDataProvider.addDataDisplay(suggestionsTable); // Add suggestions to data provider List<SearchSuggestion> list = suggestionDataProvider.getList(); for (SearchSuggestion suggestion : suggestions) { list.add(suggestion); } // Bind a selection model it to the table suggestionsTable.setSelectionModel(new SingleSelectionModel<SearchSuggestion>()); // Set table's properties suggestionsTable.setWidth("100%"); // $NON-NLS-1$ suggestionsTable.setRowCount(suggestions.size()); suggestionsTable.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.BOUND_TO_SELECTION); // Add enter key press event handler suggestionsTable.addDomHandler( new KeyDownHandler() { @Override public void onKeyDown(KeyDownEvent event) { if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) { onSelect(); } if (event.getNativeKeyCode() == KeyCodes.KEY_ESCAPE) { hideSuggestions(); } } }, KeyDownEvent.getType()); // Add click event handler suggestionsTable.addDomHandler( new ClickHandler() { @Override public void onClick(ClickEvent event) { onSelect(); } }, ClickEvent.getType()); return suggestionsTable; }