插件开发――应用运行顺序
新创建的RCP项目,默认生成了一些类:
Activator:插件激活器,在插件启动时(所有类调用之前)调用其start()方法,在插件卸载时调用其stop()方法。 Application:应用程序管理类,也算是应用程序的入口类。 ApplicationActionBarAdvisor:菜单、工具栏、状态栏管理类。 ApplicationWorkbenchAdvisor:应用工作台管理类。 ApplicationWorkbenchWindowAdvisor:应用工作台窗口管理类,可以进行窗口相关的初始化工作,例如程序启动后窗口最大化。 Perspective:透视图管理类,初始化界面布局,就是设定界面有几个视图,其位置怎么分布。 |
Eclipse中,将项目下的META-INF/ MANIFEST.MF、build.properties和plugin.xml文件采用统一的编辑器进行展示编辑,build.properties用来配置项目的构建信息,plugin.xml文件配置插件扩展和扩展点,其他的由MANIFEST.MF文件负责。
如下图所示,概述、依赖性、运行时是由MANIFEST.MF管理的,扩展和扩展点是由plugin.xml管理的,构建由build.properties管理,后面是对应的这三个文件的文本编辑器。
RCP的项目,需要结合源码与插件配置(plugin.xml文件),以下将简要说明一下程序的启动步骤:
- 插件激活
应用启动时,首先是插件激活,在插件中任一类调用时进行插件激活,将调用Activator类的start()方法。
插件激活不是必须的,当插件采用懒加载机制时(即勾选"装入其中一个类时激活此插件")才起作用,在插件第一次被调用时进行插件激活。
- 应用启动
应用程序会查找plugin.xml文件中,扩展标签中定义的应用扩展(org.eclipse.core.runtime.applications),查找其中的应用类(如下图)。
查找到应用后,将调用应用类中的start()方法。
在应用类的start()方法中,将创建Display,创建并运行工作台,从下面的代码中可以看到创建了工作台管理类:ApplicationWorkbenchAdvisor。
public Object start(IApplicationContext context) throws Exception { Display display = PlatformUI.createDisplay(); try { int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor()); if (returnCode == PlatformUI.RETURN_RESTART) return IApplication.EXIT_RESTART; else return IApplication.EXIT_OK; } finally { display.dispose(); }
} |
-
工作台启动
工作台管理类中做了两件事:创建工作台窗口管理类、返回初始化的透视图ID。
public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {
private static final String PERSPECTIVE_ID = "plugin.demo.perspective"; //$NON-NLS-1$
public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) { return new ApplicationWorkbenchWindowAdvisor(configurer); }
public String getInitialWindowPerspectiveId() { return PERSPECTIVE_ID; } } |
-
加载透视图
应用将根据工作台管理类中返回的透视图ID,从plugin.xml文件中查找对应的透视图,执行相应的透视图类中的代码。
在此类中创建初始的界面布局。
public class Perspective implements IPerspectiveFactory {
public void createInitialLayout(IPageLayout layout) {
} } |
-
工作台窗口初始化
ApplicationWorkbenchWindowAdvisor类是工作台窗口管理类,在此处将初始化窗口信息(如初始化大小、是否显示工具栏、菜单栏、状态栏等)、创建动作条管理类(ApplicationActionBarAdvisor)。
如果需要程序启动后窗口最大化或窗口居中,在此类中进行配置,但应注意应在postWindowCreate()方法中配置,因为preWindowOpen()方法调用时,对应的Shell还没有创建出来。
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor { public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) { return new ApplicationActionBarAdvisor(configurer); }
public void preWindowOpen() { IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); configurer.setInitialSize(new Point(400, 300)); configurer.setShowCoolBar(false); configurer.setShowStatusLine(false); configurer.setTitle("Hello RCP"); //$NON-NLS-1$ } @Override public void postWindowCreate() { //最大化 getWindowConfigurer().getWindow().getShell().setMaximized(true); } } |
-
动作条管理
ApplicationActionBarAdvisor类负责菜单、工具栏、状态栏的管理,包括显示的菜单项、工具项等。
RCP菜单、工具栏的创建有多种方式,在此创建是其中的一种方式。