How to apply stereotypes to programmatically generated elements [message #717825] |
Mon, 22 August 2011 13:27 |
Neslepaks 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 |
Timothy Marc 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 #719855 is a reply to message #719186] |
Mon, 29 August 2011 08:38 |
Neslepaks 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:20Neslepeaks,
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
|
|
|
Powered by
FUDForum. Page generated in 0.02409 seconds