我正在考虑从基于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的事实,有没有更简单的方法来掌握模型?为什么很难找到?
要做您想做的事,您可以使用此代码段。
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);
希望这会有所帮助。
Wim
您可以使用修剪的窗口。
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
。