The JFace
Action
class is used in pop-up menus using the Eclipse 2.x model, but in Eclipse 3.x and Eclipse 4.x, the separation between handlers (code that does the work) and commands (logical operations such as copy that may be processed by different handlers) allows for a more flexible system, as described in Chapter 4, Interacting with the User. The first step is to replace the actions with handlers.
SampleView
class and go to the makeActions
method, which creates anonymous inner subclasses of Action
with an associated run
method. To convert these, they will need to be static
inner classes, so highlight the first new Action
expression and navigate to the Refactor | Convert Anonymous Class to Nested… menu.HandlerOne
:HandlerTwo
and DoubleClick
.static
, change the showMessage
method to public
and static
and replace the first parameter with null
:public static void showMessage(String message) { MessageDialog.openInformation( // viewer.getControl().getShell(), null, "Sample View", message); }
DoubleClick
handler will also need to comment out references to the non-static viewer
; the selection can be passed in as an argument instead, as follows:public static final class DoubleClick { public void run( @Named("selection") IStructuredSelection selection) { SampleView.showMessage("Double-click detected on " + selection.getFirstElement()); } }
hookDoubleClickAction
method will need to pass the current selection to the DoubleClick
class, which can be accessed from the DoubleClickEvent
parameter. Modify it as follows:private void hookDoubleClickAction() { viewer.addDoubleClickListener(new IDoubleClickListener() { public void doubleClick(DoubleClickEvent event) { new DoubleClick().run( (IStructuredSelection) event.getSelection()); } }); }
HandlerOne
, HandlerTwo
, and DoubleClick
classes to their own files by using the Refactor | Move Type to New File… menu. If the showMessage
method has not been updated, the refactoring will suggest changing it to public
.Action
superclass and annotate the run method with the @Execute
annotation from the org.eclipse.e4.core.di.annotations
bundle. To do this, open the META-INF/MANIFEST.MF
file, go to the Dependencies tab, and add the org.eclipse.e4.core.di.annotations
bundle. Then the run
method of the HandlerOne
, HandlerTwo
, and DoubleClick
classes should be annotated with @Execute
:public class HandlerOne { @Execute public void run() { showMessage("Action 1 executed"); } }
contributeToActionBars
, fillContextMenu
, fillLocalPullDown
, fillLocalToolBar
, hookContextMenu
, and makeActions
methods and the action1
, action2
, and doubleClickAction
fields.Since Eclipse 4.x handles the creation of menus and wires elements to handlers, the custom code that builds up menus isn't required. By recursively removing references to Action
, the handler code can be reduced to a minimum.
Of course if the application is run now, there will be no menus or interactivity. These will be added in the next step.
13.58.77.98