Eclipse RCP开发(三):RCP项目代码分析

时间:2019-09-21
本文章向大家介绍Eclipse RCP开发(三):RCP项目代码分析,主要包括Eclipse RCP开发(三):RCP项目代码分析使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

原文链接:https://blog.csdn.net/vking_wang/article/details/8716073

plugin.xml

  Eclipse默认用plugin manifest editor打开plugin.xml,主要有如下几个标签页:

1)Overview

  显示项目基本信息,其中Test区域的按钮可快速启动或调试plugin程序。

2)Dependencies

  可查看该插件所依赖的其他插件,例如本插件依赖于org.eclipse.core.runtime、org.eclipse.ui;

还可通过Dependency Analysis查看dependency hierarchy。

 这部分内容实际是定义在MANIFEST.MF文件中:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: RCP_HelloWorld
Bundle-SymbolicName: RCP_HelloWorld; singleton:=true
Bundle-Version: 1.0.0.qualifier
Require-Bundle: org.eclipse.core.runtime,
 org.eclipse.ui
Bundle-RequiredExecutionEnvironment: JavaSE-1.6

3)Extensions

  扩展,是将类连接到Eclipse结构的机制。

  这部分内容实际是定义在plugin.xml中的内容:

<plugin>
   <extension
         id="application"
         point="org.eclipse.core.runtime.applications">
      <application>
         <run
               class="rcp_helloworld.Application">
         </run>
      </application>
   </extension>
   <extension
         point="org.eclipse.ui.perspectives">
      <perspective
            name="RCP Perspective"
            class="rcp_helloworld.Perspective"
            id="RCP_HelloWorld.perspective">
      </perspective>
   </extension>
</plugin>

Application

/**
 * This class controls all aspects of the application's execution
 */
public class Application implements IApplication {
 
    /* (non-Javadoc)
     * @see org.eclipse.equinox.app.IApplication#start
            (org.eclipse.equinox.app.IApplicationContext)
     */
    @Override
    public Object start(IApplicationContext context) throws Exception {
        Display display = PlatformUI.createDisplay();
        try {
                //-----------WorkbenchAdvisor
                int returnCode = PlatformUI.
                      createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
            if (returnCode == PlatformUI.RETURN_RESTART)
               return IApplication.EXIT_RESTART;
            else
               return IApplication.EXIT_OK;
        } finally {
            display.dispose();
        }
        
    }
 
    /* (non-Javadoc)
     * @see org.eclipse.equinox.app.IApplication#stop()
     */
    @Override
    public void stop() {
        if (!PlatformUI.isWorkbenchRunning())
            return;
        final IWorkbench workbench = PlatformUI.getWorkbench();
        final Display display = workbench.getDisplay();
        display.syncExec(new Runnable() {
            @Override
            public void run() {
                if (!display.isDisposed())
                    workbench.close();
            }
        });
    }
}

相当于程序入口。

  在start方法中,创建了一个Display;然后启动Eclipse Workbench(PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor()))。

Application必须关联到Eclipse Runtime的应用程序扩展点,从而使Runtime知道Application。

一个系统中的多个Eclipse插件可以共享应用程序扩展。

WorkbenchAdvisor

上面Application代码中createAndRunWorkbench方法参数中用到了WorkbenchAdvisor。WorkbenchAdvisor告诉Workbench如何行动:如何做、做什么。

本例的WorkbenchAdvisor:

  定义了要显示的透视图
  定义了要使用的WorkbenchWindowAdvisor

public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {
   
    private static final String PERSPECTIVE_ID = "RCP_HelloWorld.perspective"; 
    //-----------WorkbenchWindowsAdvisor
    public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(
        IWorkbenchWindowConfigurer configurer) {
        return new ApplicationWorkbenchWindowAdvisor(configurer);
    }
    //----------Perspective
    public String getInitialWindowPerspectiveId() {
    return PERSPECTIVE_ID;
    }
}

  如果要保存窗口位置和尺寸,以便下次打开的时候不被重置:

    @Override
    public void initialize(IWorkbenchConfigurer configurer){
        configurer.setSaveAndRestore(true);
    }

WorkbenchWindowAdvisor

  每个应用程序的每一个窗口都有一个WorkbenchWindowAdvisor,定义如何渲染窗口:设置窗口初始大小、标题、状态栏、工具栏。 

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
 
    public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
        super(configurer);
    }
    //----------ActionBarAdvisor
    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$
    }
}

Perspective

定义透视图的布局。默认什么都不做:

public class Perspective implements IPerspectiveFactory {
 
    public void createInitialLayout(IPageLayout layout) {
    }
}

ActionBarAdvisor

创建窗口所需要的动作,并在窗口中定位它们。控制菜单栏上出现的命令、工具栏、状态栏。默认什么都不做:

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
 
    public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
        super(configurer);
    }
 
    protected void makeActions(IWorkbenchWindow window) {
    }
 
    protected void fillMenuBar(IMenuManager menuBar) {
    }
    
}

原文地址:https://www.cnblogs.com/lotuses/p/11561457.html