diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/SearchHistoryMenu.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/SearchHistoryMenu.java index 4360d900f91..4c7d127ae52 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/SearchHistoryMenu.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/SearchHistoryMenu.java @@ -54,6 +54,7 @@ public void shellDeactivated(ShellEvent e) { private int width; private Table table; private TableColumn column; + private int selectedIndexInTable = -1; public SearchHistoryMenu(Shell parent, HistoryStore history, Consumer historyEntrySelectedCallback) { super(parent); @@ -92,28 +93,58 @@ public Control createContents(Composite parent) { return table; } + private void moveSelectionInTable(int indexShift) { + selectedIndexInTable += indexShift; + if (selectedIndexInTable < 0) { + selectedIndexInTable = table.getItemCount() - 1; + } else if (selectedIndexInTable > table.getItemCount() - 1) { + selectedIndexInTable = 0; + } + table.setSelection(selectedIndexInTable); + historyEntrySelectedCallback.accept(table.getSelection()[0].getText()); + } + private void attachTableListeners() { - table.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> { - TableItem[] selection = table.getSelection(); - if (selection.length == 0) { - historyEntrySelectedCallback.accept(null); - return; + table.addListener(SWT.MouseMove, event -> { + Point point = new Point(event.x, event.y); + TableItem item = table.getItem(point); + if (item != null) { + table.setSelection(item); + selectedIndexInTable = table.getSelectionIndex(); } - String text = selection[0].getText(); - if (text != null) { - historyEntrySelectedCallback.accept(text); + }); + table.addKeyListener(KeyListener.keyPressedAdapter(e -> { + if (e.keyCode == SWT.ARROW_DOWN) { + moveSelectionInTable(1); + e.doit = false; + } else if (e.keyCode == SWT.ARROW_UP) { + moveSelectionInTable(-1); + e.doit = false; + } else if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) { + notifyParentOfSelectionInput(); + close(); } - historyEntrySelectedCallback.accept(null); + })); + table.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> { + notifyParentOfSelectionInput(); })); table.addMouseListener(MouseListener.mouseDownAdapter(e -> { table.notifyListeners(SWT.Selection, null); close(); })); - table.addKeyListener(KeyListener.keyPressedAdapter(e -> { - if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) { - close(); - } - })); + } + + private void notifyParentOfSelectionInput() { + TableItem[] selection = table.getSelection(); + if (selection.length == 0) { + historyEntrySelectedCallback.accept(null); + return; + } + String text = selection[0].getText(); + if (text != null) { + historyEntrySelectedCallback.accept(text); + } + historyEntrySelectedCallback.accept(null); } private void positionShell() {