Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Graphiti » Context menu entries via standard extension points(Allow to add functionality to context menus through Eclipse extension mechanism.)
Context menu entries via standard extension points [message #754832] Fri, 04 November 2011 14:23 Go to next message
Georg Hackenberg is currently offline Georg HackenbergFriend
Messages: 12
Registered: October 2011
Junior Member
Dear all,

I was wondering if there is a standard way to add context menu entries to Graphiti diagram editors via the Eclipse extension mechanism. The reason is that I have domain/business model processing logic in separate plugins and I would like to attach this logic to the diagram editor.

Thanks for your help and best regards,

Georg
Re: Context menu entries via standard extension points [message #754878 is a reply to message #754832] Fri, 04 November 2011 17:42 Go to previous messageGo to next message
Georg Hackenberg is currently offline Georg HackenbergFriend
Messages: 12
Registered: October 2011
Junior Member
I managed to integrate the Eclipse popupMenu extensions with the Graphiti context menu. Therefore I first implemented a FeatureProvider as follows:

	public ICustomFeature[] getCustomFeatures(ICustomContext context)
	{
		Collection<ICustomFeature> features = new ArrayList<ICustomFeature>();
		
		IConfigurationElement[] configuration = Platform.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.ui.popupMenus");
	
		for (IConfigurationElement element : configuration)
		{
			String objectClass = element.getAttribute("objectClass");
			
			if (element.getName().equals("objectContribution") && objectClass.startsWith("edu.tum.cs.mes.ifedit."))
			{
				for (IConfigurationElement action : element.getChildren())
				{
					String actionClass = action.getAttribute("class");
					String actionLabel = action.getAttribute("label");
					
					try
					{
						Bundle objectBundle = Platform.getBundle("edu.tum.cs.mes.ifedit.emf.model");
						Bundle actionBundle = Platform.getBundle(action.getContributor().getName());
	
						IObjectActionDelegate delegate = (IObjectActionDelegate) actionBundle.loadClass(actionClass).newInstance();
						
						features.add(new ActionFeature(objectBundle.loadClass(objectClass), actionLabel, "", delegate, this));
					}
					catch (ClassNotFoundException e)
					{
						e.printStackTrace();
					}
					catch (InstantiationException e)
					{
						e.printStackTrace();
					}
					catch (IllegalAccessException e)
					{
						e.printStackTrace();
					}
				}
			}
		}
		
		return features.toArray(new ICustomFeature[] {});
	}


The ActionFeature looks as follows:

public class ActionFeature extends AbstractCustomFeature
{
	
	private Class<?> clazz;
	private String name;
	private String description;
	private IObjectActionDelegate delegate;
	
	public ActionFeature(Class<?> clazz, String name, String description, IObjectActionDelegate delegate, IFeatureProvider fp)
	{
		super(fp);
		
		this.clazz = clazz;
		this.name = name;
		this.description = description;
		this.delegate = delegate;
	}
	
	@Override
	public String getName()
	{
		return name;
	}
	
	@Override
	public String getDescription()
	{
		return description;
	}
	
	@Override
	public boolean canExecute(ICustomContext context)
	{
		PictogramElement[] elements = context.getPictogramElements();
		
		if (elements != null && elements.length == 1)
		{
			Object object = getBusinessObjectForPictogramElement(elements[0]);
			
			if (clazz.isAssignableFrom((object.getClass())))
				return true;
		}
		
		return false;
	}

	@Override
	public void execute(ICustomContext context)
	{
		PictogramElement[] elements = context.getPictogramElements();
		
		if (elements != null && elements.length == 1)
		{
			Object object = getBusinessObjectForPictogramElement(elements[0]);
			
			if (clazz.isAssignableFrom(object.getClass()))
			{
				delegate.selectionChanged(null, new StructuredSelection(object));
				delegate.run(null);
			}
			else
			{
				throw new IllegalStateException("Business object type not supported: " + object.getClass().getName());
			}
		}
	}
	
	@Override
	public boolean hasDoneChanges()
	{
		return false;
	}

}


I am not entirely sure about the implications of this solution, which is why I wanted to ask you guys about your opinion.

Best,
Georg
Re: Context menu entries via standard extension points [message #755053 is a reply to message #754878] Mon, 07 November 2011 08:45 Go to previous message
Michael Wenz is currently offline Michael WenzFriend
Messages: 1931
Registered: July 2009
Location: Walldorf, Germany
Senior Member
This should work, but you're re-programming the extension point evalution in
some kind...

Did you have a look at
http://www.eclipse.org/articles/article.php?file=Article-action-contribution/index.html#example8
? The ID of the Graphiti Diagram Editor is
org.eclipse.graphiti.ui.editor.DiagramEditor.

Michael
Previous Topic:ContainerShape as parent of a connection
Next Topic:GFPropertySection: Control TableViewer height
Goto Forum:
  


Current Time: Tue Apr 16 05:41:07 GMT 2024

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

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

Back to the top