显示上下文菜单

 

通过扩展点添加菜单时,必须添加下面的方法,才能显示上下文菜单

private void hookContextMenu() {

MenuManager menuMgr = new MenuManager("#PopupMenu");

menuMgr.setRemoveAllWhenShown(false);

                //这是通过action方式添加上下文菜单,这是直接添加

menuMgr.add(addAction);

                //这是监听器,将要显示上下文菜单时,调用该方法

menuMgr.addMenuListener(new IMenuListener() {

public void menuAboutToShow(IMenuManager manager) {

AddressView.this.fillContextMenu(manager);

}

});

Menu menu = menuMgr.createContextMenu(viewer.getControl());

viewer.getControl().setMenu(menu);

getSite().registerContextMenu(menuMgr, viewer);

}

 

相关类说:

 

1.

org.eclipse.jface.action 

public class MenuManager

extends ContributionManager

implements IMenuManager

A menu manager is a contribution manager which realizes itself and its items in a menu control; either as a menu bar, a sub-menu, or a context menu.

 

 

2.

org.eclipse.jface.action

public interface IMenuListener

A menu listener that gets informed when a menu is about to show.

相关方法:

void menuAboutToShow(IMenuManager manager)

Notifies this listener that the menu is about to be shown by the given menu manager.

你可能感兴趣的:(eclipse插件)