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 帮助文档