【问题标题】:Eclipse RCP Inject into instance of IWizardEclipse RCP 注入 IWizard 实例
【发布时间】:2016-12-02 23:50:37
【问题描述】:

我正在尝试注入一些我的向导的字段。

我可以使用以下帮助类成功地注入我自己的 OSGi DS:

public class UtilRCP {

public static void inject(Plugin plugin, Object object) {

IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(plugin.getBundle().getBundleContext());
ContextInjectionFactory.inject(object, serviceContext);
    }
}

未能注入来自 RCP 生态系统的其他服务,例如 PartService 或 MApplication(null / 未找到参数“MApplication”的实际值)。

这里是代码

public class MyWizard extends MyAbstractWizard implements IImportWizard {

    private MyWizardPage page;
    @Inject
    private EPartService partService;
    @Inject
    private DatabaseProvider databaseProvider;
    @Inject
    private MApplication application;

    public MyWizard() {
        System.err.println("Create");
        System.err.println(databaseProvider);
        System.err.println(partService);
        System.err.println(application);
    }

    @Override
    public void init(IWorkbench workbench, IStructuredSelection selection) {

        UtilRCP.inject(Activator.getDefault(), this);
        System.err.println("Init");
        System.err.println(databaseProvider);
        System.err.println(partService);
        System.err.println(application);
    }

    @Override
    public void addPages() {

        super.addPages();
        page = new MyWizardPage();
        addPage(page);
    }

    @Override
    public boolean performFinish() {

        return true;
    }
}

【问题讨论】:

    标签: eclipse dependency-injection eclipse-rcp


    【解决方案1】:

    服务上下文内容非常有限,不适合这样使用。

    在像这样的 3.x 样式向导中,您可以使用以下方法从 IWorkbench 对象获取工作台上下文:

    @Override
    public void init(IWorkbench workbench, IStructuredSelection selection) {
    
      IEclipseContext context = (IEclipseContext)workbench.getService(IEclipseContext.class);
    

    请注意,当对话框处于活动状态时,没有活动的“部分”(因为对话框不是部分)。这可能会导致各种 API 出现问题。特别是应用程序(工作台)部分服务会给出一个异常,抱怨没有活动部分。

    您可以通过显式获取顶级窗口的部件服务来获得工作部件服务:

    @Inject
    MApplication application;
    @Inject
    EModelService modelService;
    
    MWindow window = (MWindow)modelService.find("top level window id", application);
    
    EPartService partService = window.getContext().get(EPartService.class);
    

    我相信 3.x RCP 的顶级窗口 ID 是“IDEWindow”。

    如果它是 E4 应用程序,您可以在 Application.e4xml 中找到 主窗口 id

    【讨论】:

    • 这就像一个魅力,非常感谢!您能否解释一下为什么通过调用 IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(plugin.getBundle().getBundleContext()); 来获取上下文是不同的?和 IEclipseContext serviceContext = workbench.getService(IEclipseContext.class);
    • 使用 PartService 时,出现以下异常: java.lang.IllegalStateException: Application does not have an active window at org.eclipse.e4.ui.internal.workbench.ApplicationPartServiceImpl.getActiveWindowService(ApplicationPartServiceImpl .java:43)
    • 有许多不同的上下文,每个部分一个,一个应用程序和许多其他上下文。服务上下文是 OSGi 服务的专用上下文,不适合其他用途。
    • 当对话框处于活动状态时,没有活动部分。应用程序部分服务在这种情况下不起作用,因为它需要委托给活动部分。我在答案中添加了一些代码,可以找到应该工作的主窗口部分服务(我只在纯 e4 应用程序中使用了此代码,而不是 3.x 应用程序)。
    • 顺便说一句,我怎样才能以“e4 方式”做到这一点?我们的应用程序是基于 e4 的。
    猜你喜欢
    • 2021-01-05
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 2016-12-04
    • 2018-02-26
    • 2011-04-29
    • 2012-06-04
    • 2013-01-18
    相关资源
    最近更新 更多