Since the Action
class is deprecated, the supported mechanism is to create a command, a handler, and a menu to display the command in the menu bar.
Hello
command as follows:<extension point="org.eclipse.ui.commands"> <command name="Hello" description="Says Hello World" id="com.packtpub.e4.clock.ui.command.hello"/> </extension>
<extension point="org.eclipse.ui.handlers"> <handler class= "com.packtpub.e4.clock.ui.handlers.HelloHandler" commandId="com.packtpub.e4.clock.ui.command.hello"/> </extension>
IHandler
, typically AbstractHandler
. Create a class HelloHandler
in a new com.packtpub.e4.clock.ui.handlers
package, which implements AbstractHandler
(from the org.eclipse.core.commands
package).public class HelloHandler extends AbstractHandler { public Object execute(ExecutionEvent event) { MessageDialog.openInformation(null, "Hello", "World"); return null; } }
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 specified by its locationURI
, which is a URL that begins with menu:
such as menu:window?after=additions
or menu:file?after=additions
. To place it in the Help menu, add this to the plugin.xml
file.<extension point="org.eclipse.ui.menus"> <menuContribution allPopups="false" locationURI="menu:help?after=additions"> <command commandId="com.packtpub.e4.clock.ui.command.hello" label="Hello" style="push"> </command> </menuContribution> </extension>
Hello World
message. If the Hello menu is disabled, verify that the handler extension point is defined, which connects the command to the handler class.The main issue with the actions framework was that it tightly coupled the state of the command with the user interface. Although an action could be used uniformly between different menu locations, the Action
superclass still lives in the JFace package, which has dependencies on both SWT and other UI components. As a result, Action
cannot be used in a headless environment.
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 3.x uses a final class and then a retargetable IHandler
to perform the actual execution.
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 org.eclipse.ui.menus
extension point allows menuContributions
to be added anywhere in the user interface. To address where the menu can be contributed to, the locationURI
object defines where the menu item can be created. The syntax for the URI is as follows:
menu
: Menus begin with the menu:
protocol (can also be toolbar:
or popup:
)identifier
: This can be a known short name (such as file
, window
, and 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 pop-up menu's registerContextMenu()
call.?after(or before)=key
: This is the placement instruction to put this after or before other items; typically additions is used as an extensible location for others to contribute to.The locationURI
allows plug-ins to contribute to other menus, regardless of where they are ultimately located.
Note, that if the handler implements the IHandler
interface directly instead of subclassing AbstractHandler
, the isEnabled()
method will need to be overridden as otherwise the command won't be enabled, and the menu won't have any effect.