Dynamic context menu items
I am using Eclipse RCP to build desktop app. When the user invokes a popup menu I'd like to add some items to the men开发者_JS百科u. Something like a list of "suggested actions" to take for a problem. The pop-up is on a table and it already has commands on it. What is the right way to implement this?
in your ViewPart (for example) you could add
public void createPartControl(Composite parent) {
...
final Action a = new Action("") {};
final MenuManager mgr = new MenuManager();
mgr.setRemoveAllWhenShown(true);
mgr.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
final IStructuredSelection selection = (IStructuredSelection) listViewer
.getSelection();
if (!selection.isEmpty()) {
// example Action, here delete...
Action deleteAction = new Action("Delete") {
public void run() {
....
}
};
mgr.add(deleteAction);
// *** decide here which actions to add by ***
// *** evaluation of some of your variables ***
mgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
}
});
tableViewer.getControl().setMenu(
mgr.createContextMenu(tableViewer.getControl()));
....
}
精彩评论