Time for action – contributing commands to pop-up menus

It's useful to be able to add contributions to pop-up menus so that they can be used by different places. Fortunately this can be done fairly easily with the menuContribution fragment and a combination of enablement tests. However, to implement this in E4, the view must be moved into the fragment.e4xmi file in order to attach a PopupMenu.

  1. Add the org.eclipse.ui.services package as a dependency to the plugin.xml in the Dependencies tab, if it's not already added.
  2. Open the TimeZoneTableView class and add the following to the end of the createPartControl method:
    private void createPartControl(Composite parent, EMenuService menuService) {
      menuService.registerContextMenu(tableViewer.getControl(), "com.packtpub.e4.clock.ui.popup");
      tableViewer.addSelectionChangedListener(event -> {
        // forward selection
        Object selection = ((IStructuredSelection) event.getSelection()).getFirstElement();
        if (selection != null && selectionService != null) {
          selectionService.setSelection(selection);
        }
      });
  3. Running the Eclipse instance and showing the menu results in nothing being displayed, because no menu items have been added to it yet.
  4. Create a command and a handler fragment called Show the Time by adding them to the fragment.e4xmi file as before.
  5. Create a class called ShowTheTime in the com.packtpub.e4.clock.ui.handlers package to show the time in a specific time zone:
    public class ShowTheTime {
    @Execute
    public void execute(ESelectionService selectionService) {
      Object selection = selectionService.getSelection();
      if (selection instanceof ZoneId) {
        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
        String theTime = ZonedDateTime.now((ZoneId) selection).format(formatter);
        MessageDialog.openInformation(null, "The time is",
         theTime);
      }
    }
    Time for action – contributing commands to pop-up menus
  6. To add a popup menu, the Time Zone Table View first needs to be converted into an E4 PartDescriptor. Comment out the e4view in the plugin.xml, and in the fragment.e4xmi file, add a new Model Fragment with the following information:
    1. Extended Element ID: org.eclipse.e4.legacy.ide.application
    2. Feature Name: descriptors
  7. Select the PartDescriptor from the drop-down list and click on Add. Fill in the fields as follows:
    1. ID: com.packtpub.e4.clock.ui.partdescriptor.timeZoneTableView
    2. Label: Time Zone Table View
    3. Icon URI: platform:/plugin/com.packtpub.e4.clock.ui/icons/sample.gif
    4. Class URI: bundleclass://com.packtpub.e4.clock.ui/com.packtpub.e4.clock.ui.views.TimeZoneTableView
    5. Category: Timekeeping
  8. Under the Menus node, select Popup Menu from the drop-down list and click on Add. Give it the ID com.packtpub.e4.clock.ui.popup, which corresponds to the argument used in the menuService.registerContextMenu call previously.
  9. Under the Popup Menu node, select Handled Menu Item from the drop-down list and click on Add. Fill in the fields as follows:
    1. ID: com.packtpub.e4.clock.ui.handledmenuitem.showTheTime
    2. Label: Show the Time
    3. Command: Show the Time - com.packtpub.e4.clock.ui.command.show
    Time for action – contributing commands to pop-up menus
  10. Run the target Eclipse instance, and open the TimeZoneTableView. Right-click on a time zone in the table, and the command Show the Time will be displayed in a pop-up menu. Select the menu item and a dialog should show the time.

What just happened?

The E4 fragment can be used to add views, commands, and handlers, and wire them together. This approach to registering commands is powerful, because any time a time zone is exposed as a selection in the future will now have a Show the Time menu added to it automatically.

The commands define a generic operation, and handlers bind those commands to implementations. The context-sensitive menu is provided by the pop-up menu extension point using the LocationURI popup:org.eclipse.ui.popup.any. This allows the menu to be added to any pop-up menu that uses the EMenuService and when the selection contains a ZoneId. The EMenuService is responsible for listening to the mouse gestures to show a menu, and filling it with details when it is shown.

In the example, the command was enabled when the object was an instance of a ZoneId, and also if it could be adapted to a ZoneId. This would allow another object type (say, a contact card) to have an adapter to convert it to a ZoneId, and thus show the time in that contact's location. This could be used to provide compatibility with older Java runtimes, using a TimeZone for example.

Have a go hero – using view menus and toolbars

The way to add a view menu is similar to adding a pop-up menu; the location URI used is the view's ID rather than the menu item itself. Add a Show the Time menu to the Time Zone View as a view menu.

Another way of adding the menu is to add it as a toolbar, which is an icon in the main Eclipse window. Add the Show the Time icon by adding it to the global toolbar instead.

Pop quiz – understanding menus

Q1. How can a Command be connected to a menu?

Q2. What is the M1 key?

Q3. How are keystrokes bound to Command instances?

Q4. What is a menu locationURI?

Q5. How is a pop-up menu created?

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

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