Emf写交易

时间:2016-06-30 04:57:10

标签: eclipse eclipse-emf eclipse-emf-ecore

如果我尝试通过事务编辑域I am getting exception as修改该资源文件,那么我在资源管理器中有资源文件

  

java.lang.IllegalStateException:无法修改资源集   写交易   org.eclipse.emf.transaction.impl.TransactionChangeRecorder.assertWriting(TransactionChangeRecorder.java:348)     在   org.eclipse.emf.transaction.impl.TransactionChangeRecorder.appendNotification(TransactionChangeRecorder.java:302)     在   org.eclipse.emf.transaction.impl.TransactionChangeRecorder.processObjectNotification(TransactionChangeRecorder.java:284)     在   org.eclipse.emf.transaction.impl.TransactionChangeRecorder.notifyChanged(TransactionChangeRecorder.java:240)     在   org.eclipse.emf.common.notify.impl.BasicNotifierImpl.eNotify(BasicNotifierImpl.java:374)     在   org.eclipse.emf.common.notify.impl.NotificationImpl.dispatch(NotificationImpl.java:1027)     在   org.eclipse.emf.common.notify.impl.NotifyingListImpl.addUnique(NotifyingListImpl.java:299)     在   org.eclipse.emf.common.util.AbstractEList.add(AbstractEList.java:303)

1 个答案:

答案 0 :(得分:3)

我认为问题在于,您正在尝试在另一个写入事务中执行写入事务。命令应该可以解决问题。这可以使用模型的EditingDomain来完成:(确保org.eclipse.emf.transaction在您的依赖项中)

import org.eclipse.emf.transaction.TransactionalEditingDomain; 
import org.eclipse.emf.transaction.util.TransactionUtil;

public void doEditing(EObject element) {
    // Make sure your element is attached to a resource, otherwise this will return null
    TransactionalEditingDomain domain = TransactionUtil.getEditingDomain(element);
    domain.getCommandStack().execute(new RecordingCommand(domain) {

        @Override
        protected void doExecute() {
            // Implement your write operations here,
            // for example: set a new name
            element.eSet(element.eClass().getEStructuralFeature("name"), "aNewName");
        }
    });
}