在我的RCP应用程序中,TreeViewer左侧有一个用于导航的视图,右侧有一个用于视图的文件夹。透视图看起来像这样:
TreeViewer
public void createInitialLayout(IPageLayout layout) { layout.setEditorAreaVisible(false); layout.setFixed(false); layout.addStandaloneView(NavigationView.ID, false, IPageLayout.LEFT, 0.7f, layout.getEditorArea()); right = layout.createFolder("right", IPageLayout.RIGHT, 0.3f, "com.my.app.views.browser.navigation"); layout.getViewLayout(WallpaperView.Id).setCloseable(false);//dummy view to keep the folder from closing layout.getViewLayout(WallpaperView.Id).setMoveable(false); right.addView(WallpaperView.Id); //add some placeholders for the potential views right.addPlaceholder(DefaultAdminView.ID+":*"); }
我想根据用户在导航树中选择的内容打开不同的视图。认为这并不难。我的导航树视图:
tree = new TreeViewer(composite); tree.setContentProvider(new BrowserNavigationTreeContentProvider()); tree.setLabelProvider(new BrowserNavigationTreeLabelProvider()); tree.setInput(UserProfileAdvisor.getProject()); //register Mouselistener for doubleclick events tree.addDoubleClickListener(new IDoubleClickListener(){ @Override public void doubleClick(DoubleClickEvent event) { TreeSelection ts = (TreeSelection) event.getSelection(); Object selectedItem = ts.getFirstElement(); String viewId = DefaultAdminView.ID; //set viewId depending on the selectedItem.class try { PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(viewId, String.valueOf(++viewCounter), IWorkbenchPage.VIEW_ACTIVATE); } catch (PartInitException e) { ILogHelper.error("The view for the selected object could not be opened", e); } } });
这似乎很好。仅有一个小问题:我需要以某种方式将对象(例如,selectedItem)传递给我的视图,以使用户与其内容进行交互。我怎么做?
我看到了一些示例,其中一些同事编写了自己的视图,并将其放置在右侧。然后,他们添加CTabFolder,实例化视图并手动添加。有没有更聪明的解决方案?
CTabFolder
创建一个新接口,为其提供一个类似的方法accept( Object parameter ),并使您的视图实现它。
accept( Object parameter )
然后,当您执行PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(viewId, String.valueOf(++viewCounter), IWorkbenchPage.VIEW_ACTIVATE)该方法时,showView将返回IViewPart。将此返回值转换为您的界面并调用accept方法。
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(viewId, String.valueOf(++viewCounter), IWorkbenchPage.VIEW_ACTIVATE)
showView
IViewPart
accept