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 選擇的更好的方法是,將消費(fèi)者視圖作為監(jiān)聽(tīng)器注冊(cè)到特定的視圖部分。正如在下面的例子中可以看到的,源視圖部分的視圖 ID 在注冊(cè)選擇監(jiān)聽(tīng)器期間被作為一個(gè)參數(shù)。
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);