RCP - 应用程序没有活动窗口

时间:2015-03-09 19:04:17

标签: java rcp e4

在自定义服务中获取非陈旧的EPartService / EModelService / MApplication引用的最佳方法是什么?我知道它可以通过@Execute方法在Handler中完成,但我没有看到在自定义服务类中执行此操作的方法。任何帮助将不胜感激。

@Singleton
@Creatable
public class MyService {

    @Inject
    private EPartService partService;
    @Inject
    private EModelService modelService;
    @Inject
    private MApplication application;

    @Inject
    public MyService(final IEclipseContext context) {
        context.set(MyService.class.getName(), this);
    }

    public void doWork(){
     // Application does not have an active window
    }
}

2 个答案:

答案 0 :(得分:3)

使用活动叶子上下文:

IEclipseContext activeContext = application.getContext().getActiveLeaf();

EPartService partService = activeContext.get(EPartService.class);

我认为模型和零件服务实际上并没有改变所以只使用application.getContext()也应该没问题。

应用程序对象不会更改。

答案 1 :(得分:0)

发生此问题的原因是为每个窗口创建了EPartService,并且当您注释服务时,@ Singleton EPartService仅向您的服务注入一次,并且当您更改窗口时EPartService未更新。所以删除注释@Singleton,一切都会好的。