1、代码
private void makeActions() { action1 = new Action() { public void run() { showMessage("Action 1 executed"); } }; action1.setText("Action 1"); action1.setToolTipText("Action 1 tooltip"); action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages(). getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK)); action2 = new Action() { public void run() { showMessage("Action 2 executed"); } }; action2.setText("Action 2"); action2.setToolTipText("Action 2 tooltip"); action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages(). getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK)); contributeToActionBars(); } private void contributeToActionBars() { //获得视图的操作栏对象 IActionBars bars = getViewSite().getActionBars(); //添加下拉菜单 fillLocalPullDown(bars.getMenuManager()); //添加工具栏 fillLocalToolBar(bars.getToolBarManager()); hookContenxtMenu(); } //添加下拉菜单 private void fillLocalPullDown(IMenuManager manager) { manager.add(action1); manager.add(new Separator()); manager.add(action2); } //添加工具栏 private void fillLocalToolBar(IToolBarManager manager) { manager.add(action1); manager.add(action2); } private void hookContenxtMenu() { //创建菜单管理器对象 MenuManager menuMgr = new MenuManager("#PopupMenu"); menuMgr.add(action1); menuMgr.add(action2); menuMgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); //为列表对象创建上下文菜单 Menu menu = menuMgr.createContextMenu(viewer.getControl()); //设置菜单 viewer.getControl().setMenu(menu); //注册上下文菜单 getSite().registerContextMenu(menuMgr, viewer); }
2、效果:
标签:菜单,void,action1,add,视图,action2,manager,右键,Action From: https://www.cnblogs.com/wwssgg/p/16879468.html