4、定义WorkbenchAdvisor类 和Application类
(1)创建WorkbenchAdvisor类
l 构建 RCP 应用程序的核心任务之一就是创建一个实现抽象类 org.eclipse.ui.application.WorkbenchAdvisor 的类
l WorkbenchAdvisor 类负责配置,在执行 RCP 应用程序时显示的工作台
package com.xqtu.google; import org.eclipse.ui.application.IWorkbenchWindowConfigurer; import org.eclipse.ui.application.WorkbenchAdvisor; import org.eclipse.swt.graphics.Point; public class GoogleWorkbenchAdvisor extends WorkbenchAdvisor { public String getInitialWindowPerspectiveId() { return "com.xqtu.google.GooglePerspective"; } public void preWindowOpen(IWorkbenchWindowConfigurer configurer) { super.preWindowOpen(configurer); configurer.setTitle("Google"); configurer.setInitialSize(new Point(300, 300)); configurer.setShowMenuBar(false); configurer.setShowStatusLine(false); configurer.setShowCoolBar(false); } }
l 在getInitialWindowPerspectiveId()方法中,向新的工作台窗口返回初始透视图的标识符
l 增加preWindowOpen()方法,设置工作台的窗口标题和尺寸
(2)创建Application类
l 在执行应用程序之前,需要创建一个 Application 类,这与 Java 类中的main方法类似, 是RCP应用程序的入口点
l 该类需要实现org.eclipse.core.runtime.IPlatformRunnable接口
package com.xqtu.google; import org.eclipse.core.runtime.IPlatformRunnable; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.application.WorkbenchAdvisor; public class GoogleApplication implements IPlatformRunnable { public Object run(Object args) throws Exception { WorkbenchAdvisor workbenchAdvisor = new GoogleWorkbenchAdvisor(); Display display = PlatformUI.createDisplay(); int returnCode = PlatformUI.createAndRunWorkbench(display, workbenchAdvisor); if (returnCode == PlatformUI.RETURN_RESTART) { return IPlatformRunnable.EXIT_RESTART; } else { return IPlatformRunnable.EXIT_OK; } } }
l 其中的run()方法对大多数RCP 应用程序而言,不需要定制,重新使用就可
l 如前面所示,需要在plugin.xml的org.eclipse.core.runtime.applications 扩展点指定运行的Application 类
<extension id="GoogleApplication" point="org.eclipse.core.runtime.applications"> <application> <run class="com.xqtu.google.GoogleApplication"/> </application> </extension>
(3)运行应用程序
l Run > Run...
l 在Configurations列表中选择Run-time Workbench,并点击 New 按钮
l 在Name域中键入Google
l 在Arguments页中,Run an application下拉框中选择Google.GoogleApplication
l 点击Plug-ins页,选择Choose plug-ins and fragments to launch from the list
l 点击Deselect All按钮
l 选中Workspace Plug-ins选项包含Google项的选择
l 点击Add Required Plug-ins按钮,自动包含执行应用程序必需的插件
l 点击Apply按钮
l 点击Run按钮来执行该应用程序
l 如果正确进行了所有配置的话,应该显示一个标题为“Google”的窗口,这是一个普通工作台框架 
|