show up dynamic (part) menu [message #635367] |
Tue, 26 October 2010 15:05 |
|
Hello,
I want to implement a popup menu for a JFace TreeViewer that is
contained in a view part represented in the model.
The popup menu should (as usual) be different depending on the
selection. I found a snippet where that is implementetdoing in this way:
MenuManager manager = new MenuManager();
manager.setRemoveAllWhenShown(true);
manager.addMenuListener(new IMenuListener() {
@Override
public void menuAboutToShow(final IMenuManager currentManager) {
IStructuredSelection selection =
(IStructuredSelection) treeViewer.getSelection();
if (!selection.isEmpty())
currentManager.add(someAction);
...
}
}
});
treeViewer.getControl().setMenu(manager.createContextMenu(tr eeViewer.getControl()));
---
Now I thought about introducing the menu to the workbench model.
It is possible to add menus to parts or create menu contributions via
the model editor.
So what is the proper way to (combine and) show them up in this case?
Thanks,
Jens
|
|
|
Re: show up dynamic (part) menu [message #635540 is a reply to message #635367] |
Wed, 27 October 2010 09:32 |
|
Hi all,
I figured out that I can use the EMenuService to add a menu to any control.
MPopupMenu contextMenu =
menuService.registerContextMenu(viewer.getControl(), "contextMenu");
That only succeeds, when the menu is defined in the "Menus" section of
the part and is of type MPopupMenu.
I had to add the type as an attribute in the e4xmi manually (the e4
Workbenchmodel editor doesn't seem to support popup menus there yet):
<menus xsi:type="menu:PopupMenu" .. > .. </menus>
Now the context menu is showing up, but the set of menu items is still
static.
My question now is whether I could use visibleWhen-Expressions to show /
hide single items depending on the current selection in the tree.
I don't know how and where to define those expressions, so
I would appreciate if anyone could give me a simple example.
Thanks,
Jens
|
|
|
|
Re: show up dynamic (part) menu [message #635662 is a reply to message #635598] |
Wed, 27 October 2010 16:25 |
|
Hi Paul,
Thank you very much! Now, my context menu lives up to its name.
Primary insights:
* I have to define the core expression via the extension point
org.eclipse.core.expressions.definitions
* they must refer to variables that I modify by the part's
IEclipseContext in an ISelectionChangedListener of the TreeViewer
* I should use Menu Contributions, since "normal" menu items won't
disappear again (when the visibleWhen expression is no longer true)
Secondary insights:
* DirectMenuItems don't call the @CanExecute-Method, therefore I have to
use Commands
* An entire Menu Contribution can be addressed by a visibleWhen
expression (rather than every single item)
Proposals for the e4 Workbenchmodel Editor:
* visibleWhen expressions should be assignable to Menu Contributions
* Popup Menus should be insertable into Parts
* Ctrl+X in a text field (on the right) should not cut the entire item
(on the left) ^^
So long,
Jens
|
|
|
|
|
Re: show up dynamic (part) menu [message #640137 is a reply to message #635367] |
Fri, 19 November 2010 10:08 |
Missing name Missing name Messages: 57 Registered: September 2009 |
Member |
|
|
Hello,
I fill this topic to ask a question about popupmenu on a TreeViewer.
I define a TreeViewer and add a context menu on right click like in org.eclipse.e4.demo.simpleide.navigator project example.
My aim is to display the popupmenu on right click on a Document node only.
I define a fragment.xmi file containing command, handler and HandledMenuItem for a menu Open on right clic.
The popupMenu is defined on a Part
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_Y-YtMPJXEd-zYovACAqcKg" featurename="children" parentElementId="west.southStack">
<elements xsi:type="basic:Part" xmi:id="_ccS-gPJXEd-zYovACAqcKg" elementId="ui.context" contributionURI="platform:/plugin/ui.contextview/ui.contextview.ui.ContextView" label="Context">
<menus xsi:type="menu:PopupMenu" xmi:id="_3ABIMPMwEd-ASfDNQGFGZQ" elementId="ContextView.popupmenu">
<children xsi:type="menu:HandledMenuItem" xmi:id="_KwfCAPMxEd-ASfDNQGFGZQ" elementId="ContextView.popupmenu.open" label="Open" enabled="true" command="_JQv4IPMyEd-ASfDNQGFGZQ">
<visibleWhen xsi:type="ui:CoreExpression" xmi:id="_wPOsQPOxEd-oqu9Gg20ngw" coreExpressionId="ui.contextview.selection.document"/>
</children>
<children xsi:type="menu:MenuSeparator" xmi:id="_Roqv4PMzEd-ASfDNQGFGZQ"/>
</menus>
</elements>
</fragments>
The plugin.xml file contains the extension:
<extension
point="org.eclipse.core.expressions.definitions">
<definition
id="ui.contextview.selection.document">
<with
variable="output.selection">
<count
value="1">
</count>
<iterate
ifEmpty="false"
operator="and">
<instanceof
value="ui.data.context.api.Document">
</instanceof>
</iterate>
</with>
</definition>
</extension>
My pb is the Open Menu is always disabled. Have you got an idea ?
Thank you.
[Updated on: Fri, 19 November 2010 10:09] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.03479 seconds