Eclipse RCP開發基礎


RCP 開發環境

       Eclipse 的重要特徵就是他的插件架構, eclipse 的內核比較小,主要是由許多功能插件組合而成。 Rich client platform ( RCP )也是基於這種插件機制的,與 eclipse workbench 的結構模式一樣,程序是由若干個插件組成,通過擴展點的的方式,提供擴展功能的編程接口。

       Eclipse RCP 程序通常由主應用程序( org.eclipse.core.runtime.application ),視窗(org.eclipse.ui.perspective )和 workbench advisor 組成。一般一個 RCP 程序至少需要 "org.eclipse.core.runtime"和 "org.eclipse.ui" 兩個的插件支持,包含在 required plugins 選項中。

 

       兩個重要的文件

MANIFEST.MF : OSGI 的 manifest 文件,用來描述插件的依賴關係和 build 環境等;

Plugin.xml : eclipse 的配置文件,描述插件擴展點等。

PDE 插件提供了這兩個文件的圖形化編輯器。

 

配置環境

       如果 Eclipse IDE 不是 RCP/Plug-in 的開發版本,則通過 eclipse 的在線更新插件功能,下載“ Eclipse for RCP/Plug-in Developers ”軟件包。


       新建 RCP 程序,命名爲 RcpProject :

 

       RCP 應用程序啓動的過程中, eclipse 會尋找擴展點 "org.eclipse.core.runtime.application" 對應的類,然後這個類被加載運行。在這個類中,創建一個 Display ,創建並運行一個 Workbench ,通過 WorkbenchAdviso r 配置這個 Workbench , WorkbenchAdvisor 又是通過 WorkbenchWindowAdvisor 來實現界面的顯示的,然後在這個類裏面實現菜單、工具欄等。

正常運行程序:

 

配置 Run configuration

       可以在 Run as — Run configuration 中, Plug-in 選項中檢查程序的依賴關係,通過 Add Required Plug-ins ,自動把依賴的插件添加進去。增加運行選項 –consoleLog ,這些可以在命令行中看到 RCP 程序的錯誤信息:

或者可以在 Windows -> Preference -> Plug-in Development -> Target Platform 中設置:


雙擊 plugin.xml 或 MANIFEST.MF 文件,進入 PDE 的圖形化編輯界面的 extensiton, 增加擴展點org.eclipse.core.runtime.products.

plugin.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>

<?eclipse version="3.4"?>

<plugin>

 

   <extension

         id= "application"

         point= "org.eclipse.core.runtime.applications" >

      <application>

         <run

               class= "rcpproject.Application" >

         </run>

      </application>

   </extension>

   <extension

         point= "org.eclipse.ui.perspectives" >

      <perspective

            name= "RCP Perspective"

            class= "rcpproject.Perspective"

            id= "RcpProject.perspective" >

       </perspective>

   </extension>

   <extension

         point="org.eclipse.core.runtime.products">

      <product

            application="RcpProject.product"

            name="name">

      </product>

   </extension>

 

</plugin>

 

打開工程的 Run as — Run configuration ,修改配置從 Run an application 到 Run a product ,如圖所示:


運行程序( Run as a product ),如果提示“ org.eclipse.epp.package.rcp is missing ”:

 

那麼,在 Run as — Run configuration 中, Plug-in 選項中檢查程序的依賴關係,通過 Add Required Plug-ins ,自動把依賴的插件添加進去,然後運行程序。

事件響應

       Eclipse RCP 開發中事件響應機制是通過聲明 command 組件來實現的,他把 UI 中的菜單、按鈕等與事件響應函數或者類關聯起來。 Comand 組件在 plugin.xml 文件中以 org.eclipse.ui.commands 作爲擴展點。因此,一個事件響應的基本過程,需要三個方面參與: UI , command 組件和事件處理函數。

 

添加事件到菜單

       雙擊 plugin.xml 或 MANIFEST.MF 文件,進入 PDE 的圖形化編輯界面的 extensiton, 增加擴展點org.eclipse.ui.commands.

 

添加 command 的 default handler 類,雙擊 defaultHandler 超鏈接,新建這個類,繼承自org.eclipse.core.commands.AbstractHandler ,代碼如下:

package rcpproject.commands;

 

import org.eclipse.core.commands.AbstractHandler;

import org.eclipse.core.commands.ExecutionEvent;

import org.eclipse.core.commands.ExecutionException;

import org.eclipse.core.commands.IHandler;

import org.eclipse.ui.handlers.HandlerUtil;

 

public class ExitHandler extends AbstractHandler implements IHandler {

 

    @Override

    public Object execute(ExecutionEvent event) throws ExecutionException {

       HandlerUtil.getActiveWorkbenchWindow (event).close();

       return null ;

    }

}

這樣就建立了一個 command 組件和事件處理函數的關聯,下面把他們與菜單事件連接起來。增加"org.eclipse.ui.menus" 擴展點,並新建一個 menuContribution ,設置其 localURI 爲“menu:org.eclipse.ui.main.menu ”。

 

在 menuContribution 下新建一個菜單( menu ),命名爲 File :


 

 

爲 File 菜單創建一個 command ,設置這個 command 爲之前創建的 command 組件,通過 commandId 關聯起來,並設置提示內容( tooltip )。

 

 

新建一個 command ,綁定一個事件處理函數 Handler ,彈出一個消息框。

 

添加 command 的 default handler 類,雙擊 defaultHandler 超鏈接,新建這個類,繼承自org.eclipse.core.commands.AbstractHandler ,代碼如下:

public class HelloWordHandler extends AbstractHandler implements IHandler {

    public Object execute(ExecutionEvent event) throws ExecutionException {

    MessageDialog.openInformation (HandlerUtil.getActiveWorkbenchWindow (

              event).getShell(), "Info", "Info for you");

       return null ;

    }

}

 

添加事件到工具欄

       這樣就建立了一個 command 組件和事件處理函數的關聯,下面把他們與工具欄事件連接起來。增加"org.eclipse.ui.menus" 擴展點,並新建一個 menuContribution ,設置其 localURI 爲

toolbar:org.eclipse.ui.main.toolbar


在 menuContribution 下創建 toolbar ,並把它與 HelloWorld 的 command 組件關聯:

 


需要修改 ApplicationWorkbenchWindowAdvisor.java 文件中的代碼,顯示 RCP 程序的工具欄:

configurer.setShowCoolBar( true );

運行程序:

 

添加事件到 View toolbar

       首先,在 extension 中增加擴展點 org.eclipse.ui.views, 並新建一個 view ,命名 id 爲“rcpproject.views.View1” ,並與類名稱 rcpproject.ViewPart1 關聯,雙擊 class 超鏈接,新建這個類。

 

       修改 Perspective.java 代碼,增加一個 ViewPart ,紅色部分是這個 ViewPart 實例的 id (字符串類型),即是之前在 plugin.xml 中創建的 ViewPart 。

public class Perspective implements IPerspectiveFactory {

    public void createInitialLayout(IPageLayout layout) {

       String editroArea = layout.getEditorArea();

       layout.setEditorAreaVisible( false );

       layout.setFixed( true );

        layout.addView( "rcpproject.views.View1" , IPageLayout. LEFT , 1.0f, editroArea);

    }

}

 

在之前創建的擴展點 org.eclipse.ui.menus 下,新建一個 menuContribution ,設置其 locationURI 爲“toolbar:rcpproject.views.View1” ,即 toolbar + 冒號 + id 名稱,然後在其下創建一個 command ,並關聯到事件響應函數 HelloWorldHandler 上。

 

運行程序:

 

 

添加事件到下拉菜單

       在 File 菜單相同的擴展點,增加 menuContribution ,其 locationURI 與 File 相同,建立 menu ,命名爲FatherMenu ,在 FatherMenu 下,建立兩個 command ,都鏈接到 HelloWorld 事件 Handler ,同時,也建立兩個menu ,每個 menu 下面建立一個 command ,也鏈接到 HelloWorld 事件 Handler 。


       運行程序:

 

 

 

 

 

添加事件到工具欄下拉菜單

       按照“添加事件到工具欄”的步驟,建立 fathercommand 按鈕,也鏈接到事件 HelloWorld 。

 

 

 

 

不同之處在於,添加的 toolbar 下的 command 的 style 選擇 pulldown 類型。然後還是在 org.eclipse.ui.menus 擴展點下,新建一個 menuContribution ,設置其 locationURI 爲“ menu:rcpproject.fathercommand ”, 即menu + 冒號 + id. 在他下面建立兩個 command ,設置他們的 label 爲“ Say HelloWorld 1” ,“ Say HelloWorld 2” ,並且都鏈接到 HelloWorld 事件響應上,這樣點擊 fathercommand ,或者下面兩個按鈕都會激發 HelloWorld 事件。


 

運行程序:

 

發佈了41 篇原創文章 · 獲贊 6 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章