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 next 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

Re: Dynamic menu with MHandledMenuItem [message #1013292 is a reply to message #1013263] Sat, 23 February 2013 03:18 Go to previous messageGo to next message
Kirill Zotkin is currently offline Kirill Zotkin
Messages: 11
Registered: July 2009
Junior Member
	@Inject
	protected IEclipseContext context;
	...

				parameter
						.setName("org.domainworkbench.commandparameter.emfCommand");
				context.set("org.domainworkbench.commandparameter.emfCommand",
						emfCommand);
				// parameter.setValue(String.valueOf(emfCommand.hashCode()));


Didn't helped in this example too. Anyway, i understand that this was the wrong way.

UPD:
With String handler arg type it works (type-id is registered against a java.lang.Object).

[Updated on: Sat, 23 February 2013 05:22]

Report message to a moderator

Re: Dynamic menu with MHandledMenuItem [message #1013582 is a reply to message #1013292] Sat, 23 February 2013 21:21 Go to previous message
Kirill Zotkin is currently offline Kirill Zotkin
Messages: 11
Registered: July 2009
Junior Member
Ok, to summarize:
	@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());
				menuItem.setContributorURI("platform:/plugin/org.domainworkbench");
				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);
			}
		}
	}


Here's how it works for String parameter type:
	@Execute
	public void execute(
			@Named("org.domainworkbench.commandparameter.emfCommand") String emfCommand,// Command
																						// emfCommand,
			@Named("org.domainworkbench.commandparameter.emfFeature") Object feature
	) {
		System.out.println(emfCommand + "\n\t" + feature.toString());
	}

Without type-id you need to pass Strings in commandService.createCommand(...parameters).
Althoug i still suspect that there is another way with MParameters.

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

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


	@Execute
	public void execute(
			@Named("org.domainworkbench.commandparameter.emfCommand") Command emfCommand,
			@Named("org.domainworkbench.commandparameter.emfFeature") Object feature) {
		emfCommand.execute();
		//...
		System.out.println(emfCommand.getResult() + " " + feature);

	}


Since it looks like MParameter's value is not used, i have added the call of convertToObject to the ParameterizedCommand.getParameterMap() in order to get it working. The bug is filed at https://bugs.eclipse.org/bugs/show_bug.cgi?id=401611

[Updated on: Sun, 24 February 2013 03:35]

Report message to a moderator

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


Current Time: Mon Jul 28 04:32:31 EDT 2014

Powered by FUDForum. Page generated in 0.02037 seconds