|
|
Re: popup menu contribution to outline (Xtext 2.0 M5) [message #656948 is a reply to message #656745] |
Tue, 01 March 2011 07:04 |
Henrik Rentz-Reichert Messages: 261 Registered: July 2009 |
Senior Member |
|
|
Jan,
thanks for your reply - even though you haven't got my point really ;-).
Now I came up with the following solution.
Since the new Xtext 2.0 org.eclipse.xtext.ui.editor.outline.impl.OutlinePage doesn't register a context menu at all I had to
derive from that. That gives me also the opportunity to set the default expansion level to 2.
public class RoomOutlinePage extends OutlinePage {
private Menu contextMenu;
private static final String contextMenuID = "RoomOutlineContextMenu";
/* (non-Javadoc)
* @see org.eclipse.xtext.ui.editor.outline.impl.OutlinePage#createC ontrol(org.eclipse.swt.widgets.Composite)
*/
@Override
public void createControl(Composite parent) {
super.createControl(parent);
configureContextMenu();
}
protected void configureContextMenu() {
MenuManager manager = new MenuManager(contextMenuID, contextMenuID);
manager.setRemoveAllWhenShown(true);
manager.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
fillContextMenu(manager);
}
});
contextMenu = manager.createContextMenu(getTreeViewer().getTree());
getTreeViewer().getTree().setMenu(contextMenu);
IPageSite site = getSite();
site.registerContextMenu("org.eclipse.etrice.core.room.outline ", manager, getTreeViewer()); //$NON-NLS-1$
}
protected void fillContextMenu(IMenuManager menu) {
menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
}
/* (non-Javadoc)
* @see org.eclipse.xtext.ui.editor.outline.impl.OutlinePage#getDefa ultExpansionLevel()
*/
@Override
protected int getDefaultExpansionLevel() {
return 2;
}
}
Then I had to override the binding in my UiModule
@Override
public Class<? extends IContentOutlinePage> bindIContentOutlinePage() {
return RoomOutlinePage.class;
}
After that I can register a menu contribution which adapts to an IOutlineNode.
I have to checkEnabled since I want to show my menu item only for selected nodes.
<menuContribution
allPopups="false"
locationURI="popup:org.eclipse.etrice.core.room.outline?after=additions ">
<command
commandId="org.eclipse.etrice.core.room.ui.editBehavior"
label="Edit Behavior"
style="push">
<visibleWhen
checkEnabled="true">
<iterate>
<adapt
type="org.eclipse.xtext.ui.editor.outline.IOutlineNode">
</adapt>
</iterate>
</visibleWhen>
</command>
</menuContribution>
And here is how I access the IOutlineNode from the handler's getEnabled method
@Override
public boolean isEnabled() {
IWorkbench wb = PlatformUI.getWorkbench();
IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
IWorkbenchPage page = win.getActivePage();
IWorkbenchPart part = page.getActivePart();
if (part instanceof ContentOutline) {
ISelection selection = ((ContentOutline)part).getSelection();
if (selection instanceof IStructuredSelection) {
IStructuredSelection ss = (IStructuredSelection) selection;
Object sel = ss.getFirstElement();
if (sel instanceof StructureClassNode) {
return true;
}
}
}
return false;
}
(I' didn't see another way to access the current selection of the outline)
Henrik
Am 28.02.2011 13:17, schrieb Jan Koehnlein:
> Hi Henrik
>
> it is not a good idea to pass an EObject beyond the borders of an IUnitOfWork, as it could be unloaded/modified concurrently.
> Rather than that, you should write your action for IOutlineNode and use IOutlineNode.readOnly() to resolve the element and perform
> your action within the same transaction.
>
> Regards
> Jan
>
>
> Am 25.02.11 18:20, schrieb Henrik Rentz-Reichert:
>> Hi,
>>
>> what is the proper way to add a menu item to the outline view?
>>
>> I tried to create my own IOutlineNode (derived from EObjectNode) and to
>> override getAdapter(Class adapterType) to return the EObject associated
>> with this node.
>>
>> In the plugin.xml I have
>>
>> <menuContribution
>> allPopups="false"
>> locationURI="popup:org.eclipse.xtext.ui.outline?after=additions ">
>> <command
>> commandId="org.eclipse.etrice.core.room.ui.editBehavior"
>> label="Edit Behavior"
>> style="push">
>> <visibleWhen
>> checkEnabled="false">
>> <iterate>
>> <adapt
>> type="org.eclipse.etrice.core.room.ActorClass">
>> </adapt>
>> </iterate>
>> </visibleWhen>
>> </command>
>> </menuContribution>
>>
>>
>> Now I realized that getAdapter is only called with IResource.
>>
>> Unfortunately neither the examples nor the documentation are up to date.
>>
>> Can you give me a code snippet that does the job?
>>
>> Thanks,
>> Henrik
>
>
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04140 seconds