|
Re: Context menu entries via standard extension points [message #754878 is a reply to message #754832] |
Fri, 04 November 2011 17:42 |
Georg Hackenberg 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
|
|
|
|
Powered by
FUDForum. Page generated in 0.02557 seconds