Search code examples
javarcpe4

RCP 4 Toggle a button in the toolbar


I am trying to do the following:

  1. Create a button in the toolbar (Already done as a 'handled tool item')
  2. Click on the button and have the button look like it's pressed in (I read something about using IAction.AS_CHECK_BOX, but I can't find any clear RCP 4 examples on how to do this). When the button is pressed in a certain action can be performed (For the sake of this example let's call it "Action A")
  3. Click on the button again and have the button look like it's no longer pressed in. When the button is no longer pressed in a different action can be performed ("For the sake of this example let's call it "Action B")

A more concrete example would be a text editor program. Let's say the toolbar has a 'Bold' button. The user presses the 'Bold' button and the button icon now looks like it has been pressed in. At this point, everything the user types into the text area will be in bold. The user then presses the 'Bold' button again and the button no longer looks like it is pressed in. At this point, everything the user types into the text area is in regular font.

I have tried searching around but cannot find any examples that clearly show how to do it. Any help would be appreciated!


Solution

  • When you add the 'Handler Tool Item' to the Toolbar in the Application.e4xmi you can specify the 'Type' as 'Check' to get the pressed / not pressed behavior.

    In your Handler for the item you can inject the 'MToolItem' so that you can test the checked state:

    @Execute
    public void execute(final MToolItem item)
    {
      if (item.isSelected())
        ... button pressed in
      else
        ... button not pressed
    }