问题
I have a perspective consisting of multiple ViewPart
s. One of these ViewPart
s has a command assigned using a menuContribution
with locationURI="toolbar:..."
.
The ViewPart
with the Toolbar-Command only contains one Child (a TreeView
) and does this.getSite().setSelectionProvider(child)
in it's createPartControl(Composite)
-Method.
The problem is, that when i click one of the other ViewPart
s in my Perspective the Command gets disabled, but the selection does resist in the TreeView
-Component.
I guess the enabledWhen
-Condition is not valid anymore, because the selection
-Variable now points to stuff that resists in another ViewPart.
This distracts, because the Toolbar-Button should be activated, when a valid row in the TreeView
is selected.
How do i tell my Core Expression, that it only should check the selection of the ViewPart
the Toolbar resists in and not the global-selection?
回答1:
I had a similar problem, and what I ended up doing was implementing my own property tester. This is probably the easiest way, because the predefined selection
variable will always point to the global selection.
Roughly, you will need to follow the steps below.
add
org.eclipse.core.expressions
plug-in to your dependency.define an extension for
org.eclipse.core.expressions.propertyTesters
extension point. This would look something like the following (documentation link):<extension point="org.eclipse.core.expressions.propertyTesters"> <propertyTester class="your.package.TreeItemSelectionPropertyTester" id="your.package.treeItemSelectionPropertyTester" namespace="your.namespace" properties="itemSelected" type="java.lang.Object"> </propertyTester> </extension>
Implement your PropertyTester code. As the above documentation says, the class should be public and should extend
org.eclipse.core.expressions.PropertyTester
class.public class CodeSelectionPropertyTester extends PropertyTester { @Override public boolean test(Object receiver, String property, Object[] args, Object expectedValue) { if (property.equals("itemSelected")) { // write your own code to test // whether there is a valid selection... } // ... } }
Use the implemented property tester in your enabledWhen clause.
<enabledWhen> <test property="your.namespace.itemSelected"> </test> </enabledWhen>
来源:https://stackoverflow.com/questions/18272671/enabling-disabling-toolbar-command-based-on-selection-in-viewpart-not-perspecti