读书人

RCP(1)

发布时间: 2012-07-05 07:59:17 作者: rapoo

RCP(一)

RCP是建立在eclipse平台基础之上的一个富客户端应用程序,由于RCP程序是基于插件架构的,所以RCP应用程序也是通过扩展的方式对应用进行扩展,它是根据org.eclipse.core.runtime.applications扩展点建立的,此扩展点的实现类,是整个RCP应用的入口。

?

1.org.eclipse.core.runtime.applications扩展点的建立

?

?

<extension      id="myapplication"      point="org.eclipse.core.runtime.applications">   <application>      <run name="code">public class MyApplication implements IPlatformRunnable {    public Object run(Object args) throws Exception {        Display display = PlatformUI.createDisplay();        try {            int returnCode = PlatformUI.createAndRunWorkbench(display, new MyWorkbenchAdvisor());            if (returnCode == PlatformUI.RETURN_RESTART) {                return IPlatformRunnable.EXIT_RESTART;            }            return IPlatformRunnable.EXIT_OK;        } finally {            display.dispose();        }    }}

?所有RCP应用程序里这个方法的实现几乎是完全一样的,即启动 Workbench,并把一个WorkbenchAdvisor实例作为参数传给它,所以应用程序的定制实际上是通过这个WorkbenchAdvisor实例实现的。

?

?

?

public class MyWorkbenchAdvisor extends WorkbenchAdvisor {    private static final String PERSPECTIVE_ID = "com.example.ui.MyPerspective";    public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(            IWorkbenchWindowConfigurer configurer) {        return new MyWorkbenchWindowAdvisor(configurer);    }    public String getInitialWindowPerspectiveId() {        return PERSPECTIVE_ID;    }    public void initialize(IWorkbenchConfigurer configurer) {        super.initialize(configurer);        //The workaround call        WorkbenchAdapterBuilder.registerAdapters();    }}

?MyWorkbenchAdvisor是org.eclipse.ui.application.WorkbenchAdvisor的一个子类,它覆盖了WorkbenchAdvisor的createWorkbenchWindowAdvisor和getInitialWindowPerspectiveId方法。createWorkbenchWindowAdvisor返回一个WorkbenchWindowAdvisor对象实例,这个对象里面主要用于定制应用程序窗口,包括菜单和工具条。getInitialWindowPerspectiveId返回一个透视图ID,一般都会自己定义。

?

?

public class MyWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {    public MyWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {        super(configurer);    }    public ActionBarAdvisor createActionBarAdvisor(            IActionBarConfigurer configurer) {        return new MyActionBarAdvisor(configurer);    }    public void preWindowOpen() {        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();        configurer.setInitialSize(new Point(700, 500));        configurer.setShowCoolBar(false);        configurer.setShowStatusLine(false);        configurer.setTitle("My RCP Application");    }}

?在这个类中可以通过覆盖其父类的一些方法,控制Workbench的一些显示,如定义窗口大小和标题,隐藏了工具条。在createActionBarAdvisor方法中实现定制菜单和工具条的类。

?

public class MyActionBarAdvisor extends ActionBarAdvisor {    private IWorkbenchAction exitAction;    public MyActionBarAdvisor(IActionBarConfigurer configurer) {        super(configurer);    }    protected void makeActions(final IWorkbenchWindow window) {        exitAction = ActionFactory.QUIT.create(window);        register(exitAction);    }    protected void fillMenuBar(IMenuManager menuBar) {        MenuManager fileMenu = new MenuManager("&File",                IWorkbenchActionConstants.M_FILE);        menuBar.add(fileMenu);        fileMenu.add(exitAction);    }}

??先在makeActions()里构造需要出现在菜单 或工具条上的命令,注意要调用register()方法注册这些命令,作用是在应用程序结束后释放资源,同时支持快捷键操作;然后在 fillMenuBar()方法里把这些命令加入主菜单,因为我们隐藏了工具条,所以没有覆盖fillCoolBar()方法,另外你还可以通过覆盖 fillStatusLine()定义自己的状态栏。

?

RCP应用程序的缺省外观是一个空白窗口,一般我们要通过一个WorkbenchAdvisor类对界面进行定制。 WorkbenchAdvisor有很多回调方法,可以在preWindowOpen()方法里设置菜单、工具条、状态栏、进度栏、透视图切换工具是否可 见,在fillActionBars()方法里添加菜单和工具条项,在getInitialWindowPerspectiveId()方法里指定首选的 透视图。

读书人网 >开源软件

热点推荐