Removing/adding context menu entries based on current selection [message #895776] |
Mon, 16 July 2012 00:20 |
brian sar Messages: 1 Registered: July 2012 |
Junior Member |
|
|
In a plugin I am dynamically adding a Part with a tree in it(representing a small subset of the file system) and a MPopupMenu for the tree via a processor. What I would like, is, if a user right-clicks on a tree element, e.g. Folder, I would like to show a different context menu then if he/she clicks on a File item. I need some complex logic to handle all possible combinations. I found that to achieve something like this I have to use core expressions. If I understood correctly, the best solution is to use a property tester, since I need quite some code to handle all the combination. I can not manage to wire up core expression programmatically.
Here is my current code. Everything works, expect, I would need a hook which gets called before a context menu shows up.
private void registerPopup(final MPart part, final MApplication app) {
final MCommand command = MCommandsFactory.INSTANCE.createCommand();
command.setCommandName("tesadsa");
command.setElementId("testet2t");
app.getCommands().add(command);
final MHandler handler = MCommandsFactory.INSTANCE.createHandler();
handler.setContributorURI("platform:/plugin/"
+ FrameworkUtil.getBundle(this.getClass()).getSymbolicName());
handler.setContributionURI("bundleclass://"
+ FrameworkUtil.getBundle(this.getClass()).getSymbolicName()
+ "/" + WorkspacePopupHook.class.getName());
handler.setCommand(command);
app.getHandlers().add(handler);
final MCoreExpression coreExpression = MUiFactory.INSTANCE
.createCoreExpression();
// final Expression true1 = AlwaysEnabledExpression.TRUE;
// coreExpression.setCoreExpression(new WorkspaceExpression(null));
// coreExpression.setCoreExpression(true1);
// final OrExpression o = new OrExpression();
// final IConfigurationElement config = new
// ThirdLevelConfigurationElementHandle(
// null, 0);
// try {
// final PropertyTesterDescriptor p = new PropertyTesterDescriptor(
// config);
// } catch (final CoreException e) {
// e.printStackTrace();
// }
final MHandledMenuItem handledMenuItem = MMenuFactory.INSTANCE
.createHandledMenuItem();
handledMenuItem.setLabel("test");
handledMenuItem.setCommand(command);
//handledMenuItem.setVisibleWhen(coreExpression);
final MPopupMenu popupMenu = MMenuFactory.INSTANCE.createPopupMenu();
popupMenu.setElementId("testid");
popupMenu.setLabel("test2");
popupMenu.getChildren().add(handledMenuItem);
part.getMenus().add(popupMenu);
}
If I remove this comment
//handledMenuItem.setVisibleWhen(coreExpression);
it wont work anymore, I guess the default value for MCoreExpression is always false. So if this is the right way to do this, how can I instantiate PropertyTesterDescriptor since it needs an implementation of IConfigurationElement object which I don`t know how to instantiate. I also tried to define a core expression as an extension point and then get it through EModelService
final List findElements = mService.findElements(app, null,MCoreExpression.class, Collections.EMPTY_LIST);
with no success.
Thanks in advance.
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02280 seconds