Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Papyrus » How to apply stereotypes to programmatically generated elements
How to apply stereotypes to programmatically generated elements [message #717825] Mon, 22 August 2011 13:27 Go to next message
Neslepaks  is currently offline Neslepaks Friend
Messages: 37
Registered: April 2011
Member
In my recent post I showed how I managed to create elements to diagram programmatically:
http://www.eclipse.org/forums/index.php/mv/msg/220362/717730/#msg_717730

This time I'd need to apply a stereotype to a package I have generated using the way in my post. The stereotype is defined in a profile that is not programmatically created.

I've tried a few ways but, so far, they haven't worked.
For example this throws IllegalArgumentException:
final Stereotype st = profile.getOwnedStereotype("LicensedPackage");
...

//Get a generated package
final org.eclipse.uml2.uml.Package p = (org.eclipse.uml2.uml.Package) modelpackage.getOwnedElements().get(i);

//Inside a transaction, apply the stereotype to the package
ted = TransactionUtil.getEditingDomain(model);
ted.getCommandStack().execute(new RecordingCommand(ted) {
	protected void doExecute() {
		p.applyStereotype(st);
	}
});


So, how should I apply a stereotype to a package correctly?

[Updated on: Mon, 22 August 2011 13:33]

Report message to a moderator

Re: How to apply stereotypes to programmatically generated elements [message #718061 is a reply to message #717825] Tue, 23 August 2011 06:33 Go to previous messageGo to next message
Timothy Marc is currently offline Timothy MarcFriend
Messages: 547
Registered: July 2009
Senior Member
Neslepeaks,

this is the way, I successfully apply stereotypes programmatically:

1. Get the applicable stereotypes for a given metaclass, in your case p:
Stereotype st = p.getApplicableStereotype("prefix::LicensedPackage") //must
be the fully qualified name of the stereotype to apply;

2. Apply the stereotype:
p.applyStereotype(st);

You don't have to access the profile correctly. As soon as your profile is
applied to a nesting package, the stereotypes are accessible for all
contained children with the way I described above.

HTH,
Timothy

"Neslepaks" schrieb im Newsbeitrag news:j2tksj$2td$1@news.eclipse.org...

In my recent post I showed how I managed to create elements to diagram
programmatically:
http://www.eclipse.org/forums/index.php/mv/msg/220362/717730/#msg_717730

This time I'd need to apply a stereotype to a package I have generated using
the way in my post. The stereotype is defined in a profile that is not
programmatically created.

I've tried a few ways but, so far, they haven't worked.
For example this throws IllegalArgumentException:

final Stereotype st = profile.getOwnedStereotype("LicensedPackage");
...

//Get a generated package
final org.eclipse.uml2.uml.Package p = (org.eclipse.uml2.uml.Package)
modelpackage.getOwnedElements().get(i);

//Inside a transaction, apply the stereotype to the package
ted = TransactionUtil.getEditingDomain(model);
ted.getCommandStack().execute(new RecordingCommand(ted) {
protected void doExecute() {
p.applyStereotype(st);
}
});


So, how should I apply stereotype to a package correctly?
Re: How to apply stereotypes to programmatically generated elements [message #718136 is a reply to message #718061] Tue, 23 August 2011 11:32 Go to previous messageGo to next message
Neslepaks  is currently offline Neslepaks Friend
Messages: 37
Registered: April 2011
Member
OK. Thanks, now I got it working. It seems that I had also written my prefixes wrong earlier.
Re: How to apply stereotypes to programmatically generated elements [message #718842 is a reply to message #718136] Thu, 25 August 2011 13:33 Go to previous messageGo to next message
Neslepaks  is currently offline Neslepaks Friend
Messages: 37
Registered: April 2011
Member
I have defined enumerated values in my profile, so how it is possible to set enumerated tagged values for stereotyped element?
Re: How to apply stereotypes to programmatically generated elements [message #719186 is a reply to message #718842] Fri, 26 August 2011 09:20 Go to previous messageGo to next message
Timothy Marc is currently offline Timothy MarcFriend
Messages: 547
Registered: July 2009
Senior Member
Neslepeaks,

this depends on how you've defined your profile? If it is a static profile,
you can simply use the following snippet:


final Stereotype stereotype = element.getAppliedStereotype(stereotypeName);
//element is the element with the stereotype applied
final MySpecifcStereotype myStereo = (MySpecifcStereotype)
element.getStereotypeApplication(stereotype); //get the ECore instance for
your stereotype
myStereo.setEnum(MyEnum.myLiteral);

That should work.

In case you have defined you profile dynamically, you have to use the
reflection API of Ecore instead.

HTH
Timothy


"Neslepaks" schrieb im Newsbeitrag news:j35i9r$sdf$1@news.eclipse.org...

I have defined enumerated values in my profile, so how it is possible to set
enumerated tagged values for stereotyped element?
Re: How to apply stereotypes to programmatically generated elements [message #719855 is a reply to message #719186] Mon, 29 August 2011 08:38 Go to previous message
Neslepaks  is currently offline Neslepaks Friend
Messages: 37
Registered: April 2011
Member
I suppose it is static, as I have applied it this way: http://wiki.eclipse.org/Papyrus_User_Guide#Apply_a_static_profile

Still, I don't understand the snippet. Maybe I didn't explain my problem clearly enough.

For example, my profile has an enumeration ColorType with possible values "Black" and "White",
and a stereotype ColoredPackage which has tagged value Color of type ColorType.
Then I'd like to set programmatically that Color to value "Black".

So, how should I define the MySpecificStereotype and do I need to implement the setEnum myself?
How about MyEnum?

Timothy Marc wrote on Fri, 26 August 2011 05:20
Neslepeaks,

this depends on how you've defined your profile? If it is a static profile,
you can simply use the following snippet:


final Stereotype stereotype = element.getAppliedStereotype(stereotypeName);
//element is the element with the stereotype applied
final MySpecifcStereotype myStereo = (MySpecifcStereotype)
element.getStereotypeApplication(stereotype); //get the ECore instance for
your stereotype
myStereo.setEnum(MyEnum.myLiteral);

That should work.

In case you have defined you profile dynamically, you have to use the
reflection API of Ecore instead.

HTH
Timothy


"Neslepaks" schrieb im Newsbeitrag news:j35i9r$sdf$1@news.eclipse.org...

I have defined enumerated values in my profile, so how it is possible to set
enumerated tagged values for stereotyped element?

[Updated on: Mon, 29 August 2011 08:39]

Report message to a moderator

Previous Topic:Adding a propperty tab to Papyrus does not work
Next Topic:CallEvent to trigger stateMachine transition ?
Goto Forum:
  


Current Time: Mon Dec 22 16:35:07 GMT 2014

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

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