E4 - 使用LifeCycleHandler监听E4应用程序的启动
在Eclipse 3.x平台中,如果你需要在程序的插件加载后执行一些任务,比如弹出用户登录对话框,你可以在对应的Application.java的start方法中添加。但在E4中,简化了Application等相关类,而是使用了org.eclipse.e4.ui.workbench.swt插件中的E4Application负责工作区模型的创建和GUI的启动,你没法简单修改了,不过我们可以使用LifeCycleHandler Hook E4应用程序启动,你只需要在plugin.xml添加“lifeCycleURI”属性就行。
<extension id="product" point="org.eclipse.core.runtime.products">
<product name="client" application="org.eclipse.e4.ui.workbench.swt.E4Application">
<property name="appName" value="client"></property>
<property name="applicationXMI" value="client/Application.e4xmi"></property>
<property name="applicationCSS" value="platform:/plugin/client/css/default.css"></property>
<property name="lifeCycleURI" value="platform:/plugin/client/client.handlers.StartupLifeCycleHandler"></property>
</product>
</extension>
<product name="client" application="org.eclipse.e4.ui.workbench.swt.E4Application">
<property name="appName" value="client"></property>
<property name="applicationXMI" value="client/Application.e4xmi"></property>
<property name="applicationCSS" value="platform:/plugin/client/css/default.css"></property>
<property name="lifeCycleURI" value="platform:/plugin/client/client.handlers.StartupLifeCycleHandler"></property>
</product>
</extension>
LifeCycleHandler.java只是一个POJO对象,我们可以在方法上添加Eclipse特有的注解@PostContextCreate,这个注解标记该方法在插件启动后执行。
下面是实现例子,很简单的:
import javax.inject.Inject;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.ui.workbench.lifecycle.PostContextCreate;
import org.eclipse.jface.window.Window;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.ui.workbench.lifecycle.PostContextCreate;
import org.eclipse.jface.window.Window;
/**
*
* @author 刘尧兴
* @version at 2011-7-13
*/
@SuppressWarnings("restriction")
public class StartupLifeCycleHandler {
@PostContextCreate
public void startup(IEclipseContext context) {
context.set("login", "admin".equals(System.getenv("user")));
}
}
*
* @author 刘尧兴
* @version at 2011-7-13
*/
@SuppressWarnings("restriction")
public class StartupLifeCycleHandler {
@PostContextCreate
public void startup(IEclipseContext context) {
context.set("login", "admin".equals(System.getenv("user")));
}
}
在E4Application.java是这样处理的:
String lifeCycleURI = getArgValue(E4Workbench.LIFE_CYCLE_URI_ARG,applicationContext, false);
if (lifeCycleURI != null) {
lcManager = factory.create(lifeCycleURI, appContext);
if (lcManager != null) {
// Let the manager manipulate the appContext if desired
ContextInjectionFactory.invoke(lcManager,PostContextCreate.class, appContext, null);
}
}
// Create the app model and its context
MApplication appModel = loadApplicationModel(applicationContext,appContext);
appModel.setContext(appContext);
if (lifeCycleURI != null) {
lcManager = factory.create(lifeCycleURI, appContext);
if (lcManager != null) {
// Let the manager manipulate the appContext if desired
ContextInjectionFactory.invoke(lcManager,PostContextCreate.class, appContext, null);
}
}
// Create the app model and its context
MApplication appModel = loadApplicationModel(applicationContext,appContext);
appModel.setContext(appContext);