我有一个带有自定义工具栏的视图,其中包含第一条记录、下一条记录、上一条记录等按钮。我想实现这个功能。我有工具栏按钮的命令对象。如何传递有关表查看器上当前行是哪一行的信息?如果我选择工具栏上的下一条记录按钮,如何将焦点移动到下一条记录?
2 回答
Access current row:
int currentSelectionPosition=tableViewer.getTable().getSelectionIndex();
Set selection on next row
tableViewer.getTable().select(currentSelectionPosition+1);
First you have to make sure that the TableViewer
is the selection provider for your view:
getSite().setSelectionProvider(tableViewer);
This notifies the selection service of your workbench window every time the selected row in your table changes. The selection contains the model object that corresponds to the selected row.
In your command handler, you can get the current selection by calling
HandlerUtil.getCurrentSelectionChecked(executionEvent)
To move the focus in your TableViewer
when the next button is pressed, you have to implement a public method in your view that increases the selection index of the underlying Table
by one. To enable your command handler to call this method, you need to get the instance of the view. A quick-and-dirty solution would be to get the current part by calling
HandlerUtil.getActivePartChecked(executionEvent)
and cast the returned IWorkbenchPart
to your view class.