2

谁能告诉我如何使用AddCommand而不是“SetCommand”来执行以下操作。

我有这样的课:

class Profile {
    List achievements;
    List grades;
    List extracurrics;
}

现在,假设我需要向这个 Profile 对象添加一个等级对象,我怎样才能通过AddCommand仅使用来实现这一点

4

2 回答 2

2

SetCommand is basically used to set values in EMF model, and AddCommand is used to modify collection values inside EMF model, so in general it should not be a problem to use AddCommand.

You can create new AddCommand using static creation function in AddCommand:

AddCommand.create(EditingDomain domain, EObject owner, EStructuralFeature feature, java.lang.Object value) 

Explanation of given values:

domain: the editing domain your model lives in
owner: element you are doing the modifications to
feature: feature in model, that should be given to you by the EPackage of your model.
         So this case is the Grades list feature
value: the new object you add to the list

There are many different create helpers in add command, so if you need to define index to list, it is also doable.

I don't have EMF running here, so I cannot provide any direct sources, but let me know if that didn't do the trick.

于 2011-02-11T16:46:07.697 回答
1

它应该看起来像这样:

Profile p = ...;
Grade g = ...;
Command add = AddCommand.create(domain,p, YourProfilePackage.Literals.PROFILE__GRADES, Collections.singleton(g));

YourProfilePackage 应该在您的 EMF 模型自动生成的代码中。

于 2011-02-14T11:52:47.400 回答