在e3应用程序中获取e4应用程序模型

时间:2014-06-02 15:01:38

标签: eclipse rcp e4

我正在考虑从基于Eclipse 3.x的大型应用程序中开始使用Eclipse RCP e4工具。出于这个原因,我没有e4xmi文件。

应用程序模型确实存在于兼容层下面,但是很难从代码中获取它(请注意,您可以使用依赖项注入来获取所有内容,但只有在您说服框架开始为您创建对象之后)

这是获得我能找到的模型的唯一方法:

PartSite ps = (PartSite)PlatformUI.getWorkbench().getActiveWorkbenchWindow()
              .getActivePage().getActivePart().getSite();
IEclipseContext iec = ps.getContext();
MApplication ma = iec.get(MApplication.class);

暂时忽略PartSite是内部API的事实,是否有更简单的方法来掌握模型?为什么这么难找?

2 个答案:

答案 0 :(得分:1)

要做你想做的事,你可以使用这个片段。

    IWorkbench workbench = getService(IWorkbench.class, IWorkbench.class);
    MApplication mApplication = workbench.getApplication();
    EModelService modelService = mApplication.getContext().get(EModelService.class);

这是我从OSGi获得服务的方式

private <T> T getService(Class<T> pClass, Class pContextClass) {
    BundleContext context = FrameworkUtil.getBundle(pContextClass).getBundleContext();
    ServiceReference<T> reference = context.getServiceReference(pClass);
    if(reference == null){
        return null;}
    T service = context.getService(reference);
    return service;
}

之后你可以这样做:

    MWindow window = modelService.createModelElement(MWindow.class);
    window.setHeight(100);
    window.setWidth(100);
    window.setLabel("Hello");
    window.setVisible(true);
    mApplication.getChildren().add(window);

希望这有帮助。

维姆

答案 1 :(得分:0)

您可以使用“修剪窗口”。

MTrimmedWindow trimmedWindow = modelService.createModelElement(MTrimmedWindow.class);
trimmedWindow.setX(100);
trimmedWindow.setY(50);
trimmedWindow.setWidth(100);
trimmedWindow.setHeight(50);
window.setLabel("Hello");
window.setVisible(true);
mApplication.getChildren().add(window);

如果您使用MWindow,则可能会遇到异常

org.eclipse.e4.core.di.InjectionException: Unable to process "WorkbenchWindow.model"

在这种情况下,您需要使用MTrimmedWindow

相关问题