Enabling/Disabling Toolbar-Command based on selection in ViewPart, not Perspective

不羁的心 提交于 2019-12-25 11:10:00

问题


I have a perspective consisting of multiple ViewParts. One of these ViewParts 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 ViewParts 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.

  1. add org.eclipse.core.expressions plug-in to your dependency.

  2. 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>
    
  3. 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...
            }
    
            // ...
        }
    }
    
  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!