我试图从我自己的Utility类引用一个MToolItem,这样我就可以通过编程方式设置它的选择状态。问题是我似乎总是回到null。我知道我有正确的id,类似的代码在处理程序类中工作(通过在execute方法中传入EModelService和MApplication)。当我进行find()调用时,EModelService或MApplication是否可能过时?有更好的方法吗?
public class MyUtilityClass {
@Inject
private EModelService modelService;
@Inject
private MApplication app;
private void toggle(final boolean selected) {
MToolItem toolItem = (MToolItem) modelService.find("my.tool.id", app);
// toolItem is always null
if (toolItem != null) {
toolItem.setSelected(selected);
}
// I have also tried to find it via way below but it also doesn't work
final List<MToolItem> toolItems = modelService.findElements(
app, "my.tool.id", MToolItem.class,
new ArrayList<String>(), EModelService.ANYWHERE);
}
}
答案 0 :(得分:0)
假设注入的对象不为null,我会写这样的东西:
private void toggle(final boolean selected) {
MWindow window = (MWindow) app.getChildren().get(0);
List<MToolItem> toolItems = modelService.findElements(window, "my.tool.id",
MToolItem.class,
null,
EModelService.OUTSIDE_PERSPECTIVE
| EModelService.IN_ANY_PERSPECTIVE
| EModelService.IN_SHARED_AREA);
if(toolItems.isEmpty()){
return; // no item found
}
// exec your code
toolItems.get(0).setSelected(selected);
}