Java Eclipse4-将部件添加到表单

Java Eclipse4-将部件添加到表单,java,eclipse,eclipse-plugin,eclipse-pde,Java,Eclipse,Eclipse Plugin,Eclipse Pde,如果您懒得阅读所有这些内容,那么通常的问题是:假设这些部件是由Eclipse而不是手动创建的,如何将复合父级传递给现有部件 我正在开发一个Eclipse4插件。我在单独的类中有不同的部分视图,这些类通过事件进行通信。一切都很好,直到我必须实现一个包含2个部分的SashForm。我见过一些例子,但它们大多展示了SashForm的简单用法。 我想要的是通过注释接收所有依赖项注入和自动事件,从而保持这种模块化。 但我不知道该怎么做。 我最好的尝试是这样使用IEclipseContext: @Injec

如果您懒得阅读所有这些内容,那么通常的问题是:假设这些部件是由Eclipse而不是手动创建的,如何将复合父级传递给现有部件

我正在开发一个Eclipse4插件。我在单独的类中有不同的部分视图,这些类通过事件进行通信。一切都很好,直到我必须实现一个包含2个部分的SashForm。我见过一些例子,但它们大多展示了SashForm的简单用法。 我想要的是通过注释接收所有依赖项注入和自动事件,从而保持这种模块化。 但我不知道该怎么做。 我最好的尝试是这样使用IEclipseContext:

@Inject
private EventPool eventPool;

@Inject
private EventBroker eventBroker;

@Inject
private CMTreeContentProvider contentProvider;

@Inject
private LoginService loginService;

@PostConstruct
public void createPartControl(Composite shell) {
    SashForm form = new SashForm(shell, SWT.HORIZONTAL);
    form.setLayout(new FillLayout());
    renderCMTree(form);
    renderProjects(form);
}

private void renderCMTree(SashForm form) {
    Composite child = new Composite(form, SWT.NONE);
    child.setLayout(new FillLayout());
    IEclipseContext context = EclipseContextFactory.create();
    context.set(Composite.class, child);
    context.set(EventPool.class, eventPool);
    context.set(EventBroker.class, eventBroker);
    context.set(CMTreeContentProvider.class, contentProvider);
    ContextInjectionFactory.make(CMTreeView.class, context);
    context.dispose();
}

private void renderProjects(SashForm form) {
    Composite child = new Composite(form, SWT.NONE);
    child.setLayout(new FillLayout());
    IEclipseContext context = EclipseContextFactory.create();
    context.set(Composite.class, child);
    context.set(EventPool.class, eventPool);
    context.set(EventBroker.class, eventBroker);
    ContextInjectionFactory.make(CMTicketView.class, context);
    context.dispose();
}

如您所见,我必须通过上下文手动注入所有依赖项。使用这种方法,事件消费在CMTreeView和CMTicketView中不起作用,但我可以手动订阅事件。我可以接受,但也许有更好的方式保持模块化?我真的不希望这个类的行数超过2000行。

您应该尝试在Application.e4xmi中使用类似“Part Sash Container”的内容来包含这些部分

如果必须使用ContextInjectionFactory.make创建内容,则可以使用使用两个IEclipseContext参数在主上下文中传递的表单,以及仅包含您的值的辅助上下文:

@Inject
IEclipseContext context; // injected existing context


IEclipseContext staticContext = EclipseContextUtil.createContext();

// Only need to put your own values in staticContext
staticContext.set(Composite.class, child);

// Pass in both contexts to make
ContextInjectionFactory.make(CMTreeView.class, context, staticContext);
这两个上下文都将用于查找CMTreeView注入所需的值


注意:您应该使用IEventBroker而不是EventBroker,这是一个内部类。

为什么不在Application.e4xmi中使用“Part Sash Container”来包含这两个部分?@greg-449感谢您的评论。我试图使用它,但无法使其工作。我看到的所有关于PartSashContainer的示例似乎都使用RCP模型,但我开发了一个插件。是否可以将PartSashContainer添加为视图,以便通过窗口->显示视图进行访问?我使用Feautre Name“descriptors”将视图添加到模型片段中。我可以用同样的方法添加PartSashContainer吗?谢谢你的回答。但我没有Application.e4xmi文件,因为我没有开发RCP应用程序,而是开发插件,所以我只有模型片段。我想我最好从我的帖子中删除“EclipseRCP”标签。我一定会尝试一下,因为我非常喜欢e4风格的注入和单一责任。根据主应用程序的设计,您可能仍然可以在片段中使用part sash容器。e4xmi。答案的其余部分并不取决于使用零件窗扇容器。