public class MyView extends ViewPart implements ISelectionListener{
public void createPartControl(Composite parent) {
// add this view as a selection listener to the workbench page getSite().getPage().addSelectionListener((ISelectionListener) this);
}
// Implement the method defined in ISelectionListener, to consume UI selections public void selectionChanged(IWorkbenchPart part, ISelection selection) { //Examine selection and act on it! }
} 使用 UI 选择的更好的方法是,将消费者视图作为监听器注册到特定的视图部分。正如在下面的例子中可以看到的,源视图部分的视图 ID 在注册选择监听器期间被作为一个参数。
public void createPartControl(Composite parent) { // Set up a JFace Viewer viewer = new TableViewer(parent, SWT.MULTI SWT.H_SCROLL SWT.V_SCROLL); viewer.setContentProvider(new ViewContentProvider()); viewer.setLabelProvider(new ViewLabelProvider()); viewer.setSorter(new NameSorter()); viewer.setInput(getViewSite());
// ADD the JFace Viewer as a Selection Provider to the View site. getSite().setSelectionProvider(viewer);