How to Create Context Menu for CommonViewer

As we are moving from eclipse3.1 to eclipse3.3 and beyond, the well-known Common Navigator Framework (CNF) are more commonly utilized to serve the needs of providing a robust and loosely coupled content view structure.

However, the inherent design of the CommonNavigator requests each of its instances maintain their action service and composing viewer separately. In other words, if we want to reuse the common viewer by instantiating one outside of its Navigator scope like wizard page or JFace dialog, we would encounter the bottleneck of reusing the viewer’s associated action service.

Therefore, to achieve adding context menu to a reused common viewer, we have to programmatically constitute the context menu and hook up action service from the navigator.

The following code snippet simply returns an instance of CommonViewer for reuse purpose.

public static CommonViewer createEEViewer(Composite content, int aStyle) {
                CommonViewer commonViewer = new CommonViewer(
                                SuadeServersPlugin.SERVERS_VIEW_VIEWER_ID, content, aStyle);
 
 
                return commonViewer;
        }

 After getting the CommonViewer, we have to programmatically create a context menu manager, get the actionService from the Navigator, and hook it into the context menu. The following section illustrate how to implement a context menu on CommonViewer in a wizard page.

private void createContextMenu(final CommonViewer viewer) {
                MenuManager menuMgr = new MenuManager();
                menuMgr.setRemoveAllWhenShown(true);
                menuMgr.addMenuListener(new IMenuListener() {
                        CommonNavigator eeView = (CommonNavigator) PlatformUI
                                        .getWorkbench().getActiveWorkbenchWindow().getActivePage()
                                        .findView(SuadeServersPlugin.SERVERS_VIEW_VIEWER_ID);
 
                        public void menuAboutToShow(IMenuManager manager) {
                                ISelection selection = viewer.getSelection();
                                eeView.getNavigatorActionService().setContext(
                                                new ActionContext(selection));
                                eeView.getNavigatorActionService().fillContextMenu(manager);
 
                        }
                });
 
                Menu menu = menuMgr.createContextMenu(viewer.getTree());
                viewer.getTree().setMenu(menu);
        }
Advertisement
This entry was posted in Eclipse Plugin/RCP. Bookmark the permalink.

2 Responses to How to Create Context Menu for CommonViewer

  1. halena says:

    一???
    果然全英文。。。汗记~~ 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s