主要简单分析一下eclipse自动生成的几个类。

Application:
是RCP应用的入口,负责应用的启动和关闭。 实现接口:IApplication,核心方法有2个:
public Object start(IApplicationContext context) //启动应用
public void stop() //关闭应用
|
在启动rcp的时候,会调用start方法。
从plugin.xml可知,Application是作为Eclipse 运行应用org.eclipse.core.runtime.applications的一个扩展点:

代码:
public class Application implements IApplication {
@Override public Object start(IApplicationContext context) { Display display = PlatformUI.createDisplay(); try { int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor()); if (returnCode == PlatformUI.RETURN_RESTART) { return IApplication.EXIT_RESTART; } return IApplication.EXIT_OK; } finally { display.dispose(); } }
@Override public void stop() { if (!PlatformUI.isWorkbenchRunning()) return; final IWorkbench workbench = PlatformUI.getWorkbench(); final Display display = workbench.getDisplay(); display.syncExec(() -> { if (!display.isDisposed()) workbench.close(); }); }
}
|
启动应用时,先创建一个Display,然后调用PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor())来创建工作台、打开应用主窗口(可视化),并打开事件循环:应用程序处理用户事件,直到用户退出。
关闭应用之前,还应该销毁Display、workbench。
程序主窗口只有在本行执行代码:PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor()) 后才会打开,发人员可以在此之前进行登陆验证、资源初始化等操作。
ApplicationWorkbenchAdvisor:
即WorkbenchAdvisor,可以参考主题3的说明。 负责管理应用的生命周期。
我们可以在该类中实现程序启动或者关闭时的某种处理,比如设置工作台的一些样式功能选择等。该类还需要配合WorkbenchWindowAdvisor 和 ActionBarAdvisor才能构成完整的用户界面。
代码:
public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor { /** * 透视图ID, 必须与plugin.xml中配置的透视图扩展点(org.eclipse.ui.perspectives)的id一致 */ private static final String PERSPECTIVE_ID = "com.zyj.jfcs.perspective";
public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) { return new ApplicationWorkbenchWindowAdvisor(configurer); } public void initialize(IWorkbenchConfigurer configurer) { super.initialize(configurer); configurer.setSaveAndRestore(false); //不需要保存界面状态,默认为false
// //设置标题栏的弧形外观
// PlatformUI.getPreferenceStore().setValue(
// IWorkbenchPreferenceConstants.SHOW_TRADITIONAL_STYLE_TABS, false);
// //启动时显示进度条
// PlatformUI.getPreferenceStore().setValue(
// IWorkbenchPreferenceConstants.SHOW_PROGRESS_ON_STARTUP, true);
// //启动完成,显示欢迎动画
// PlatformUI.getPreferenceStore().setValue(
// IWorkbenchPreferenceConstants.SHOW_INTRO, true); }
public String getInitialWindowPerspectiveId() { //返回透视图的ID return PERSPECTIVE_ID; }
}
|
ApplicationWorkbenchWindowAdvisor
即WorkbenchWindowAdvisor,可参考之前的说明。
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) { super(configurer); }
public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
return new ApplicationActionBarAdvisor(configurer);
} /** * 主窗口创建之前调用: * 主要用来定义窗口包含的元素 */ public void preWindowOpen() { /* * 首先获取configure对象 * 通过configure对象进行初始配置 */ IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); configurer.setInitialSize(new Point(800, 600)); //显示工具栏: true 显示 | false 不显示 configurer.setShowCoolBar(true); //不显示状态栏 configurer.setShowStatusLine(true); //只显示最小化、关闭按钮 configurer.setShellStyle(SWT.MIN | SWT.CLOSE); configurer.setTitle(UIConsts.APPLICATION_TITLE); //窗口标题 }
/** * 主窗口打开之后调用 * 主要对窗口样式进行调整 */ @Override public void postWindowOpen() { //设置窗口自动剧中 Shell shell = getWindowConfigurer().getWindow().getShell(); Rectangle screenSize = Display.getDefault().getClientArea(); Rectangle frameSize = shell.getBounds(); shell.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); System.out.println("主窗口居中显示!"); }
}
|
IWorkbenchWindowConfigurer 是一个接口,提供了很多get/set方法来操作工作台的配置。
ApplicationActionBarAdvisor
即ActionBarAdvisor。
下面代码创建了一个只有一个帮助菜单项的菜单栏,其菜单项执行Eclipse内置的ACtoinFactory.INTRO,执行该动作将显示Welcome欢迎画面。
这里主要负责定义主窗口的工具栏、状态栏、菜单栏 的内容。
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
private IWorkbenchAction introAction; public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) { super(configurer); }
protected void makeActions(IWorkbenchWindow window) { introAction = ActionFactory.INTRO.create(window); register(introAction); }
protected void fillMenuBar(IMenuManager menuBar) { MenuManager helpMenu = new MenuManager("&Help", IWorkbenchActionConstants.M_HELP); menuBar.add(helpMenu);
// Help helpMenu.add(introAction); }
}
|
Perspective:
即透视图。
/** * 应用透视图,负责应用的主界面布局展示: * 不负责创建元素,只负责把已有的视图(界面、元素)渲染到透视图中,并进行布局设置 */
public class Perspective implements IPerspectiveFactory {
public void createInitialLayout(IPageLayout layout) { String editArea = layout.getEditorArea(); layout.setEditorAreaVisible(true); //隐藏编辑区域 layout.setFixed(true); //固定布局,不能移动、不能改变大小 //加入教学单位视图 layout.addStandaloneView(ApplicationConsts.TEACH_UNIT_NAME_VIEW_ID, true, IPageLayout.LEFT, 0.26f, //左侧展示,占比26% editArea); layout.getViewLayout(ApplicationConsts.TEACH_UNIT_NAME_VIEW_ID).setCloseable(false); //禁用关闭按钮 layout.getViewLayout(ApplicationConsts.TEACH_UNIT_NAME_VIEW_ID).setMoveable(false); //禁止拖拽窗口 //加入经费概览视图 layout.addStandaloneView(ApplicationConsts.PIE_DIAGRAM_VIEW_ID, true, IPageLayout.BOTTOM, 0.72f, //底侧展示,占比28% ApplicationConsts.TEACH_UNIT_NAME_VIEW_ID); layout.getViewLayout(ApplicationConsts.PIE_DIAGRAM_VIEW_ID).setCloseable(false); //禁用关闭按钮 layout.getViewLayout(ApplicationConsts.PIE_DIAGRAM_VIEW_ID).setMoveable(false); //禁止拖拽窗口 //加入教学单位课程明细视图 layout.addView(ApplicationConsts.TEACH_UNIT_CLASS_VIEW_ID, IPageLayout.RIGHT, 0.74f, //右侧展示,占比26% editArea); layout.getViewLayout(ApplicationConsts.TEACH_UNIT_CLASS_VIEW_ID).setCloseable(false); //禁用关闭按钮 layout.getViewLayout(ApplicationConsts.TEACH_UNIT_CLASS_VIEW_ID).setMoveable(false); //禁止拖拽窗口 AbstractUIPlugin.imageDescriptorFromPlugin("", ""); new PropertyChangeEvent(true, "xxx", null, null); }
}
|
基础_2:项目框架核心类简单分析
原文:https://www.cnblogs.com/zyj-468161691/p/12164826.html