Commands and handlers are common to both Eclipse 3.x and the Eclipse 4 model. In Eclipse 3.x, they are represented as extension points in the plugin.xml
file under org.eclipse.ui.commands
. In Eclipse 4, they are stored in the E4 fragment file.
A command will be created to represent saying "hello world," and a handler will be created to display the message. These will then be used to add a menu item to execute the operation.
fragment.e4xmi
file.org.eclipse.e4.legacy.ide.application
(this can be found under the Container-Type: Application from the search field)commands
com.packtpub.e4.clock.ui.command.hello
Hello
Says Hello World
org.eclipse.e4.legacy.ide.application
, but this time with the Feature Name handlers
.com.packtpub.e4.clock.ui.handler.HelloHandler
Hello – com.packtpub.e4.clock.ui.command.hello
bundleclass://com.packtpub.e4.clock.ui/com.packtub.e4.clock.ui.handlers.HleloHandler
com.packtpub.e4.clock.ui/src
com.packtpub.e4.clock.ui.handlers
HelloHandler
execute
IHandler
(typically AbstractHandler
); but in Eclipse 4, this interface is no longer necessary. Implement the class HelloHandler
as follows:package com.packtpub.e4.clock.ui.handlers; import org.eclipse.e4.core.di.annotations.Execute; public class HelloHandler { public void execute() { MessageDialog.openInformation(null, "Hello", "World"); } }
com.packtpub.e4.clock.ui.command.hello
is used to refer to it from menus or other locations. To place the contribution in an existing menu structure, it needs to be added as a menuContribution
. Create a new Model Fragment using the Extended Element ID org.eclipse.e4.legacy.ide.application
and Feature Name handlers
.com.packtpub.e4.clock.ui.handledmenuitem.Hello
Hello
Hello – com.packtpub.e4.clock.ui.command.hello
Eclipse 3.x introduced the concept of commands and handlers as a means of separating their interface from their implementation. This allows a generic command (such as Copy) to be overridden by specific views. Unlike the traditional command design pattern, which provides implementation as subclasses, the Command
in Eclipse uses a retargetable handler to perform the actual execution. In Eclipse 4, the concepts of commands and handlers are used extensively to provide the components of the user interface. The key difference is in their definition; for Eclipse 3.x, this typically occurs in the plugin.xml
, whereas in Eclipse 4, it is part of the application model.
In the example, a specific handler was defined for the command, which is valid in all contexts. The handler's class is the implementation; the command ID is the reference.
The menuContributions
fragment entry allows menus to be added anywhere in the user interface. The parent defines where the menu item can be created. The syntax for the parent is as follows:
identifier
: This can be a known short name (such as file
, window
, or help
), the global menu (org.eclipse.ui.main.menu
), the global toolbar (org.eclipse.ui.main.toolbar
), a view identifier (org.eclipse.ui.views.ContentOutline
), or an ID explicitly defined in a popup menu's registerContextMenu
call?after|before=key
: This is a placement instruction to put this after or before other items; typically additions
is used as an extensible location for others to contribute toThis allows plug-ins to contribute to other menus, regardless of where they are ultimately located.
52.14.130.13