Home > database >  How to add a command to an editor context menu in Eclipse only if text is selected
How to add a command to an editor context menu in Eclipse only if text is selected

Time:12-07

I would like to add an item to the eclipse context menu only if a text is marked as selected
below is my XML relevant snippet:

<menuContribution
    allPopups="false"
    locationURI="popup:org.eclipse.ui.popup.any">
  <command
      commandId="com.test.ide.menu.commands.sampleCommand"
      icon="icons/sample.png"
      id="com.test.ide.menu.toolbars.sampleCommand"
      tooltip="Popup test">
    <visibleWhen>
        <with variable="selection">
            <instanceof value="org.eclipse.jface.text.ITextSelection"/>
        </with>
    </visibleWhen>
  </command>
</menuContribution>

I tried the solution in How do you contribute a command to an editor context menu in Eclipse which does not resolve the issue

What am i missing?
Can you help?

Thanks
Avner

Excuse my detailed question, I am a newb to eclipse programming
i tried:

<extension
         point="org.eclipse.core.expressions.propertyTesters">
      <propertyTester
            
            id="com.test.ide.propertyTester1"
            namespace="tested"
            properties="textSelected"
            type="org.eclipse.jface.text.ITextSelection">
      </propertyTester>
</extension>

<menuContribution
    allPopups="false"
    locationURI="popup:org.eclipse.ui.popup.any">
  <command
      commandId="com.test.ide.menu.commands.sampleCommand"
      icon="icons/sample.png"
      id="com.test.ide.menu.toolbars.sampleCommand"
      tooltip="Popup test">
    <visibleWhen>
        <with variable="selection">
            <adapt type="org.eclipse.jface.text.ITextSelection">
                <test
                    property="tested.textSelected">
                </test>
            </adapt>
        </with>
    </visibleWhen>
  </command>
</menuContribution>

created a new package - com.test.ide in it created a new class TextSelectedPropertyTester
however the class is never invoked

package com.test.ide;
import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.jface.text.ITextSelection;


public class TextSelectedPropertyTester extends PropertyTester
{
    
    
    
  @Override
  public boolean test(Object receiver, String property, Object [] args, Object expectedValue)
  {
      System.out.println("testing");
      if (receiver instanceof ITextSelection) {
          return ((ITextSelection)receiver).getLength() > 0;
      }

    return false;
  }
}

however the class is never used

what am I missing?

CodePudding user response:

The problem is that editors normally always have a text selection set - it will just be zero length if no characters are selected.

I can't see a way to test this using existing expressions so it may be necessary to define your own property tester using the org.eclipse.core.expressions.propertyTester extension point.

 <extension
         point="org.eclipse.core.expressions.propertyTesters">
      <propertyTester
            class="tested.TextSelectedPropertyTester"
            id="tested.propertyTester1"
            namespace="tested"
            properties="textSelected"
            type="org.eclipse.jface.text.ITextSelection">
      </propertyTester>
</extension>

use like this:

<visibleWhen>
    <with variable="selection">
        <adapt type="org.eclipse.jface.text.ITextSelection">
            <test
                property="tested.textSelected"
                forcePluginActivation="true">
            </test>
       </adapt>
    </with>
</visibleWhen>

The property tester is very simple:

public class TextSelectedPropertyTester extends PropertyTester
{
  @Override
  public boolean test(Object receiver, String property, Object [] args, Object expectedValue)
  {
    if (receiver instanceof ITextSelection textSel) {
      return textSel.getLength() > 0;
    }

    return false;
  }
}

Note as shown the if statement uses a Java 16 instanceof type pattern, if you need to run with an older Java use:

    if (receiver instanceof ITextSelection) {
      return ((ITextSelection)receiver).getLength() > 0;
    }

  • Related