Search code examples
javaswteclipse-rap

ToolTip on disabled ToolItem - RAP vs RCP


Is there a way to have a ToolItem in a ToolBar be disabled and still have it's tooltip?

I know it is not possible by default and workarounds can be used (wrapping within another widget), but because ToolItem requires a ToolBar parent I couldn't see a solution in this case. Does anyone see how you might "hack" the tooltip workaround in this case?

Don't know if relevant but this is the code:

class CustomItem extends org.eclipse.jface.action.ContributionItem {

 @Override
    public void fill(final ToolBar toolBar, final int index) {

      toolItem = new ToolItem(toolBar, SWT.PUSH);

      // . . .

    }
}

Update:

The project that this code is needed is a RAP application and somehow it slipped my mind to think there may be some differences compare with RCP regarding the issue I'm having.

The answer provided by Subash J is correct for RCP applications but will not work for RAP.

After talking with a RAP developer he pointed me to this reported bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=435750

I updated the title so it's more descriptive of this posts content.


Solution

  • Yes you can disable a ToolItem in a ToolBar and still show the tooltip. Try the below code.

    class CustomItem extends org.eclipse.jface.action.ContributionItem {
    
     @Override
        public void fill(final ToolBar toolBar, final int index) {
          final ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);
            toolItem.setText("Sample Tool Item 1");
            toolItem.setEnabled(false);
            toolItem.setToolTipText("Sample Tool Tip 1");
    
            final ToolItem toolItem2 = new ToolItem(toolBar, SWT.PUSH);
            toolItem2.setText("Sample Tool Item 2");
            toolItem2.setToolTipText("Sample Tool Tip 2");
    
        }
    }
    

    Output will be similar like below where Sample Tool Item 1 is disabled and at the same time tooltip is displayed.

    Output