Create slot programmatically when Classifier is set on InstanceSpecification [message #1412586] |
Tue, 26 August 2014 10:21  |
Eclipse User |
|
|
|
Hello everybody!
I'm trying to create a slot programmatically when the Classifier is set on an InstanceSpecification.
I couldn't find exactly where the classifier is added to the InstanceSpecification, so I tried to add the code to whatever part I could. So I added it to the InstanceSpecificationNameLabelEditPolicy class, in the notifyChanged function:
...
else if (notification.getFeature().equals(UMLPackage.eINSTANCE.getInstanceSpecification_Classifier())) {
addValueToQoSValue();
refreshDisplay();
}
...
private void addValueToQoSValue() {
InstanceSpecification instance = getUMLElement();
for (Classifier classifier : instance.getClassifiers()) {
Stereotype stereotype = classifier.getAppliedStereotypes().get(0);
if ("QoSCharacteristic".equals(stereotype.getName())) {
EList<Property> attrs = classifier.getAllAttributes();
for (Property prop : attrs) {
Slot slot = instance.createSlot();
slot.setDefiningFeature(prop);
}
}
}
}
With this I got the following exception:
java.lang.IllegalStateException: Cannot modify resource set without a write transaction
Then I modified my function as follows:
private void addValueToQoSValue() {
try {
ServicesRegistry serviceRegistry = ServiceUtilsForActionHandlers.getInstance().getServiceRegistry();
TransactionalEditingDomain ed = TransactionalEditingDomain.Factory.INSTANCE.createEditingDomain();
RecordingCommand c = new RecordingCommand(ed) {
@Override
protected void doExecute() {
InstanceSpecification instance = getUMLElement();
for (Classifier classifier : instance.getClassifiers()) {
Stereotype stereotype = classifier.getAppliedStereotypes().get(0);
if ("QoSCharacteristic".equals(stereotype.getName())) {
EList<Property> attrs = classifier.getAllAttributes();
for (Property prop : attrs) {
Slot slot = instance.createSlot();
slot.setDefiningFeature(prop);
}
}
}
}
};
ed.getCommandStack().execute(c);
} catch (ServiceException e) {
e.printStackTrace();
}
}
But now I'm getting this exception: org.eclipse.emf.transaction.RollbackException: Cannot modify resource set without a write transaction
So my questions are:
1- Is there a better way to detect when the classifier is selected so I could create the slot?
2- If not, how can I make my code work?
Thank you!
|
|
|
Re: Create slot programmatically when Classifier is set on InstanceSpecification [message #1414876 is a reply to message #1412586] |
Mon, 01 September 2014 09:47  |
Eclipse User |
|
|
|
Hi Sofia,
The exception is due to the Transaction framework, which prevents edition of the model outside a... write transaction, as the exception says.
The simplest way to start a Transaction is to use a Command and the Papyrus CommandStack. For example, if you have an EObject which is already attached to the Papyrus model, use:
TransactionalEditingDomain domain = ServiceUtilsForEObject.getInstance().getTransactionalEditingDomain(eObject);
CommandStack stack = domain.getCommandStack();
stack.execute(myCommand);
Papyrus implements an action when a Classifier is dropped on an InstanceSpecification, to create a set of slots. Have a look at this command to see how it is implemented:
Plugin org.eclipse.papyrus.uml.diagram.dnd:
org.eclipse.papyrus.uml.diagram.dnd.strategy.instancespecification.command.SelectAndCreateSlotsCommand
Regards,
Camille
|
|
|
Powered by
FUDForum. Page generated in 0.03680 seconds