0

I am trying to get reference to an MToolItem from my own Utility class so I can programmatically set its selected state. The problem is that I always seem to get back null. I know that I have the correct id, and similar code works from a handler class (by passing in the EModelService and MApplication in the execute method). Is it possible that the EModelService or MApplication is stale when I make the find() call? Is there a better way to do this?

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);
    }
}
greg-449
  • 109,219
  • 232
  • 102
  • 145
ekjcfn3902039
  • 1,673
  • 3
  • 29
  • 54

1 Answers1

0

Assuming the injected objects are not null, I would write something like this:

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);
}
psuzzi
  • 2,187
  • 1
  • 17
  • 21