Time for action – defining the pop-up view in the fragment

The pop-up menu can be created in an almost identical way to the view menu. As before, it needs to be registered with the menu service but the code can be dramatically simplified by representing it in the fragment.e4xmi file.

  1. Go to the fragment.e4xmi file, and go to the Menus element beneath the Sample View part descriptor. Add a child Popup Menu to the Menus element, either with the context-sensitive menu and by choosing Add child | Popup Menu, or by selecting Popup Menu from the dropdown and clicking on the Add button.
  2. In the newly created Popup Menu, set the ID to com.packtpub.e4.migration.views.SampleView.popup.
  3. Select Handled Menu Item from the dropdown and click on Add.
  4. Add the Action 1 similar to before by setting these:
    1. Label: Action 1
    2. Tooltip: Action 1 tooltip
    3. Icon URI: platform:/plugin/org.eclipse.ui/icons/full/obj16/info_tsk.png or use the Find … button as before
    4. Ensure that the Enabled checkbox is selected
    5. Click on the Find … button next to the Command field and choose the Action 1 command from the list
  5. Repeat the same for the Action 2 command.
  6. Replace the createPopupMenu call from the createPartControl method with a call to register the pop-up menu with the viewer's control:
    // createPopupMenu(viewer.getControl());
    menuService.registerContextMenu(
     viewer.getControl(), part.getElementId() + ".popup");
  7. Run the application, and right-click on the elements in the tree. The pop-up menu should be seen as before.

What just happened?

The pop-up menu is created in the same way as the view menu, but with a different top-level type. Creating a pop-up menu is easier in the model fragment, since there are fewer steps in hooking it up. However, it is still necessary to ensure that the SWT control is registered with the listeners to show the pop-up menu.

Unlike Eclipse 3.x menus, which are typically bound by the instance, Eclipse 4.x menus are tied by the identifiers stored in the element's ID. The identifier used in the registerContextMenu must be the same as the ID of the pop-up menu in the fragment. This can either be a well-known hard-coded string, or it can be dynamically created from the part's ID. In this case, the part's ID was appended with .popup to disambiguate it from the part itself.

Have a go hero – invoking the handler from the hookDoubleClick action

Currently the hookDoubleClick action instantiates and runs the handler directly. This can be changed to look up the command dynamically and then execute that instead.

To look up a command, inject an instance of ICommandService and then use getCommand with the ID of the command (com.packtpub.e4.migration.command.double in this case). It can be invoked with command.executeWithChecks(new ExecutionEvent()).

Selection is not transferred between Eclipse 3.x and Eclipse 4.x services. To ensure that the ISelection is injected appropriately, inject the ESelectionService into the part. Either the selection can be set just prior to the command being executed, or a selection listener can be added to the viewer that forwards the selection on to the Eclipse 4.x selection service:

@Inject
ESelectionService selectionService;

viewer.addSelectionChangedListener(new ISelectionChangedListener() {
  @Override
  public void selectionChanged(SelectionChangedEvent event) {
    selectionService.setSelection(event.getSelection());
  }
});

Pop quiz

Q1. What is the Eclipse 4.x replacement for Action instances?

Q2. How is a selection obtained from a DoubleClickEvent?

Q3. What tag needs to be added to allow a view menu to be shown?

Q4. How are classes connected to a handler?

Q5. What is the difference between a platform: URI and a bundleclass: URI?

Q6. What is the difference between a part and a part descriptor?

Q7. How is a pop-up menu connected to the viewer?

Q8. How can a selection be forwarded from an Eclipse 3.x view to an Eclipse 4.x part?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.189.7