Time for action – creating toolbars

The first step is to add the toolbar icons, which are the informational icons in the top right. In the original implementation, these were direct Action elements inserted into a ToolBar, which is part of the Eclipse 3.x interface. To achieve this with Eclipse 4.x, models using MDirectToolItem will be created and added to an MToolBar.

  1. Create a private method createToolBar in the SampleView class. Add a call to the bottom of the createPartControl method to ensure that it is called at view creation time.
  2. Inside the createToolBar method, create an instance of an MDirectToolItem object using the MMenuFactory.INSTANCE.createDirectToolItem() expression. It may be necessary to follow the quick-fix to add org.eclipse.e4.ui.model.workbench to allow the automatic import to find the class. Assign it to a local variable one.
  3. Add a tooltip Action 1 tooltip by calling one.setTooltip().
  4. To show the informational icon, a platform URI must be created. This uses the fully qualified plug-in name and the path within that bundle. In Eclipse 3.x, this was handled automatically by the ISharedImages reference and associated ImageDescriptor; in Eclipse 4.x, a URI must be used instead. Add a method called one.setIconURI with the URI platform:/plugin/org.eclipse.ui/icons/full/obj16/info_tsk.png as the argument.

    Note

    Normally the icon would be stored locally in the current bundle rather than cross-referencing an icon from a different environment. Relying on a specific path in a different bundle is fragile; for example, this icon used to be called info_tsk.gif in older versions of Eclipse.

  5. The direct handled item needs to be associated with a bundleclass URI, which has the plug-in name and class name of the handler itself. This will then be called when the user clicks on the toolbar item. Add a call to one.setContributionURI with the URI bundleclass://com.packtpub.e4.migration/com.packtpub.e4.migration.views.HandlerOne as the argument.

    Tip

    Verify that the call to set the handler is setContributionURI, as opposed to setContributorURI. If the wrong method is used, the action may display but won't have any effect when clicked.

  6. The createToolBar method should look like:
    private void createToolBar() {
      MDirectToolItem one = MMenuFactory.INSTANCE.createDirectToolItem();
      one.setTooltip("Action 1 tooltip");
      one.setIconURI("platform:/plugin/org.eclipse.ui/"+ "icons/full/obj16/info_tsk.png");
      one.setContributionURI("bundleclass://com.packtpub.e4.migration/"
       + "com.packtpub.e4.migration.views.HandlerOne");
    }
  7. The next step is to create an MToolBar to hold the newly created items. This uses the MMenuFactory as before, this time calling createToolBar(). Add in the tool item by calling add on the children of the MToolBar. It will look like:
    MToolBar toolBar = MMenuFactory.INSTANCE.createToolBar();
    List<MToolBarElement> children = toolBar.getChildren();
    children.add(one);
  8. Next the toolbar needs to be added to the part. This requires obtaining the part itself, which is represented as an MPart in the Eclipse 4.x model. Since this won't be instantiated by the code, it needs to be injected into the instance. Add a new injected field of type MPart called part:
    @Inject
    private MPart part;
  9. Finally, at the end of the createToolBar method, set the toolBar on the part as follows:
    part.setToolbar(toolBar);
  10. Run the Eclipse application and navigate to the Window | Show View | Sample Category | Sample View menu. An informational icon i should be shown on the right-hand side, which when clicked on brings up the Action 1 executed dialog:
    Time for action – creating toolbars
  11. Repeat the same steps to add Action 2 with HandlerTwo to the view.

What just happened?

A toolbar was programmatically created to add the action replicating the original Eclipse 3.x view. Although instances of model parts can be created by factories such as MMenuFactory, acquiring a reference to the current part requires injection of the MPart with an @Inject annotation. It's also possible to get injections of other model elements, such as MWindow and MApplication.

With a reference to the current part, the MToolBar can be set using the setToolbar method.

Note

Note that the method is capitalized setToolbar although the class is capitalized MToolBar.

It's possible to set a number of different fields in the direct handled item, such as whether it is enabled or visible. Changing this when the action is shown will result in it interactively updating in the user interface.

Finally, although the user interface can be created programmatically like this, it's almost certainly easier to build these using the fragment model editor. This approach will be shown later in this chapter.

Have a go hero – optimising the handler lookup

The contribution URI string can be a fragile reference to the handler. It's better to have a compile-time reference to a class instead. This can be achieved by acquiring the class, then using FrameworkUtil from the org.osgi.framework package to look up the containing bundle, and then use the symbolic name to create the bundleclass URI.

Add the following method and replace the constant argument to setContributionURI with a call to getURI:

private String getURI(Class<?> clazz) {
  Bundle bundle = FrameworkUtil.getBundle(clazz);
  if (bundle != null) {
    return "bundleclass://" + bundle.getSymbolicName() + "/" + clazz.getName();
  } else {
    return null;
  }
}
...
one.setContributionURI(getURI(HandlerOne.class));

This will allow the class to still be used if it is renamed or moved to a different package or bundle.

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

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