eclipse 幫助中的定義

An activity is a logical grouping of function that is centered around a certain kind of task. For example, developing Java software is an activity commonly performed by users of the platform, and the JDT defines many UI contributions (views, editors, perspectives, preferences, etc.) that are only useful when performing this activity.

When an activity is enabled in the platform, the UI contributions associated with that activity are shown. When an activity is disabled in the platform, its UI contributions are not shown. 

Certain user operations serve as trigger points for enabling an activity. For example, creating a new Java project could trigger the enabling of the Java development activity. In this way, users are exposed to new function as they need it, and gradually learn about the activities that are available to them and how they affect the UI. When a user first starts the platform, it is desirable for as many activities as possible to be disabled, so that the application is as simple as possible. Choices made in the welcome page can help determine what activities should be enabled.

也就是說activity以著特定的任務為中心的功能集合,只有當activity 被啟用的時候,與之相關的UI組件才會被顯示。

Activities vs. perspectives

Perspectives are used to organize different view layouts and action sets into tasks. Why do we need activities? While perspectives and activities define similar kinds of tasks, the main difference is how the UI contributions for a plug-in are associated with them. UI contributions are associated with perspectives in the extension definition of the contribution. That is, a plug-in is in charge of determining what perspectives its views and action sets belong to. Plug-ins are also free to define their own perspectives. Even when a perspective is not active, the user can access the views and actions associated with the perspective through commands such as Show View.

Activities are a higher level of organization. Individual UI contributions are not aware of activities and do not refer to the activities in their extension definitions. Rather, the activities are expected to be configured at a higher level such as platform integration/configuration or product install. Individual plug-ins typically do not define new activities, unless the plug-in is a systems-level plug-in defined by a systems integrator. In a typical scenario, a systems integrator determines how function is grouped into activities and which ones are enabled by default. Activities are associated with UI contributions using activity pattern bindings, patterns that are matched against the id of the UI contributions made by plug-ins. 

Perspectives 和Activities都可以用來組織不同的視圖布局,區別在于插件(plugin)管理UI組件的方式不同。即使Perspectives 不顯示,仍然可以顯示Views(通過commands such as Show View),而Activities是一種更高層次的管理方式,單個的UI組件不會意識到activities的存在 ,也不會依賴于activities 。實際上activities 是在更高的層次進行配置的,比如平臺集成配置或者產品安裝的時候。比較典型的配置方式是存在一個系統集塵器,由它來決定功能如何進行分組到不同的activities,以及哪些activities是默認啟動的。Activities與UI組件聯系的方式是activity pattern bindings(與UI組件的id進行匹配)

 

定義Activities

Activities使用org.eclipse.ui.activities擴展點定義,下面是一個例子:

<extension point="org.eclipse.ui.activities">
       <activity
            id="com.examples.reimbursement.viewActivity"  name="ViewActivity"/>
      <activityPatternBinding
            activityId="com.examples.reimbursement.viewActivity"
            pattern=".*/emp\..*"/>
      <activity
            id="com.examples.reimbursement.editActivity"  name="EditActivity"/>
      <activityPatternBinding
            activityId="com.examples.reimbursement.createActivity"
            pattern=".*/emp\..*"/> 
      <activityRequirementBinding
            activityId="com.examples.reimbursement.createActivity"
            requiredActivityId="com.examples.reimbursement.viewActivity"/> 
      <activityPatternBinding
            activityId="com.examples.reimbursement.deleteActivity"
            pattern=".*/admin\..*"/>
      <activityRequirementBinding
            activityId="com.examples.reimbursement.deleteActivity"
            requiredActivityId="com.examples.reimbursement.approveActivity"/>

....

</extension>

activityPatternBinding定義了綁定的模式

activityRequirementBinding指定了Activities之間的依賴關系,比如createActivity需要viewActivity

 

activities 與UI組件的綁定

Activities 與UI組件使用java.util.regex 正則表達式進行模式匹配,在workbench中,該模式分為兩個層次:
(1)使用插件標識符
(2)插件內UI組件標識符
比如plug-in-identifier + "/" + local-identifier,上面的例子:
<activityPatternBinding 
     activityId="com.examples.reimbursement.viewActivity" 
     pattern=".*/emp\..*"/>
匹配任何插件內,其id含有emp.任何UI組件,其中\.意思是轉義. 而.則代表任意字符,詳情參見java正則表達式
<activityPatternBinding 
     activityId="com.examples.reimbursement.deleteActivity" 
     pattern=".*/admin\..*"/> 
匹配任何插件內,其id含有admin.任何UI組件

activity pattern bindings可以與任何含有特定表示的UI組件進行綁定,包括:

  • Views and editors
  • Perspectives
  • Preference and property pages
  • Menus and toolbars
  • New project wizard

 

定義role與activities的關聯

<extension
       point="com.examples.reimbursement.demo.roles">
       <role
          id="com.examples.reimbursement.demo.employee"
          label="com.examples.reimbursement.demo.employee"
          roleId="empRole">
       <enabledActivity id="com.examples.reimbursement.viewActivity"/>
       <enabledActivity id="com.examples.reimbursement.createActivity"/>
    </role>
     .....
</extension>

activities API的使用

activities API能夠定義activities、改變他們的狀態。大多數插件不需要使用該API,但是為了實現某些功能,比如允許用戶控制activities或者提供一個觸發點來觸發相關的活動,這種情況下,API是有用的。

The workbench activity support includes an API for working with the defined activities and changing their enabled state. Most plug-ins need not be concerned with this API, but it is useful when implementing function that allows the user to work with activities, or for implementing the  trigger points that enable a particular activity. It is assumed that any plug-in  that is manipulating activities through API is quite aware of the ways that activities are configured for a particular product. For example, the workbench itself uses the API to trigger the enablement of activities such as Java
development. We'll look at how the workbench uses the generic activity API to implement triggers.

The hub of all activity in the workbench is IWorkbenchActivitySupport. The activity support works in tandem with an IActivityManager. Plug-ins can obtain the activity support instance from the workbench, and the activity manager from
there.

IWorkbenchActivitySupport workbenchActivitySupport = PlatformUI.getWorkbench().getActivitySupport(); 

IActivityManager activityManager = workbenchActivitySupport.getActivityManager(); 

The following snippet enables the Java development activity (if it is not already enabled). It shows a simplified version of a trigger. ... //the user did something Java related.  Enable the Java activity.

Set enabledActivityIds = new HashSet(activityManager.getEnabledActivityIds()); 
if (enabledIds.add("org.eclipse.javaDevelopment")) 
workbenchActivitySupport.setEnabledActivityIds(enabledActivityIds); 

IActivityManager also defines protocol for getting all defined activity and category ids, and for getting the associated IActivity or ICategory for a particular id. These objects can be used to traverse the definition for an  activity or category in API, such as getting the pattern bindings or requirement  bindings. Listeners can be registered on the activity manager or on the  activities and categories themselves to detect changes in the definition of a particular activity or in the activity manager itself. See the package  org.eclipse.ui.activities for more information.

 

一種使用場合

對于RCP應用來說,一般都會存在普通用戶和管理員用戶兩種角色,因此在設計的時候,會將管理員的功能集中到一個插件中,這樣可以獨立升級管理員插件的功能,避免兩者不必要的相互干擾。但是由于eclipse rcp沒有提供比較完善的perspective控制機制,表現在很難針對perspective bar進行定制(即在任何情況下,所有perspetive都將顯示,并不能根據用戶的角色不同來只顯示部分perspective)。這就導致了普通用戶也能切換到管理員perspetive下的問題,以往的解決方案是在管理員視圖的view中硬編碼,每一個view都需要判斷當前用戶的角色,很不爽。現在有了activities的支持,就可以以聲明的方式實現view中可用性,view不再需要關心當前用戶的角色,靈活、方便了很多。

 

 

參考

  • Creating a declarative security model for RCP applications

http://www.ibm.com/developerworks/opensource/library/os-ecl-rcpsec/?S_TACT=105AGX52&S_CMP=cn-a-os

  • eclipse 幫助文檔