Eclipse Action 6

本文介绍了Eclipse UI中如何使用可见性配置来控制对象贡献的可见性和可用性,包括可见度要素、过滤器元素、选择元素、可用性元素等内容敏感的对象贡献等关键概念。

6.3.2.2. 可见度要素

可见度要素相比于对象贡献的nameFilter objectClass属性,提供了另一种功能强大的方式来指定什么时候对象贡献的action对用户可用,例如,为对象贡献指定过滤方式,可以像下面这样:

<objectContribution ...

<visibility>

<objectClass

name="org.eclipse.core.resources.IResource"/>

</visibility>

...the other stuff here...

</objectContribution>

如果action只对只读资源可见,那么可见性对象贡献可以像如下这样:

<objectContribution ...>

<visibility>

<and>

<objectClass

name="org.eclipse.core.resources.IResource"/>

<objectState name="readOnly" value="false"/>

</and>

</visibility>

... the other stuff here ...

</objectContribution>

作为<visibility>元素的一部分,你可以为逻辑表达式使用嵌套的<and>, <or> 和 <not>元素,在加上如下的Boolean表达式。

adapt 使选择的对象适配指定类型,然后可以在任何子表达式中使用新对象。例如,如果你想要使选择的对象(见 20.3节, 适配器)适配一种资源,然后测试一些资源对象状态,那么表达式可以如下:

<adapt type="org.eclipse.core.resources.IResource">

<objectState name="readOnly" value="false"/>

</adapt>

适配表达式的孩子使用 and 操作符组合起来。如果适配器或者引用的类型都没有被装载,该表达式返回了EvaluationResult.NOT_LOADED。如果类型名不存在,它会在求值的时候抛出ExpressionException异常。

and 如果所有的子元素值为true,则值为true

instanceof 使用名字来比较选择对象的类。这和objectClass 元素是一样的,只是可以使用and 和 or元素使instanceof和其他的元素组合起来。

not 如果一个元素为false,则为true。

objectClass 向上面描述的那样,用名字比较选择的对象的类。

objectState 和过滤器元素相似,使用指定的状态比较选择对象的状态。(见 6.3.2.3节, 过滤器元素)。

or 如果一个元素为true,则值为true。

pluginState 比较插件的状态,显示其是否被装载或激活。例如,只有org.eclipse.pde插件被装载了,如下的表达式<pluginState id="org.eclipse.pde" value="installed"/> 才会使一个对象贡献成为可见的;只有org.eclipse.pde插件被激活,表达式<pluginState id="org.eclipse.pde" value="activated"/>才能使一个对象贡献可见

systemProperty 比较系统属性。例如,如果希望一个对象贡献只有在英文状态下才可见,可以用如下表达式: <systemProperty name="user.language" value="en"/>

systemTest 和systemProperty 元素一样,只是 systemTest 可以使用and 和or 进行组合。

test 设置对象的属性状态。例如,如果一个对象贡献只有在Java项目中的元素被选中时,才处于可见状态,可以使用如下表达式:

<test

property="org.eclipse.debug.ui.projectNature"

value="org.eclipse.jdt.core.javanature"/>

如果执行实际测试的属性测试器没有被装载, test 表达式会返回EvaluationResult.NOT_LOADED。可测试的属性集可以使用org.eclipse.core.expressions.propertyTesters 扩展点来扩展。org.eclipse.debug.internal.ui.ResourceExtender 类是一个很不错的例子。

6.3.2.3. 过滤器元素

过滤器元素比前面讨论的objectState 元素要简单。例如,如果对象贡献只提供给非只读文件,可以使用如下的表达式:

<objectContribution ...>

<filter name="readOnly" value="false"/>

... the other stuff here ...

</objectContribution>

objectState 元素一样,, 过滤器元素使用IActionFilter 接口来决定是否选择的对象符合标准。 每个选中的对象必须实现IActionFilter 接口 (参见 20章, 高级主题) ,实现testAttribute() 方法,根据指定对象的状态来测试指定的名字/值。对于资源, Eclipse 提供了如下的内置状态比较关系,在org.eclipse.ui.IResourceActionFilter 类中列出:

name 比较文件名。 "*" 可以用在开头或结尾来代表一个或多个字符。

extension 比较文件扩展名。

path 比较文件路径。"*" 可以用在开头或结尾来代表一个或多个字符。

readOnly 比较文件的只读属性。

projectNature 比较项目特性。

persistentProperty 比较选中资源的持久化属性。如果值是简单的字符串,就检测资源该属性是否存在。如果是propertyName=propertyValue的格式, 就使用指定的名字获取属性的值,检测它与指定值是否相等。

projectPersistentProperty 比较选中资源项目的持久化属性,在语义上与上面的persistentProperty 很相似。

sessionProperty比较选中资源的session属性,在语义上与上面的persistentProperty 很相似。

projectSessionProperty比较选中资源项目的session属性,在语义上与上面的persistentProperty 很相似。

6.3.2.4. 选择元素

选择元素是基于action的名字和类型来设置单个action的可用性,这一点与nameFilter 和 objectClass 属性决定是否一个对象贡献的所有action 都是可见的很相似。

<objectContribution

objectClass="java.lang.Object"

id="com.qualityeclipse.favorites.popupMenu">

<action

label="Add to Favorites"

tooltip="Add the selected resource(s) to the

Favorites view"

class="com.qualityeclipse.favorites.actions.

AddToFavoritesActionDelegate"

menubarPath="additions"

enablesFor="+"

id="com.qualityeclipse.favorites.addToFavorites">

<selection

class="org.eclipse.core.resources.IResource"

name="*.java"/>

</action>

</objectContribution>

使用了此声明,对象贡献的action将永远可见,但是Add to Favorites action只有在选择的元素中有IResource的实现者,而且名字符合*.java的时候才可用。

6.3.2.5. 可用性元素

可用性元素相比选择元素,是一种功能更强的选择。它支持复杂的条件逻辑表达式和可见度元素比较关系(参见 6.3.2.2节, 可见度元素)。例如, 如下的声明同上面的声明拥有同样的声明

<objectContribution

objectClass="java.lang.Object"

id="com.qualityeclipse.favorites.popupMenu">

<action

label="Add to Favorites"

tooltip="Add the selected resource(s)

to the Favorites view"

class="com.qualityeclipse.favorites.actions.

AddToFavoritesActionDelegate"

menubarPath="additions"

enablesFor="+"

id="com.qualityeclipse.favorites.addToFavorites">

<enablement>

<and>

<objectClass

name="org.eclipse.core.resources.IResource"/>

<objectState name="name" value="*.java"/>

</and>

</enablement>

</action>

</objectContribution>


<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

6.3.2.6. 内容敏感的对象贡献

这是一种基于资源内容过滤action的机制。在插件清单文件中指定过滤条件,通过检查文件内容来决定action是否可见或者是否可用。例如,Run Ant... 命令只与build.xml文件相关联,如果Ant脚本写在了export.xml中怎么办? 这种新机制能够基于文件的第一个XML标签或指定的DTD来决定Run Ant...命令是否可见。 org.eclipse.ant.core 插件定义了一个新的antBuildFile 内容类型:

<extension point="org.eclipse.core.runtime.contentTypes">

<content-type

id="antBuildFile"

name="%antBuildFileContentType.name"

base-type="org.eclipse.core.runtime.xml"

file-names="build.xml"

file-extensions="macrodef,ent,xml"

priority="normal">

<describer

class="org.eclipse.ant.internal.core.

contentDescriber.AntBuildfileContentDescriber">

</describer>

</content-type>

</extension>

上面的声明将antBuildFile 内容类型关联到AntBuildfileContentDescriber 类,此类用来检查是否文件为Ant脚本。antBuildFile 内容类型能够用来指定action的可见度和可用性,编辑器关联,还有更多。想要得到更多的声明和使用你自己的内容类型,参见如下内容

  • "Content Sensitive Object Contributions" 位于 eclipse.org > projects > The Eclipse Project > Platform > UI > Development Resources > Content Sensitive Object Contributions, 或者浏览 dev.eclipse.org/viewcvs/index.cgi/~checkout~/platform-ui-home/object-aware-contributions/objCont.htm.

  • "Content types" 位于Eclipse帮助系统 Help > Help Contents > Platform Plug-in Developer Guide > Programmer's Guide > Runtime overview > Content types

  • "A central content type catalog for Eclipse" 位于 dev.eclipse.org/viewcvs/index.cgi/platform-core-home/documents/content_types.html?rev=1.11

"Content types in Eclipse" 位于eclipse.org/eclipse/platform-core/planning/3.0/plan_content_types.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值