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) {
    }
    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM