`

Eclipse Action 深入学习笔记(3)

阅读更多

filter元素是用来指定当前的contribution是否可用, 它的用法跟visibility中的objectState类似, 比如只有选择的对象为只写才可用, 那么可以这样写:

xml 代码
  1. <objectContribution ...>  
  2.    <filter name="readOnly" value="false"/>  
  3.    ... the other stuff here ...   
  4. 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是否可见来进行判断,比如说下面这样写:

xml 代码
  1. <objectContribution  
  2.       objectClass="java.lang.Object"  
  3.       id="com.qualityeclipse.favorites.popupMenu">  
  4.    <action  
  5.          label="Add to Favorites"  
  6.          tooltip="Add the selected resource(s) to the   
  7.                   Favorites view"   
  8.          class="com.qualityeclipse.favorites.actions.   
  9.                 AddToFavoritesActionDelegate"   
  10.          menubarPath="additions"  
  11.          enablesFor="+"  
  12.          id="com.qualityeclipse.favorites.addToFavorites">  
  13.       <selection  
  14.             class="org.eclipse.core.resources.IResource"  
  15.             name="*.java"/>  
  16.    action>  
  17. objectContribution>  

这里的意思是com.qualityeclipse.favorites.popupMenu下的action在任何情况下都是可见的,只有Add to Favorites只有在选中的对象实现了IResource接口且文件名后缀为java才可见

enablement元素 也是针对单个action的, 它集selection和visibility两个元素的强大于一身,举个例子就明白了:

 

xml 代码

 

  1. <objectContribution  
  2.       objectClass="java.lang.Object"  
  3.       id="com.qualityeclipse.favorites.popupMenu">  
  4.    <action  
  5.          label="Add to Favorites"  
  6.          tooltip="Add the selected resource(s)   
  7.                   to the Favorites view"   
  8.          class="com.qualityeclipse.favorites.actions.   
  9.                 AddToFavoritesActionDelegate"   
  10.          menubarPath="additions"  
  11.          enablesFor="+"  
  12.          id="com.qualityeclipse.favorites.addToFavorites">  
  13.       <enablement>  
  14.          <and>  
  15.             <objectClass  
  16.                name="org.eclipse.core.resources.IResource"/>  
  17.              <objectState name="name" value="*.java"/>  
  18.          and>  
  19.       enablement>  
  20.    action>  
  21. objectContribution>  

根据文件内容关联contribution
比如一般我们的Run Ant...菜单项是跟build.xml文件关联的, 如果要跟export.xml也关联, 那么我们就需要通过对xml的DTD声明进行判断是否可以关联, 为了处理这种情况org.eclipse.ant.core 定义了一种叫antBuildFile 的content type:

xml 代码
  1. <extension point="org.eclipse.core.runtime.contentTypes">  
  2.    <content-type  
  3.       id="antBuildFile"  
  4.       name="%antBuildFileContentType.name"  
  5.       base-type="org.eclipse.core.runtime.xml"  
  6.       file-names="build.xml"  
  7.       file-extensions="macrodef,ent,xml"  
  8.       priority="normal">  
  9.       <describer  
  10.          class="org.eclipse.ant.internal.core.   
  11.             contentDescriber.AntBuildfileContentDescriber">  
  12.       describer>  
  13.    content-type>  
  14. 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

分享到:
评论
2 楼 macrochen 2007-12-26  
这个没碰到过
1 楼 solonote 2007-12-25  
我要做一个弹出菜单,这个菜单有一个Action A,如果选中的对象包含了main方法则显示不包含则不显示,好像要用
org.eclipse.core.expressions.propertyTesters扩展点
但这个扩展点如何使用还不是很明白,请问能否指点?

相关推荐

Global site tag (gtag.js) - Google Analytics