filter元素是用来指定当前的contribution是否可用, 它的用法跟visibility中的objectState类似, 比如只有选择的对象为只写才可用, 那么可以这样写:
- <objectContribution ...>
- <filter name="readOnly" value="false"/>
- ... the other stuff here ...
- objectContribution>
和objectState 一样,filter使用的进行测试的对象必须实现IActionFilter接口或者能适配为IActionFilter, 然后调用testAttribute()方法来跟指定的name/value进行比对, 对于Resource来说, 它提供了下列属性让我们来进行比对
name,表示对文件名进行比对, 可使用*这种正则表达式
extension表示后缀名比对
path路径比对, 可使用*
readOnly只读比对
projectNature 对nature比对
persistentProperty 对选择的资源的持久属性进行比对, 如果他的值只是一个字符串,那么就对该资源已有的属性值拿出来进行比对, 如果是propertyName=propertyValue这种形式,那么先根据name从选择的对象中取出值,然后再跟指定的value进行比对
projectPersistentProperty 对选中对象所在工程的持久性属性进行比对
sessionProperty 对对象中对的session属性进行比对
projectSessionProperty 选中对象所在工程的session属性进行比对
selection 元素主要针对contribution中某个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>
这里的意思是com.qualityeclipse.favorites.popupMenu下的action在任何情况下都是可见的,只有Add to Favorites只有在选中的对象实现了IResource接口且文件名后缀为java才可见
enablement元素 也是针对单个action的, 它集selection和visibility两个元素的强大于一身,举个例子就明白了:
- <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>
根据文件内容关联contribution
比如一般我们的Run Ant...菜单项是跟build.xml文件关联的, 如果要跟export.xml也关联, 那么我们就需要通过对xml的DTD声明进行判断是否可以关联, 为了处理这种情况org.eclipse.ant.core 定义了一种叫antBuildFile 的content type:
- <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>
在AntBuildfileContentDescriber 类中将判断xml文件是否是ant内容, 接下来我们就可以通过使用antBuildFile来指定contibution是否可用.
如果想了解如何自定义内容类型,可以参考:
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
Help > Help Contents > Platform Plug-in Developer Guide > Programmer's Guide > Runtime overview > Content types
"A central content type catalog for Eclipse" at dev.eclipse.org/viewcvs/index.cgi/platform-core-home/documents/content_types.html?rev=1.11
"Content types in Eclipse" at eclipse.org/eclipse/platform-core/planning/3.0/plan_content_types.html