org.eclips.core.expression的基本结构
Expression的运算结果是返回true或者false,用来处理逻辑结构。
not 用来对表达式运行结果取反
and 用来在多个表达式之间进行与运算,只有多个表达式都为true的时候,才会返回true,否则返回false
or 用来在多个表达式之间进行或运算,只有多个表达式都为false的时候,才会返回false,否则返回true
instanceof 用来判断指定的对象是否是指定类或者接口的实例
systemTest 用来比较System系统变量中的值是否与指定值相等
iterate 用来处理java.util.Collection的所有子对象
equals 用来判断指定对象与预先设置的数据是否相等
count 用来处理java.util.Collection的集合大小
with
resolve
adapt 用来将选中对象转成预先指定的对象,以便更灵活的处理
test 基于最重要的人物总是最后出场的原则,将adapt和test放在最后进行说明。
org.eclipse.core.expression详细配置说明
not
not本身并没有具体的信息,只是将其子表达式的结果进行取反。通常的配置如下:
<not>
<instanceof value="org.eclipse.core.resources.IFile"/>
</not> 如果选中的对象不是文件,会返回true,因为not将instanceof的结果取反。
and
与not功能相类似,and是将所有的子结点结果进行与运算。
<and>
<instanceof value="org.eclipse.core.resources.IFile"/>
<test property="org.demo.matchesPattern" value="*.html"/>
</and> 只有选中的对象是文件,而且名称符合*.html才会返回true。
or
与not功能相类似,and是将所有的子结点结果进行与运算。
<or>
<instanceof value="org.eclipse.core.resources.IFolder"/>
<instanceof value="org.eclipse.core.resources.IProject"/>
</or> 只有选中的对象是目录或者是项目,就会返回true,如果是文件,就会返回false。
instanceof
用来判断指定的对象是否是指定类或者接口的实例
<instanceof value="org.eclipse.core.resources.IFile"/> 只有选中的对象是文件的实例时,如org.eclipse.core.resource.internal.File时,才会返回true.
systemTest
用来比较System系统变量中的值是否与指定值相等,它会调用System.getProperty("xxx")方法取得系统变量值,,并将该值与指定值进行比较。
<systemTest property="encoding" value="GB2312"/> 那么该配置只有当前JVM的默认编码为GB2312(即中文时),才会返回true.
iterate
iterate是一个非常少用的属性,它主要用来处理java.util.Collection中子元素的内容。它会逐渐遍历集合的所有子元素,然后再将每个子元素放入子表达式中进行计算,其实operator有or和and两种可选,是用来支持子表达的或运算和与运算,与前面的or和and相似。
<iterate operator="or">
<instanceof value="org.eclipse.core.resources.IFile"/>
</iterate>
equals
在配置文件中,其实只有一种字符串数据类型,而在Java中的数据类型丰富的多,因此Eclipse提供了一整套的转换机制,用来将字符串转成相应的Java类型,下面是相应的转换规则:1. 对于字符串"true",将会转成Boolean.TRUE2. 对于字符串"false",将会转成Boolean.FALSE3. 如果字符串中有小数点,将会转成浮点数Float4. 如果字符串中只有数字,就会转成整数Integer5. 如果以上的转换出现错误,或者不符合以上情况,就会转成字符串6. 如果希望使用字符串true,或者1.2之类的数据,而不被转成Boolean.True和相应的Float,就使用单绰号',如"''true",就会转成"true",而不是Boolean.True
<equals value="100"/>
count
用来验证一个java.util.Collection中的size是否符合要求,这个值可以有以下几种:v *,表示任意数量v ?,表示0或1个数量v +,表示至少一个数量v 数字,表示size要等于这个指定的数量
<count value="0" /> 这个属性虽然用的少,但是却不可少,以资源管理器为例,当一个工作区没有任何项目的时候,那么对应的size就为0,如果需要在这种情况出现一个菜单,那么就肯定需要这样一个表达式才能正确的处理。
with
通过variable属性取得选中对象的相应数据,比如通过选中对象的getAffectedProjects方法得到一个java.util.Collection对象,再通过iterate和test来进行表达式处理。
<with variable="affectedProjects">
<iterate operator="or">
<test property="org.demo.projectNature" value="org.eclipse.jdt.core.javanature"/>
</iterate>
</with>
resolve
它和with属性基本一致,但是它多了一个args属性,从而有更高的灵活性,其它方面与with并无区别。
<resolve variable="pluginDescriptor" args="org.eclipse.core.runtime">
<test property="org.demo.isActive"/>
</resolve>
adapt
adapter是Eclipse架构的根本所在,它不仅仅解决了上下文环境的问题,对系统的扩展和灵活性都是一个非常好的解决方案,因此在表达式中也充分的利用这个特点。它可以将待处理的对象转换成指定的对象,这样也会有效的增加表达式的灵活性。
<adapt type="org.eclipse.core.resources.IResource">
<test property="org.eclipse.core.resources.extension" value="datasetx"/>
</adapt> 它表示会先将指定的对象通过adapter方式转成一个IResource的实例,如果转换成功,再判断其扩展名是否为datasetx,如果转换不成功,也会返回false。
test
以上提供了很多功能,但是可能对于开发人员,并不能完全满足他们相应的需求。所以Expression插件提供了test结点,这个结点允许用户通过一个扩展点来定义各种新的表达式处理功能,象org.eclipse.core.resources就提供了对扩展名等一系列的支持。在以后的文章将会详细的描述如何进行扩展。
<adapt type="org.eclipse.core.resources.IResource">
<test property="org.eclipse.core.resources.extension" value="datasetx"/>
</adapt> 这个例子会调用,org.eclipse.core.internal.propertytester.ResourcePropertyTester类来处理数据,从而获得更高的灵活性。
我测试了一个with 是不可以自定义参数的,只能用系统默认的几个:
activeContexts | A java.util.Collection ofjava.lang.String | This is a collection of the active context IDs as strings. Most commonly used with <iterate/>, <count/>, and <test/> with a combined | 3.2 |
activeActionSets | An IActionSetDescriptor[] | Note: This is currently not used as it points to an internal class and the type might change in any release. | 3.2 |
activeShell | org.eclipse.swt.widgets.Shell | The currently active shell. It can be a dialog or workbench window shell. | 3.2 |
activeWorkbenchWindowShell | org.eclipse.swt.widgets.Shell | The active workbench window shell. | 3.2 |
activeWorkbenchWindow | org.eclipse.ui.IWorkbenchWindow | The active workbench window. | 3.2 |
activeWorkbenchWindow.isCoolbarVisible | java.lang.Boolean | Reports coolbar visibility for the currently active workbench window. | 3.3 |
activeWorkbenchWindow.isPerspectiveBarVisible | java.lang.Boolean | Reports perspective bar visibility for the currently active workbench window. | 3.3 |
activeWorkbenchWindow.activePerspective | java.lang.String | Reports the name of the current perspective of the active workbench window. | 3.4 |
activeEditor | org.eclipse.ui.IEditorPart | The currently active editor. This is remembered even if the editor is not the currently active part. | 3.2 |
activeEditorId | java.lang.String | The ID of the currently active editor. This can be used for expressions on the editor type. | 3.2 |
activePart | org.eclipse.ui.IWorkbenchPart | The active part, which can be the same as the active editor. | 3.2 |
activePartId | java.lang.String | The ID of the currently active part. | 3.2 |
activeSite | org.eclipse.ui.IWorkbenchPartSite | The site of the currently active part. | 3.2 |
selection | org.eclipse.jface.viewers.ISelection | The current global selection. It is often used with <test/> elements with | 3.2 |
activeMenu | A java.util.Collection ofjava.lang.String | This is the list of IDs of the showing context menu. Examples are like #TextEditorRuler or a part ID. Most commonly used with <iterate/>, <count/>, and <test/> with a combined | 3.2 |
activeMenuSelection | org.eclipse.jface.viewers.ISelection | This is a selection that is available while a context menu is showing. It is the selection from the selection provider used to register the context menu, usually from | 3.3 |
activeMenuEditorInput | org.eclipse.jface.viewers.ISelection | This is a selection that is available while a context menu is showing. It is the selection from the editor input, usually if includeEditorInput was set to | 3.3 |
activeFocusControl | org.eclipse.swt.widgets.Control | A control that has focus and has been registered with the IFocusService. | 3.3 |
activeFocusControlId |
所以只有test是可以自定义的
test是灵活性最高的一种,可以直接通过代码来控制条件。
如: public class EPropertyTester extends PropertyTester
public boolean test(Object receiver, String property, Object[] args,
Object expectedValue) {
if(receiver instanceof User)
{
User user=(User) receiver;
return user.getName().equals("aaa");
}
return false;
}
定义扩展点
<extension
point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
class="tester.EPropertyTester"
id="tester"
namespace="gef.propertyTester3"
properties="aa"
type="viewer.User">
</propertyTester>
</extension>
<handler
class="gef.handlers.SampleHandler"
commandId="gef.commands.sampleCommand">
<enabledWhen>
<with
variable="selection">
<iterate
ifEmpty="false"
operator="and">
<test
args="aa,bb,cc"
forcePluginActivation="true"
property="gef.propertyTester3.aa"
value="abc">
</test>
</iterate>
</with>
</enabledWhen>
</handler>
在调用propertyTesters的扩展时,是通过 property="gef.propertyTester3.aa" 来调用的,空间名.属性名的形式
调用。
<menuContribution
locationURI="menu:gef.view2?after=additions">
<command
commandId="gef.commands.sampleCommand"
id="gef.menus.sampleCommand"
mnemonic="S">
<visibleWhen
checkEnabled="true"> 设为true,标明如何Enabled为true,就显示,Enabled为tfalse就不显示。
</visibleWhen>
</command>
</menuContribution>
可以参考一下eclipse源码。
http://www.ceclipse.org/read-cec-tid-28291.html
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework