У меня есть модель на основе emf. В модели у меня есть класс объектов и функции, которые я могу добавить ограничения. например, функция A "IMPLIES". Я пытаюсь добавить ограничение к функции с использованием стека команд 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 );
}