Eclipse Resource Plugin里面很多值得一看的东西,其中一个就是Property Testers ,它在某些条件过滤的时候非常有用,先看一段jdt里面的使用
<page objectClass="org.eclipse.core.resources.IProject" name="%javaCategoryPageName" class="org.eclipse.jdt.internal.ui.preferences.JavaCategoryPropertyPage" id="org.eclipse.jdt.ui.propertyPages.JavaCategoryPropertyPage"> <enabledWhen> <adapt type="org.eclipse.core.resources.IProject"> <test property="org.eclipse.core.resources.projectNature" value="org.eclipse.jdt.core.javanature"/> </adapt> </enabledWhen> </page>
这里的
org.eclipse.core.resources.projectNature
就是eclipse已经定义好的property。
简介
扩展点:org.eclipse.core.expressions.propertyTesters
作用:用来给一个已知的类型添加属性测试支持,新添加的属性可以用于各种表达式条件判断。
扩展语法:
<!ELEMENT propertyTester EMPTY>
<!ATTLIST propertyTester
id CDATA #REQUIRED
type CDATA #REQUIRED
namespace CDATA #REQUIRED
properties CDATA #REQUIRED
class CDATA #REQUIRED>
引用一下Eclipse自己的解释:
- id - unique identifier for the property tester
- type - the type to be extended by this property tester
- namespace - a unique id determining the name space the properties are added to
- properties - a comma separated list of properties provided by this property tester
- class - the name of the class that implements the testing methods. The class must be public and extend org.eclipse.core.expressions.PropertyTester with a public 0-argument constructor.
示例
<extension point="org.eclipse.core.expressions.propertyTesters"> <propertyTester id="org.eclipse.core.expressions.bundlePropertyTester" type="org.eclipse.core.runtime.Platform" namespace="org.eclipse.core.runtime" properties="product,isBundleInstalled,bundleState" class="org.eclipse.core.internal.expressions.propertytester.PlatformPropertyTester"> </propertyTester> </extension>
很简单的一个使用,这些都在eclipse的文档里面说的很清楚了。
至于org.eclipse.core.expressions.PropertyTester 如何扩展,也很简单,一般只需要实现test (Object receiver, String property, Object[] args, Object expectedValue)方法就行了。比如:
/*******************************************************************************
* Copyright (c) 2006, 2008 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.core.internal.expressions.propertytester;
import org.osgi.framework.Bundle;
import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.core.runtime.IProduct;
import org.eclipse.core.runtime.Platform;
/**
* A property tester for testing platform properties. Can test whether or
* not a given bundle is installed in the running environment, as well as
* the id of the currently active product.
* <p>
* For example:<br />
* <test property="org.eclipse.core.runtime.product" value="org.eclipse.sdk.ide"/> <br />
* <test property="org.eclipse.core.runtime.isBundleInstalled" args="org.eclipse.core.expressions"/> <br />
* <test property="org.eclipse.core.runtime.bundleState" args="org.eclipse.core.expressions" value="ACTIVE"/>
* <p>
*/
public class PlatformPropertyTester extends PropertyTester {
private static final String PROPERTY_PRODUCT = "product"; //$NON-NLS-1$
private static final String PROPERTY_IS_BUNDLE_INSTALLED = "isBundleInstalled"; //$NON-NLS-1$
private static final String PROPERTY_BUNDLE_STATE = "bundleState"; //$NON-NLS-1$
/* (non-Javadoc)
* @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object, java.lang.String, java.lang.Object[], java.lang.Object)
*/
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
if (Platform.class.equals(receiver)) {
if (PROPERTY_PRODUCT.equals(property)) {
IProduct product= Platform.getProduct();
if (product != null) {
return product.getId().equals(expectedValue);
}
return false;
} else if (PROPERTY_IS_BUNDLE_INSTALLED.equals(property) && args.length >= 1 && args[0] instanceof String) {
return Platform.getBundle((String)args[0]) != null;
} else if (PROPERTY_BUNDLE_STATE.equals(property)
&& args.length >= 1 && args[0] instanceof String) {
Bundle b = Platform.getBundle((String) args[0]);
if (b != null) {
return bundleState(b.getState(), expectedValue);
}
return false;
}
}
return false;
}
private boolean bundleState(int bundleState, Object expectedValue) {
if ("UNINSTALLED".equals(expectedValue)) { //$NON-NLS-1$
return bundleState == Bundle.UNINSTALLED;
}
if ("INSTALLED".equals(expectedValue)) { //$NON-NLS-1$
return bundleState == Bundle.INSTALLED;
}
if ("RESOLVED".equals(expectedValue)) { //$NON-NLS-1$
return bundleState == Bundle.RESOLVED;
}
if ("STARTING".equals(expectedValue)) { //$NON-NLS-1$
return bundleState == Bundle.STARTING;
}
if ("STOPPING".equals(expectedValue)) { //$NON-NLS-1$
return bundleState == Bundle.STOPPING;
}
if ("ACTIVE".equals(expectedValue)) { //$NON-NLS-1$
return bundleState == Bundle.ACTIVE;
}
return false;
}
}