IAction and Widget mapping

This post explains how you can map a IAction as an model to a Button. In this case IAction will act as an model to your UI component Button by which you can control the behaviour of the button. In fact in the same way you can use IAction for MenuItem and ToolItem.

IAction belong to jface and button is and SWT component. Well  ActionContributionItem in the package org.eclipse.jface.action from the plugin org.eclipse.jface comes to rescue, the class provides API for the conversion.

Below are the step wise instructions needs to be followed to achieve it:

  1. Instantiate ActionContributionItem with IAction object
  2. Call the fill method on the ActionContributionItem object with appropriate parameter (in our case it should be instanceof Composite)
  3. That's all you are done now call a getWidget API to work on your SWT widget.

Benefits:

  1. Listeners are managed automatically so you need bother about them
  2. Look and feel is also been taken care of
Things to keep in mind:

  1. You need to set the layout data on the Widget 
  2. Icon and/or text should be set on the IAction as per your usability 

Comments

Popular posts from this blog

java.net.SocketException: Connection reset

Hide empty last column from Table/Tree Viewer

Spring: implements interface not working in @Controller