【问题标题】:RCP - Application does not have an active windowRCP - 应用程序没有活动窗口
【发布时间】:2015-12-01 16:25:23
【问题描述】:

在自定义服务中获取非陈旧 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
    }
}

【问题讨论】:

    标签: java rcp e4


    【解决方案1】:

    使用活动叶上下文:

    IEclipseContext activeContext = application.getContext().getActiveLeaf();
    
    EPartService partService = activeContext.get(EPartService.class);
    

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

    应用程序对象没有改变。

    【讨论】:

    • 感谢您的回复!当您说“我认为模型和零件服务实际上没有改变”和“应用程序对象没有改变”时,我有点困惑。你是说这些物品都不会过时吗?如果是这样,我的示例代码中的注入和代码中的活动叶之间的根本区别是什么?
    • 对于这些特定的服务,我认为没有区别。像 EHandlerService 和 EContextService 这样的东西确实会改变,所以你需要为它们使用活动叶。这还取决于您的服务在启动时创建的时间,如果是在应用程序启动完成之前,您可能无法注入所有内容。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-06
      • 2017-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多