|
|
|
Re: How to append a text node to a EObject [message #1676104 is a reply to message #1662254] |
Sun, 15 March 2015 06:25   |
Eclipse User |
|
|
|
Hi Bob,
the CData example works great and I have adapted it for my project.
But I still have a question:
In the CustomPropertyTabs Tutorial you explain that Model changes should only be made in response to a deliberate user action and not in the createBindings method. You example shows how a new MetaData extension Element is added by pressing a Button in the property section. But also in this example you put the code, which modifies the EObject into a TransactionalEditingDomain execute block.
So why is the following code - which did the same - not recommanded? I just want to verify that my Element contains a MetaData extension with the name 'txtSubject'. And it works without any problems.
public class MailPropertySection extends AbstractImixsPropertySection {
final static EStructuralFeature METADATA_FEATURE = ModelPackage.eINSTANCE.getDocumentRoot_MetaData();
final static EStructuralFeature METADATA_VALUE = ModelPackage.eINSTANCE.getMetaData_Value();
@Override
protected AbstractDetailComposite createSectionRoot() {
return new MailDetailComposite(this);
}
@Override
public AbstractDetailComposite createSectionRoot(Composite parent, int style) {
return new MailDetailComposite(parent, style);
}
public class MailDetailComposite extends AbstractDetailComposite {
public MailDetailComposite(AbstractBpmn2PropertySection section) {
super(section);
}
public MailDetailComposite(Composite parent, int style) {
super(parent, style);
}
@Override
public void createBindings(final EObject be) {
if (be==null || !(be instanceof IntermediateCatchEvent)) {
return;
}
setTitle("Mail Configuration");
// create a new Property Tab section with a twistie
Composite section = createSectionComposite(this, "Mail Body");
// get the MetaData object from this BaseElement's extension elements
MetaData metaData = (MetaData) findExtensionAttributeValue((BaseElement)be, METADATA_FEATURE,"txtSubject");
if (metaData==null) {
// create the new MetaData and insert it into the
// BaseElement's extension elements container
// Note that this has to be done inside a transaction
TransactionalEditingDomain domain = getDiagramEditor().getEditingDomain();
domain.getCommandStack().execute(new RecordingCommand(domain) {
@Override
protected void doExecute() {
MetaData metaData = ModelFactory.eINSTANCE.createMetaData();
metaData.setValue("Some Subject....");
metaData.setName("txtSubject");
addExtensionAttributeValue((BaseElement)be, METADATA_FEATURE, metaData);
setBusinessObject(be);
}
});
} else {
TextObjectEditor valueEditor = new TextObjectEditor(this, metaData, METADATA_VALUE);
// valueEditor.setMultiLine(true);
valueEditor.createControl(this, "Value");
}
}
/**
* Add a new extension element to the given BaseElement.
*/
void addExtensionAttributeValue(BaseElement be, EStructuralFeature feature, Object value) {
ExtensionAttributeValue eav = null;
if (be.getExtensionValues().size()>0) {
// reuse the <bpmn2:extensionElements> container if this BaseElement already has one
eav = be.getExtensionValues().get(0);
}
else {
// otherwise create a new one
eav = Bpmn2Factory.eINSTANCE.createExtensionAttributeValue();
be.getExtensionValues().add(eav);
}
eav.getValue().add(feature, value);
}
/**
* Find the first entry in this BaseElement's extension elements container
* that matches the given structural feature MetaData and the name.
*/
Object findExtensionAttributeValue(BaseElement be, EStructuralFeature feature,String itemName) {
for (ExtensionAttributeValue eav : be.getExtensionValues()) {
for (FeatureMap.Entry entry : eav.getValue()) {
if (entry.getEStructuralFeature() == feature) {
if (entry.getValue() instanceof MetaData) {
MetaData confItem=(MetaData) entry.getValue();
if (confItem.getName().equals(itemName))
return confItem;
//return entry.getValue();
}
}
}
}
return null;
}
}
}
===
Ralph
[Updated on: Sun, 15 March 2015 06:25] by Moderator
|
|
|
|
|
|
Re: How to append a text node to a EObject [message #1690513 is a reply to message #1690069] |
Fri, 27 March 2015 13:37   |
Eclipse User |
|
|
|
Hi Bob,
finally I got the trick with the InsertionAdapter.
Now my createBindings method looks like this and works perfect.
@Override
public void createBindings(final EObject be) {
if (be == null || !(be instanceof IntermediateCatchEvent)) {
return;
}
setTitle("Mail Configuration");
// create a new Property Tab section with a twistie
Composite section = createSectionComposite(this, "Mail Body");
// get the MetaData object from this BaseElement's extension
// elements
ConfigItem metaData = (ConfigItem) findExtensionAttributeValue(
(BaseElement) be, METADATA_FEATURE, "txtSubject");
if (metaData == null) {
// create the new MetaData and insert it into the
// BaseElement's extension elements container
metaData = ModelFactory.eINSTANCE.createConfigItem();
metaData.setValue("Some Subject....");
metaData.setName("txtSubject");
// addExtensionAttributeValue
ExtensionAttributeValue eav = null;
if (((BaseElement) be).getExtensionValues().size() == 0) {
// otherwise create a new one
eav = Bpmn2Factory.eINSTANCE
.createExtensionAttributeValue();
InsertionAdapter.add(eav, METADATA_FEATURE, metaData);
InsertionAdapter.add(be, Bpmn2Package.eINSTANCE
.getBaseElement_ExtensionValues(), eav);
} else {
// reuse the <bpmn2:extensionElements> container if this
// BaseElement already has one
eav = ((BaseElement) be).getExtensionValues().get(0);
}
}
TextObjectEditor valueEditor = new TextObjectEditor(this, metaData,
METADATA_VALUE);
// valueEditor.setMultiLine(true);
valueEditor.setStyle(SWT.MULTI | SWT.V_SCROLL);
valueEditor.createControl(this, "Value");
}
Thanks a lot!!
===
Ralph
|
|
|
|
Powered by
FUDForum. Page generated in 0.32591 seconds