Sometimes two different views need to behave differently for the same action item in the Menu Bar. For example: In Outlook Express when the calendar page is active and user triggers New action item from the menu then it will open new calendar entry form. And when the Address book is active and user triggers the same action item New through the menu then it will open the new contact entry form.

To implement this type of situation we need to use Retargetable action. Here one example is given to explain this approach.

Example:

Suppose we have two views: AddressView and CalenderView. And there is one action item New in the menu bar. When user selects this action item and AddressView is active, then new Contact entry form should be opened and when the CalenderView is opened then new Calender entry form should be opened. For that there are two action files OpenContactForm and OpenCalenderForm. So this New action item need to be associated with different action file depending on the current active view.

Steps need to be followed for implementing this.

  • First it needs to declare the retargetable action (New) in the plugin.xml. ActionSets can define retargetable actions. The action is visible only when the corresponding actionSet is visible. But the action will be enabled when a view or editor that implements that action is active.

Following is the code for defining retargetable action in plugin.xml.

 <extension point = "org.eclipse.ui.actionSets">

  <actionSet id="actionSetsID" label="New" visible="true">
    <action id="actionID" menubarPath="" toolbarPath="" 
      label ="Retargatable Action" retarget="true">
    </action>
  </actionSet>

</extension>
  • You can associate this retargetable action to the different action files in different views by using globalActionHandler.
  • In CalenderView this action need to be associated with action file OpenCalenderForm using the following code:
 getViewSite().getActionBars().setGlobalActionHandler(
    actionID, new OpenCalenderForm ());
  • In AddressView this action need to be associated with action file OpenContactForm using the following code:
 getViewSite().getActionBars().setGlobalActionHandler(
    actionID, new OpenContactForm());