Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Project context menu
Project context menu [message #875451] Tue, 22 May 2012 15:22 Go to next message
Edwin Park is currently offline Edwin Park
Messages: 124
Registered: July 2009
Senior Member
Hi,

I'm trying to figure out how to:

1. contribute a menu item command in the right-click context menu of a project in the Project Explorer view.
2. get access to the selected IProject in the command handler

I currently have a top-level menu/command/handler contributed via a fragment.e4xmi that uses the ESelectionService to determine if the selection holds an IProject. This is fine, but given that the command is really only related to a project, I'd like to be able to let the user right-click on the project itself and select the command from the project's context menu.

Any pointers/help is greatly appreciated!

Thanks,
Edwin
Re: Project context menu [message #875774 is a reply to message #875451] Wed, 23 May 2012 06:25 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 597
Registered: December 2010
Senior Member

Edwin Park wrote on Tue, 22 May 2012 22:22

2. get access to the selected IProject in the command handler

@Inject
public void handleSelection(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IProject project) {
	if (project != null) {
		// handle it
	}
}
Re: Project context menu [message #876978 is a reply to message #875774] Fri, 25 May 2012 12:08 Go to previous messageGo to next message
Edwin Park is currently offline Edwin Park
Messages: 124
Registered: July 2009
Senior Member
Thanks for the reply, but I don't think this works for my particular situation (or I may need some additional help figuring out how it would!)

My understanding is the @Inject would work for a handleSelection() method in a View... but in my case I want to get access to the selected project from a command handler annotated with @Execute. Basically this is a one-off command I want to run that needs to know the location of the selected project to do its work.

@Execute
public void execute(...???...) {
    IProject project = ???
    IPath location = project.getLocation();
    ...
}


Just for the heck of it I tried doing what you suggested anyway, but when I do that the method is never invoked when I select the command:

@Execute
@Inject
public void execute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IProject project) throws IOException {
    if (project != null) {
        IPath location = project.getLocation();
        ...
    }
}


I'm also still in the dark as to how to contribute a menu item to the right-click context menu for a project (my original question #1)...

Thanks,
Edwin
Re: Project context menu [message #877003 is a reply to message #876978] Fri, 25 May 2012 13:23 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 597
Registered: December 2010
Senior Member

Try to remove the @Inject after @Execute in the second code sample.

When you annotate methods with @Focus, @Execute the framework tries to inject the values of the matching type or of the matching name (what @Inject does), regardless if it is in a view or in a handler.

Another alternative would be to use the ESelectionService.getSelection() inside your method which gets the value from the context after the aggregation, much in the same way as the parameter injection does.
Re: Project context menu [message #880144 is a reply to message #876978] Fri, 01 June 2012 10:17 Go to previous message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Edwin Park wrote on Fri, 25 May 2012 12:08


@Execute
@Inject
public void execute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IProject project) throws IOException {
    if (project != null) {
        IPath location = project.getLocation();
        ...
    }
}




I think Sopot mentioned this, but your handlers shouldn't have any @Inject annotations in them. Everything should be requested in your @Execute method parameter list.

Quote:


I'm also still in the dark as to how to contribute a menu item to the right-click context menu for a project (my original question #1)...


To contribute to a popup menu, use an MMenuContribution added to the MApplication. It follows the same patterns as an org.eclipse.ui.menus menuContribution. There's also a visibleWhen attribute where you can add an MCoreExpression. The expression must still be defined in the org.eclipse.core.expressions.definitions extension point.

PW


Previous Topic:Is it possible to listen for a workbench to be closed by force?
Next Topic:Eclipse IDE as a pure E4 IDE
Goto Forum:
  


Current Time: Fri Aug 22 19:46:10 EDT 2014

Powered by FUDForum. Page generated in 0.02925 seconds