I have a view with a command. I have the command appearing in the view's toolbar. The view has a TableViewer. I register the viewer as the selection provider for the workbench site, so that I can use a selection-based enablement expression in the comand handler definition (in the plugin.xml).
Here's my problem: the selection is a TreeNode. TreeNode.getValue() returns the *real* view model element for my view--let's say, com.acme.Customer. Now, I want the command to be enabled if the user selects a Customer, but disabled if he selects something else. (a) I can't figure out how to do that via the enablement expression. Doing an instanceof on the variable=selection won't help since that's a TreeNode, not a Customer (b) What if I wanted an even more complex enablement--one that needs to be programatic? The handler isn't called when I change the selection.
I've looked at a million write-ups on the web on commands and handlers and I'm just not finding the answer to what seems like a fairly simple thing. If would appreciate a pointer in the right direction.
The millionth-and-tenth writeup finally clued me in. The answer is to use a custom property tester. A property tester is a class which can be (indirectly) referenced in an enablement expression and which is instantiated and exercised at expression evaluation time. You can develop a property tester to do any amount of complex programmatic testing of, say, the viewer selection and respond with a true/false as to whether the command should be enabled (assuming you're using it in an "enabledWhen" element of a handler definition).