Skip Navigation

Controlling the Visibility and Activation of Menu Contributions

This procedure uses Sabre Red 360 declared variables and definitions to build the conditional menu functionality that is in the com.sabre.redapp.example.conditional.activation sample plug-in. The procedure is divided into the following parts:

Before you begin, you need the sample editor that is included in the com.sabre.redapp.example.editor.basic sample plug-in. If you do not use this basic sample editor plug-in, the menu entries will be hidden.

After you complete this procedure, import the basic editor sample as a separate plug-in. You must launch the conditional activation plug-in that you create in this procedure and the basic editor plug-in together to run and test the conditional activation within Sabre Red 360.

This procedure focuses on how to add conditions to menu entries, therefore, it is assumed that the following are already added to the conditional activation plug-in. The steps are not included.

  • The Sample Editor Pop-up Dialog and Sample Emulator Pop-up Dialog menu entries on the Tools menu

  • A command for the pop-up dialogs on menuContribution

  • The handlers for each command

Displaying and Hiding a Menu Entry Based on the Status of a Component

The following steps show how to make a menu entry visible on the Tools menu only when the active component is an editor named Sample Editor. If Sample Editor is not active, the menu entries are hidden.

  1. Open a plug-in project to which you want to add conditional activation.

  2. For the org.eclipse.ui.menus extension point, right-click the command element. The command element is named Sample Editor Pop-up Dialog. Choose visibleWhen from the menu.

image067

 

  1. For the checkEnabled property of visibleWhen, choose false. A value of false lets you define conditions for menuContribution.

image069

 

  1. Right-click visibleWhen, and then choose New > with from the menu.

image071

 

  1. In the variable property for with, type edgeActiveEditorId. The variable property stores the identifier of the active editor. This is a Sabre Red 360 declared definition.

image073

 

  1. Right-click the with element, and then choose New > equals from the menu. (This is shown in the preceding figure.)

image075

 

  1. In the value field, type the editor id. The value property must match the identifier of the editor that you want to test. You can obtain the editor id in the id property of org.eclipse.ui.editors in your editor.

        For the sample editor and sample conditional activation plug-ins, the value property is com.sabre.redapp.example.editor.basic.SampleEditor .

When you launch Sabre Red 360 with both the sample basic editor plug-in and the conditional activation plug-in that you are building in this procedure, the behavior of your plug-in should be similar to the next figure.

image077

When the Sabre emulator is active, the Sample Editor Pop-up Dialog menu entry is hidden.

When the Sample Editor is active, the Sample Editor Pop-up Dialog menu entry is visible and enabled.

Disabling Menu Entries with a Handler Policy

The following steps show how to disable the Sabre Emulator Pop-up Dialog menu entry if the active editor is not Sabre emulator. These steps add a reference to the Sabre Red 360 definition. (For more information, see Use of Declared Variables and Definitions to Build Definitions.)

  1. Right-click the handler, and then choose New > activeWhen.

image079

 

  1. Select activeWhen, and then choose New > reference from the menu.

image081

 

  1. In the definitionId property, type com.sabre.edge.command.definitions.editor.

        This is a Sabre Red 360 declared definition that checks if Sabre emulator is the active editor.

When you launch Sabre Red 360 with both the sample basic editor plug-in and the conditional activation plug-in that you are building in this procedure, the behavior of your plug-in should be similar to the next figure.

image083

The Sample Emulator Pop-up Dialog menu is disabled when the active editor is Sample Editor, and this menu entry is enabled when the active editor is Sabre Emulator.

Adding References to Definitions

The following steps extract the expression that checks the active editor to a definition and adds a reference to the definition from menuContribution.

  1. On the Extensions tab, add the org.eclipse.core.expressions.definitions extension point.

  1. In the id property, type a unique identifier for your definition.

        Common practice is to use the plug-in name followed by a unique identifier. The identifier must be unique across Sabre Red 360.

        In this example, the value is com.sabre.redapp.example.conditional.activation.editor.

Next, you will build the expression.

image085
  1. Add the with element to the definition element.

  2. In the variable property, type edgeActiveEditorId. This is a Sabre Red 360 declared definition that stores the identifier of the active editor.

  3. Add the equals element as a child of with. In the value property, type the identifier of the editor that you want to be active in order to trigger the definition.

        The identifier of the Sample Editor in the com.sabre.redapp.example.editor.basic sample plug-in is com.sabre.redapp.example.editor.basic.SampleEditor .

        The condition is fulfilled when the variable edgeActiveEditorId has the value com.sabre.redapp.example.editor.basic.SampleEditor.

        Eclipse should generate the following code in plugin.xml.

<extension point="org.eclipse.core.expressions.definitions">
  <definition id="com.sabre.redapp.example.conditional.activation.editor">
        <with variable="edgeActiveEditorId">
          <equals value="com.sabre.redapp.example.editor.basic.SampleEditor"/>
        </with>
  </definition>
</extension>

 

  1. To your menuContribution element, add the visibleWhen statement. For the checkEnabled property, choose false.

image087
  1. To the visibleWhen statement that you added in the preceding step, add the reference element.

image089
  1. In the definitionId property, type the identifier of the definition.

        In the sample that this procedure builds, this value is com.sabre.redapp.example.conditional.activation.editor.

You are done building the conditional activation in the sample plug-in.