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
.
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.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
.Action 1 tooltip
by calling one.setTooltip()
.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.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.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"); }
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);
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;
createToolBar
method, set the toolBar
on the part
as follows:part.setToolbar(toolBar);
Action 2
with HandlerTwo
to the view.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.
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.
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.
18.216.94.152