Time for action – creating a direct menu and keybindings

Although using commands and handlers provides a generic way for reusing content, it is possible to provide a shorter route to implementing menus with a Direct MenuItem. The difference between this and a Handled MenuItem is that Direct just contains a reference to the @Executable class.

  1. To add a new direct menu item, open the Application.e4xmi file and navigate to the Application | Windows | Trimmed Window | Main Menu | Menu (File). Right-click on the menu and choose Add child | Direct MenuItem. In the dialog shown, fill in the details, including the class URI link to the HelloHandler, defined previously:
    • ID: com.packtpub.e4.application.directmenuitem.hello
    • Label: Direct Hello
    • Class URI: bundleclass://com.packtpub.e4.application/ com.packtpub.e4.application.handlers.HelloHandler
    Time for action – creating a direct menu and keybindings
  2. Run the application, choose File | Direct Hello to show the same message as before.
  3. Keys can be bound to commands in an application, and can be enabled in one of the several UI contexts. By default, these include In Dialogs and Windows, In Dialogs, and In Windows; additional contexts can be created. To set a keybinding to the menu, open the Application.e4xmi and navigate to the Application | BindingTables | BindingTable – In Dialog and Windows. Right-click on the binding table and Add child | KeyBinding. Fill in the fields as follows:
    • ID: leave blank
    • Sequence: M1+L
    • Command: helloCommand - com.packtpub.e4.application.command.hello
    Time for action – creating a direct menu and keybindings
  4. Run the application, and press M1 + L (Cmd + L on OS X, Alt + L on Windows/Linux). The Hello command will be run.

What just happened?

A Direct MenuItem is a way of hooking up a menu item directly to an executable method in a very simple way, without needing to have a separate command and handler defined. For application-wide operations, such as quitting the application, using a Direct MenuItem may be appropriate.

However, if the command needs to be handled in a different context then it is more appropriate to define a handler which can be replaced in different scopes.

Unlike a Handled MenuItem, the Direct MenuItem cannot have command parameters associated with it. Nor can a Direct MenuItem have a keybinding assigned.

To associate a keybinding with a command, an associated context must be selected. This is typically the In Dialogs and Windows, although other contexts can be selected as well (such as In Dialogs and In Windows). These are represented as the Binding Table in the Application.e4xmi node.

The sequence can be a single character, or it can be a sequence of characters. The meta characters (M1, M2, M3, M4, and so on) are defined in the org.eclipse.ui.bindings extension point.

  • M1 is the Cmd key on OS X and Ctrl on Windows
  • M2 is Shift on all platforms
  • M3 is Alt on all platforms
  • M4 is Ctrl on OS X

When the binding is invoked, it will execute the command specified in the list. As a command, it can have associated parameters like the Handled MenuItems.

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

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