2011-09-20 87 views
0

我正在使用TransactionalEditingDomain來管理模型上的更改。但是,我在創建空模型時遇到了一些問題。我認爲問題出在我將模型添加到模型Resource(modelResource.getContents().add(model);)時,因爲它應該放在事務中。因此,我試圖使用AddCommand來執行此操作,但我無法爲資源的contents找到EStructuralFeature使用命令設置EObject的資源

換句話說,我希望寫類似:

Command cmd = AddCommand.create(editingDomain, modelResource, FEAT_CONTENTS, model); 
commandStack.execute(cmd); 

的問題是,我無法找到FEAT_CONTENTS ......沒有任何人有一個建議?

回答

2

我已經找到了「官方」的解決方案與上Eclipse Forum of EMF使用AddCommand:

Command cmd = new AddCommand(editingDomain, modelResource.getContents(), model); 
commandStack.execute(cmd); 

由於去除根對象也是非平凡的,RemoveCommand可以使用相同的方法:

Command cmd = new RemoveCommand(editingDomain, modelResource.getContents(), model); 

最後,爲了完整起見,您還應該知道DeleteCommand(也會刪除對已刪除對象的所有引用)根本無法用於根對象。

+0

太棒了!我不能測試它,因爲我搬到了不同的項目。 – Matteo

0

我發現了一個解決方案,但真誠的我不喜歡它:

commandStack.execute(new RecordingCommand(editingDomain) { 
    protected void doExecute() { 
     modelResource.getContents().add(model); 
    } 
});