Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Papyrus » Contextual menu according to Stereotype of the selected element
Contextual menu according to Stereotype of the selected element [message #1778288] Tue, 12 December 2017 16:28 Go to next message
Yoann Farré is currently offline Yoann FarréFriend
Messages: 49
Registered: November 2017
Member
Hello,

I would create a contextual menu according to the Stereotype applied on an element. This menu will be used to execute some actions which are not the creation of new elements in the model.

I know how to create a such contextual menu using handlers and commands on a UML::Package or UML::Class for example but I don't know how to extend this principle to the stereotypes.

Is it possible? And how could I do that?

Thank you for reading!

Yoann.
Re: Contextual menu according to Stereotype of the selected element [message #1778291 is a reply to message #1778288] Tue, 12 December 2017 16:48 Go to previous messageGo to next message
Camille Letavernier is currently offline Camille LetavernierFriend
Messages: 885
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 #1778323 is a reply to message #1778291] Wed, 13 December 2017 07:40 Go to previous messageGo to next message
Yoann Farré is currently offline Yoann FarréFriend
Messages: 49
Registered: November 2017
Member
Hi Camille,

Thank you! This is really helpful. I will try to implement this solution!

Yoann.
Re: Contextual menu according to Stereotype of the selected element [message #1778428 is a reply to message #1778288] Thu, 14 December 2017 08:29 Go to previous message
Yoann Farré is currently offline Yoann FarréFriend
Messages: 49
Registered: November 2017
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;
	}

}


Previous Topic:Papyrus Neon (2.0.X) RCP
Next Topic:Persistence of StereotypeShapes
Goto Forum:
  


Current Time: Wed Sep 26 12:21:46 GMT 2018

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

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

Back to the top