Eclipse command framework core expression: Property tester
Addition to Eclipse command framework series, this post explains use of property tester to control command’s enable/visible properties. In earlier post we have tried declarative way to achieve same thing but property tester gives us more control.
Property tester is associated with, tester class which implements PropertyTester.java, a namespace and set of property which it supports. We can refer this property tester using its namespace + a property name. It gives you call back to test() method with property name as parameter inside property tester class. We can configure property tester for type of input parameter.
Here is sample declaration of property tester extension point
<extension
point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
class="com.example.advance.cmd.EditorTester"
id="com.example.advance.cmd.propertyTester1"
namespace="com.example.advance.cmd"
properties="isEditor1,isEditor2"
type="org.eclipse.ui.editors.text.TextEditor">
</propertyTester>
</extension>
- Type : type to be extended by property tester
- class:Property tester class which extends “org.eclipse.core.resources.PropertyTester” class and provides implementation for test() method.
- namesopace: distinguish this property tester from other.
- properties: Properties supporter by this property tester.
Note: To extend “org.eclipse.core.expression.propertytester” extension point, plug-in project needs “org.eclipse.core.expression” dependency.
Extending “org.eclipse.core.resources.propertytester” extension point.
Here is the sample PropertyTester’s implementation
package com.example.advance.cmd;
import org.eclipse.core.expressions.PropertyTester;
import com.example.advance.cmd.editor.Editor1;
import com.example.advance.cmd.editor.Editor2;
public class EditorTester extends PropertyTester{
@Override
public boolean test(Object receiver, String property, Object[] args,
Object expectedValue) {
if("isEditory1".equalsIgnoreCase(property)){
if(receiver instanceof Editor1){
return true;
}
}else if("isEditor2".equalsIgnoreCase(property)){
if(receiver instanceof Editor2){
return true;
}
}
return false;
}
}
Using property tester for enable command
PLugin.xml will be like below
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="true"
locationURI="popup:#TextEditorContext?after=additions">
<command
commandId="com.example.advance.cmd.command1"
label="Command One"
style="push"
tooltip="Command 1">
<visibleWhen
checkEnabled="true">
</visibleWhen>
</command>
</menuContribution>
</extension>
<extension
point="org.eclipse.ui.handlers">
<handler
class="com.example.advance.cmd.Cmd1Handler1"
commandId="com.example.advance.cmd.command1">
<enabledWhen>
<with
variable="activeEditor">
<test
forcePluginActivation="true"
property="com.example.advance.cmd.isEditor1">
</test>
</with>
</enabledWhen>
</handler>
</extension>
Here I have enabled “Command One” only for test “com.example.advance.cmd.iEditor2” property test and visible only when it is enable. Result of this you can notice here that “Command One” is visible only in Editor1 not in Editor2 in images below.
Conventional properties are accessible across platforms and surfers whereas nonstandard properties can involve platform-specific properties.
ReplyDeleteProperty Management Charlotte
cool thanks
ReplyDeleteI hope to see more post from you. Thank you for sharing this post. Your blog posts are more interesting and impressive
ReplyDeleteUmeed Career Portal
Your blog is filled with unique good articles! I was impressed how well you express your thoughts.
ReplyDeleteFBB SBI Styleup Credit Card 2021 | FBB SBI Styleup Card Offers and Features