E4 学习笔记

常见错误

     1、Caused by: java.lang.NullPointerException
          at org.eclipse.e4.ui.internal.workbench.E4Workbench.processHierarchy(E4Workbench.java:xxxx)

     解决方法:

     在Application.e4xmi文件中添加:      

<addons xmi:id="_XGB3wPZlEd-XstlTZ6nTXg" elementId="org.eclipse.e4.core.commands.service" contributionURI="platform:/plugin/org.eclipse.e4.core.commands/org.eclipse.e4.core.commands.CommandServiceAddon"/>
<addons xmi:id="_XGB3wPZlEd-XstlTZ6nTXh" elementId="org.eclipse.e4.ui.contexts.service" contributionURI="platform:/plugin/org.eclipse.e4.ui.services/org.eclipse.e4.ui.services.ContextServiceAddon"/>
<addons xmi:id="_XGB3wPZlEd-XstlTZ6nTXi" elementId="org.eclipse.e4.ui.bindings.service" contributionURI="platform:/plugin/org.eclipse.e4.ui.bindings/org.eclipse.e4.ui.bindings.BindingServiceAddon"/>
<addons xmi:id="_LK0NgPZmEd-XstlTZ6nTXj" elementId="org.eclipse.e4.ui.workbench.commands.model" contributionURI="platform:/plugin/org.eclipse.e4.ui.workbench/org.eclipse.e4.ui.internal.workbench.addons.CommandProcessingAddon"/>
<addons xmi:id="_LK0NgPZmEd-XstlTZ6nTXk" elementId="org.eclipse.e4.ui.workbench.contexts.model" contributionURI="platform:/plugin/org.eclipse.e4.ui.workbench/org.eclipse.e4.ui.internal.workbench.addons.ContextProcessingAddon"/>
<addons xmi:id="_LK0NgPZmEd-XstlTZ6nTXl" elementId="org.eclipse.e4.ui.workbench.bindings.model" contributionURI="platform:/plugin/org.eclipse.e4.ui.workbench.swt/org.eclipse.e4.ui.workbench.swt.util.BindingProcessingAddon"/>

 

    2、@UIEventHandler 编译错误

     解决方法:

     org.eclipse.e4.core.services.annotations.UIEventHandler@UIEventHandler 替换成 org.eclipse.e4.core.di.extensions.@UIEventTopic

     org.eclipse.e4.core.services.annotations.UIEventHandler@EventHandler 替换成 org.eclipse.e4.core.di.extensions.@EventTopic

 

使用技巧

     1、获取IEclipseContext实例

public static IEclipseContext getServiceContext() {
        return EclipseContextFactory.getServiceContext(Activator.getDefault().getContext());
    }
 
2.获取在e4.xmi文件中ID为app.base的应用程序

@Inject
@Named("app.base")
private MApplication application;

    

     3、纯代码创建MApplication

private MApplication createApplication() {
MApplication app = ApplicationFactoryImpl.eINSTANCE.createApplication();
app.setContext(applicationContext);
MWindow window = BasicFactoryImpl.eINSTANCE.createWindow();
window.setElementId("singleValidId");
app.getChildren().add(window);

MPartSashContainer psc = BasicFactoryImpl.eINSTANCE.createPartSashContainer();
psc.setElementId("twoValidIds");
psc.getTags().add("oneValidTag");
window.getChildren().add(psc);

MPartStack stack = BasicFactoryImpl.eINSTANCE.createPartStack();
stack.getTags().add("twoValidTags");
psc.getChildren().add(stack);

MPart part1 = BasicFactoryImpl.eINSTANCE.createPart();
part1.setElementId("twoValidIds");
stack.getChildren().add(part1);

MPart part2 = BasicFactoryImpl.eINSTANCE.createPart();
part2.getTags().add("twoValidTags");
part2.getTags().add("secondTag");
stack.getChildren().add(part2);

MPart part3 = BasicFactoryImpl.eINSTANCE.createPart();
psc.getChildren().add(part3);

return app;
}

      4、使用ModelService.findElements查找模型对象。

MPartSashContainer psc = BasicFactoryImpl.eINSTANCE.createPartSashContainer();
   psc.setElementId("twoValidIds");
   psc.getTags().add("oneValidTag");
//查找
   List<String> tags = new ArrayList<String>();
   tags.add("oneValidTag");
   List<MUIElement> oneTags = modelService.findElements(application, null,null, tags);
   assertEquals(oneTags.size(), 1);

    5、使用ModelService.insert插入对象

 
  

MPartStack stack = BasicFactoryImpl.eINSTANCE.createPartStack();
stack.setElementId("theStack");
window.getChildren().add(stack);

//插入

modelService.insert(newPart, (MPartSashContainerElement) modelService.find("theStack", mapplication), EModelService.ABOVE, 35);


    6、在上下文中查找当前激活窗体

// lookup function that goes down the context's active child chain
IEclipseContext.set(IServiceConstants.ACTIVE_PART, new ContextFunction() {
@Override
public Object compute(IEclipseContext context) {
IEclipseContext childContext = context.getActiveChild();
if (childContext == null) {
return null;
}

while (childContext != null) {
context = childContext;
childContext = context.getActiveChild();
}
return context.getLocal(Object.class.getName());
}
});

   7、获得Eclipse 实例安装地址。

public Location getInstanceLocation() {
if (locationTracker == null) {
BundleContext context = FrameworkUtil.getBundle(ResourceHandlerTest.class).getBundleContext();
Filter filter = null;
try {
filter = context.createFilter(Location.INSTANCE_FILTER);
} catch (InvalidSyntaxException e) {
// ignore this. It should never happen as we have tested the
// above format.
}
locationTracker = new ServiceTracker(context, filter, null);
locationTracker.open();
}
return (Location) locationTracker.getService();
}

   8、Handler的 @Execute和@CanExecute用法

public class DeleteContactHandler {
@CanExecute
boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MContext context) {
if (context == null)
return false;
Contact contact = (Contact) context.getContext().get(IServiceConstants.ACTIVE_SELECTION);
return contact != null;
}

@Execute
void execute(@Named(IServiceConstants.ACTIVE_PART) MContext context) {
Contact contact = (Contact) context.getContext().get(IServiceConstants.ACTIVE_SELECTION);
ContactsRepositoryFactory.getContactsRepository().removeContact(contact);
}
}

转载于:https://www.cnblogs.com/vwpolo/archive/2011/07/15/2107403.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值