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 Menu Item. The difference between this and a Handled Menu Item is that Direct Menu Item contains a direct reference to the @Executable class, instead of indirect through a handler.

  1. To add a new direct menu item, open the Application.e4xmi file and navigate to the Application | Windows and Dialogs | Trimmed Window | Main Menu | Menu (File). Right-click on the menu and choose Add child | Direct Menu Item. In the dialog shown, fill in the following details, including the class URI link to the HelloHandler, defined previously:
    1. ID: com.packtpub.e4.application.directmenuitem.hello
    2. Label: Direct Hello
    3. 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, and navigate to the File | Direct Hello menu, which shows the same message as previously.

    Note

    Keys can be bound to commands in an application, and can be enabled in one of several UI contexts. By default, these include In Dialogs and Windows, In Dialogs, and In Windows; additional contexts can also be created.

  3. To set a keybinding to the menu, open the Application.e4xmi and navigate to the Application | BindingTables | BindingTable – In Dialog and Windows node. Right-click on the binding table and choose Add child | KeyBinding. Fill in the fields as follows:
    1. ID: com.packtpub.e4.application.keybinding.m1l
    2. Sequence: M1+L
    3. 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 macOS, Ctrl + L on Windows/Linux). The Hello command will be run.

What just happened?

A Direct Menu Item 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 Menu Item may be appropriate.

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

Unlike a Handled Menu Item, the Direct Menu Item cannot have command parameters associated with it. Nor can a Direct Menu Item 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 context, although other contexts can be selected as well (such as In Dialogs and In Windows). These are stored in the Binding Table node in the Application.e4xmi file.

The sequence can be a single character, or it can be a sequence of characters. Combinations of characters are represented with a + joining them. 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 macOS and the Ctrl key on Windows
  • M2 is the Shift key on all platforms
  • M3 is the Alt key on all platforms
  • M4 is the Cmd key on macOS

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 Menu Items.

Tip

Use the M1 key definition instead of Ctrl for normal keyboard shortcuts, since this will provide the correct behavior on different operating systems.

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

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