[英]Adding to an emf model
我有一个基于电动势的模型。 在模型中,我有一个要素类,可以在要素中添加约束。 例如功能A“ IMPLIES”功能B。我正在尝试使用emf命令堆栈向功能添加约束。 它将约束添加到要素,但缺少属性。 我的代码如下
public static Object doExecute(Feature contextFeature, FeatureModel featureModel, ComposedAdapterFactory adapterFactory) {
CreateConstraintDialog dlg = new CreateConstraintDialog(Display.getCurrent().getActiveShell(), contextFeature, featureModel, adapterFactory);
dlg.open();
// check if dialog was cancelled:
if (dlg.getReturnCode() == Window.CANCEL)
return null;
Feature selectedFeature = dlg.getSelectedFeature();
if (selectedFeature == null)
return null;
ConstraintType selectedConstraintType = dlg.getSelectedConstraintType();
Constraint constraint = FmFactory.eINSTANCE.createConstraint();
constraint.setType(selectedConstraintType);
constraint.setConstrainedFeature(selectedFeature);
constraint.setContext(contextFeature);
EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(contextFeature);
Command cmd = AddCommand.create(editingDomain, contextFeature, FmPackage.FEATURE__CONSTRAINTS, constraint);
editingDomain.getCommandStack().execute(cmd);
return null;
}
编辑
当我删除constraint.setContext(contextFeature)时; 从上面的代码中,编辑器收到有关更改的通知(即,向功能部件添加了新约束),但是缺少上下文属性,因为未设置该属性。
setContext方法如下
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public void setContext(Feature newContext) {
if (newContext != eInternalContainer() || (eContainerFeatureID() != FmPackage.CONSTRAINT__CONTEXT && newContext != null)) {
if (EcoreUtil.isAncestor(this, newContext))
throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
NotificationChain msgs = null;
if (eInternalContainer() != null)
msgs = eBasicRemoveFromContainer(msgs);
if (newContext != null)
msgs = ((InternalEObject)newContext).eInverseAdd(this, FmPackage.FEATURE__CONSTRAINTS, Feature.class, msgs);
msgs = basicSetContext(newContext, msgs);
if (msgs != null) msgs.dispatch();
}
else if (eNotificationRequired())
eNotify(new ENotificationImpl(this, Notification.SET, FmPackage.CONSTRAINT__CONTEXT, newContext, newContext));
}
上面的代码向功能添加了约束,但是缺少上下文。 任何想法
谢谢
刚创建constraint
实例后,由于尚未附加到EMF模型,因此无需使用命令来设置其属性。 您可以只调用setter方法。 您将使用命令的地方仅仅是向现有功能添加constraint
。
无关,但是在执行命令之前,还应该始终在命令上调用canExecute
方法:
CompoundCommand cmd = ....;
if (cmd.canExecute()) {
editingDomain.getCommandStack().execute( cmd );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.