Skip to main content



      Home
Home » Modeling » TMF (Xtext) » popup menu contribution to outline (Xtext 2.0 M5)
popup menu contribution to outline (Xtext 2.0 M5) [message #656447] Fri, 25 February 2011 12:20 Go to next message
Eclipse UserFriend
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
Re: popup menu contribution to outline (Xtext 2.0 M5) [message #656745 is a reply to message #656447] Mon, 28 February 2011 07:17 Go to previous messageGo to next message
Eclipse UserFriend
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


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: popup menu contribution to outline (Xtext 2.0 M5) [message #656948 is a reply to message #656745] Tue, 01 March 2011 02:04 Go to previous messageGo to next message
Eclipse UserFriend
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
>
>
Re: popup menu contribution to outline (Xtext 2.0 M5) [message #731879 is a reply to message #656948] Mon, 03 October 2011 07:25 Go to previous messageGo to next message
Eclipse UserFriend
Hi toghether,

I am new to Eclipse and would like to have context menus for the tree nodes of the xtext outline, as described in my first post Universal tree-based code editor with custom tree node sub menus.


I guess I am at the right place now since Henrik seems to have a solution for context menus.
However, I don't know enough about eclipse and java to directly use the code snippets. Could
you please upload a small xtext project example with a working tree context menu?

Sunny regards,

Stefan

[Updated on: Mon, 03 October 2011 07:30] by Moderator

Re: popup menu contribution to outline (Xtext 2.0 M5) [message #735049 is a reply to message #731879] Mon, 10 October 2011 14:54 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

the code can be found here: http://git.eclipse.org/c/etrice/

~Christian
Re: popup menu contribution to outline (Xtext 2.0 M5) [message #735527 is a reply to message #735049] Wed, 12 October 2011 02:18 Go to previous message
Eclipse UserFriend
Hi Christian,

thank you very much! My outline popup menu works now.

Best regards,

Stefan

Edit: here is my example code:

http://www.eclipse.org/forums/index.php/m/734711/#msg_734711

[Updated on: Thu, 03 November 2011 09:54] by Moderator

Previous Topic:Automatic braces completion
Next Topic:Partial parsing of large files
Goto Forum:
  


Current Time: Thu Nov 13 09:50:21 EST 2025

Powered by FUDForum. Page generated in 0.04340 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top