Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters to the view

In my RCP application, I have a View with a TreeViewer for Navigation on the left side and a Folder for my views on the right side. The Perspective looks like this:

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+":*");

}

I would like to open different views, based on what the user selects in the navigation tree. Figured that wouldn't be to hard. My Navigation Tree view:

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);
        }
    }

});

This seems to work fine. There's just one tiny problem: I need to pass the object (let's say the selectedItem) to my view somehow, in order to let the user interact with its content. How do I do that?

I've seen some examples where some of my colleagues wrote an own View which they placed on the right side. Then they added a CTabFolder, instantiated the views and added them manually. Is there a smarter solution?

like image 375
Michael Avatar asked Dec 01 '22 03:12

Michael


1 Answers

Create a new interface, giving it a method like accept( Object parameter ) and make your views implement it.

Then, when you do PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(viewId, String.valueOf(++viewCounter), IWorkbenchPage.VIEW_ACTIVATE) the method showView returns an IViewPart. Cast this return to your interface and call the accept method.

like image 158
Mario Marinato Avatar answered Dec 04 '22 07:12

Mario Marinato