【发布时间】:2015-05-30 05:38:57
【问题描述】:
我有一个基于 emf 的模型。在模型中,我有一个要素类,并且可以向这些要素添加约束。例如功能 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));
}
上面的代码为特征添加了约束,但是上下文丢失了。任何想法
谢谢
【问题讨论】:
标签: java eclipse-plugin eclipse-emf emf