|
Re: Contextual menu according to Stereotype of the selected element [message #1778291 is a reply to message #1778288] |
Tue, 12 December 2017 16:48 |
Camille Letavernier Messages: 952 Registered: February 2011 |
Senior Member |
|
|
Hi Yoann,
It is absolutely possible, although default Eclipse Property Testers may not be sufficient to specify your "visibleWhen" expression. Also, I don't think that Papyrus provides this Property Test either, so you may need to implement your own.
You need to:
- Contribute (and implement) a new IPropertyTester, via the org.eclipse.core.expressions.propertyTesters extension point:
- In the property tester code, find the selected UML Element (Using org.eclipse.papyrus.uml.tools.utils.UMLUtil.resolveUMLElement(Object) may help you to retrieve an EObject from a selected Tree Element or Diagram part)
- Once you have the selected UML Element, iterate over its applied stereotypes (Via Element#getAppliedStereotype(String), or Element#getAppliedStereotypes(), or Element#getStereotypeApplications(), ...)
- If you find your stereotype, then return true
- Then, you can reference this property tester from your Eclipse UI Command/Menu/Handler visibleWhen/activeWhen/enableWhen expressions, as usual
HTH,
Camille
Camille Letavernier
|
|
|
|
Re: Contextual menu according to Stereotype of the selected element [message #1778428 is a reply to message #1778288] |
Thu, 14 December 2017 08:29 |
Yoann Farré Messages: 235 Registered: November 2017 |
Senior Member |
|
|
If someone is interested by a such feature, this is the code of the tester I use (working code).
public class StereotypeTester extends PropertyTester {
/**
* id for the property to test
*/
public static final String STEREOTYPE_ID = "stereotype"; //$NON-NLS-1$
/**
*
* @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object,
* java.lang.String, java.lang.Object[], java.lang.Object)
*
* @param receiver
* @param property
* @param args
* @param expectedValue
* @return
*/
@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
if (STEREOTYPE_ID.equals(property) && receiver instanceof IStructuredSelection) {
Object object = ((IStructuredSelection) receiver).getFirstElement();
EObject eObject = UMLUtil.resolveUMLElement(object);
if (eObject instanceof Element) {
return testStereotype((Element) eObject, expectedValue);
}
}
return false;
}
/**
* Test the stereotype
*
* @param element
* the current element
* @param expectedValue
* the expected value : the wanted stereotype
* @return <code>true</code> if the current element has the wanted stereotype
* applied
*/
private boolean testStereotype(Element element, Object expectedValue) {
EList<Stereotype> stereotypeList = element.getAppliedStereotypes();
for (Stereotype stereotype : stereotypeList) {
if (expectedValue.equals(stereotype.getQualifiedName())) {
return true;
}
}
return false;
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.03083 seconds