Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Dynamic menu with MHandledMenuItem (Pass the object value to command parameters)
Dynamic menu with MHandledMenuItem [message #1013263] Sat, 23 February 2013 01:31 Go to previous message
Kirill Zotkin is currently offline Kirill Zotkin
Messages: 11
Registered: July 2009
Junior Member
Hello!

I've looked at http://stackoverflow.com/questions/12315749/e4-dynamic-menu-contributions and decided to implement with Kepler the dynamic menu for creating children of EObjects. (The complete source is here.)

...class CreateChildContribution...

	@Inject
	public void setSelection(
			@Optional @Named(IServiceConstants.ACTIVE_SELECTION) EObject eObject) {
		this.eObject = eObject;
	}

	@AboutToShow
	public void aboutToShow(List<MMenuElement> items) {
		items.clear();
		if (eObject == null)
			return;

		EditingDomain domain = editingDomainProvider.getEditingDomain();
		Collection<?> newChildDescriptors = domain.getNewChildDescriptors(
				eObject, null);

		if (newChildDescriptors != null) {
			for (Object descriptor : newChildDescriptors) {
				Command emfCommand = CreateChildCommand.create(domain,
						itemProvider, descriptor,
						Collections.singletonList(eObject));
				Object emfFeature = ((CommandParameter) descriptor)
						.getFeature();

				MHandledMenuItem menuItem = MMenuFactory.INSTANCE
						.createHandledMenuItem();
				menuItem.setLabel(emfCommand.getLabel());
				MCommand modeledCommand = (MCommand) modelService.find(
						"org.domainworkbench.command.createChild", app);
				menuItem.setCommand(modeledCommand);
				menuItem.setContributorURI("platform:/plugin/org.domainworkbench");
				MParameter parameter = MCommandsFactory.INSTANCE
						.createParameter();
				parameter
						.setName("org.domainworkbench.commandparameter.emfCommand");
				parameter.setValue(String.valueOf(emfCommand.hashCode()));
				menuItem.getParameters().add(parameter);
				parameter = MCommandsFactory.INSTANCE.createParameter();
				parameter
						.setName("org.domainworkbench.commandparameter.emfFeature");
				parameter.setValue(String.valueOf(emfFeature.hashCode()));
				menuItem.getParameters().add(parameter);

				Map<String, Object> parameters = new HashMap<String, Object>();
				parameters.put(
						"org.domainworkbench.commandparameter.emfCommand",
						emfCommand);
				parameters.put(
						"org.domainworkbench.commandparameter.emfFeature",
						emfFeature);
				ParameterizedCommand command = commandService.createCommand(
						"org.domainworkbench.command.createChild", parameters);
				menuItem.setWbCommand(command);
				items.add(menuItem);
			}
		}
	}
	

I also have a type-id, although it's convertToObject doesn't gets called:

public class ContextParameterValueConverter extends
		AbstractParameterValueConverter {

	protected IEclipseContext context;

	public ContextParameterValueConverter() {
		super();
		this.context = createContext();
	}

	private IEclipseContext createContext() {
		return context = EclipseContextFactory.create();

		// Bundle bundle = FrameworkUtil
		// .getBundle(ContextParameterValueConverter.class);
		// BundleContext bundleContext = bundle.getBundleContext();
		// IEclipseContext serviceContext = EclipseContextFactory
		// .getServiceContext(bundleContext);
		// return serviceContext.getActiveLeaf();
	}

	@Override
	public Object convertToObject(String parameterValue)
			throws ParameterValueConversionException {
		return context.get(parameterValue);
	}

	@Override
	public String convertToString(Object parameterValue)
			throws ParameterValueConversionException {
		String result = String.valueOf(parameterValue.hashCode());
		context.set(result, parameterValue);
		return result;
	}

}


The menu is shown correctly but the handler's execute method doesn't get called because the type of the parameter value is String.
I wonder why this type-id doesn't applies to the binding parameter, and why it is not possible to pass the object value to MParameter.setValue()?
I am ready to contribute.

[Updated on: Sat, 23 February 2013 21:32]

Report message to a moderator

 
Read Message
Read Message
Read Message
Previous Topic:Perspective Minimize/Maximize problem
Next Topic:OSGI DS vs DI via Contexts
Goto Forum:
  


Current Time: Tue Jun 18 03:31:52 EDT 2013

Powered by FUDForum. Page generated in 0.01618 seconds