6.6. Key Bindings
Both workbench actions and editor actions can have accelerator keys associated with them (see Section 7.3.5, Keyboard actions, on page 288 for how to programmatically associate accelerator keys). Originally, the accelerator was specified as part of the action declaration, but that approach did not prevent multiple actions from declaring the same accelerator and did not allow the user to change key bindings. The new approach involves associating actions and key bindings with commands and grouping those commands into categories (see Figure 6-11). In addition, each command has a scope indicating when it will be available to the user, and it is part of a configuration so that the user can select between different configurations to get different key bindings (e.g., Emacs key bindings vs. Eclipse key bindings).
Figure 6-11. Key binding declaration.
Categories are used to group commands for presentation to the user. Categories appear in the General > Keys preference page as the first column in the table on the View tab and the Command / Category drop-down list on the Modify tab. To add a key binding for the Add to Favorites action, first you need to define a category. Create a new org.eclipse.ui.commands extension (see Section 6.2.1, Defining a workbench window menu, on page 209 for an example of how extensions are added), then right-click and select New > category. Enter the following attributes for the new category:
Commands are contained in categories and have key bindings associated with them. Actions then reference commands to associate key bindings. To add a command for the Add to Favorites action, right-click on the org.eclipse.ui.commands extension and select New > command. Enter the following attributes for the new command.
6.6.3. Key bindings
Commands do not reference key bindings; rather, key bindings are declared separately and reference commands. This allows for multiple key bindings to be associated with the same command. For example, the default accelerator for saving the contents of a file is Ctrl+S, but after switching to the Emacs configuration, the save accelerator becomes Ctrl+X Ctrl+S.
To add a key binding for the Add to Favorites action, create a new org.eclipse.ui.bindings extension (see Section 6.2.1, Defining a workbench window menu, on page 209 for an example of how extensions are added), then right-click and select New > key. Enter the following attributes for the new key binding. After completion, the Favorites key binding appears in the Keys preference page (see Figure 6-12).
Figure 6-12. Keys preference page showing Favorites key binding.
Other key binding attributes that are not used in the Favorites example include:
The final step in defining an accelerator for the Favorites example involves modifying the editor action defined in Section 6.5.5, Defining an editor top-level action, on page 248 to reference the new command created in Section 6.6.2, Commands, on page 252.
Select the editor action in the plug-in manifest and modify the definitionId attribute to have the value "com.qualityeclipse.favorites.commands.addToFavoritesView" so that the action now references the new command and associated key binding.
The keyboard can be used to select menu items in the workbench window. For example, if you press and release the Alt key and then press and release F (or press Alt+F), you will see the workbench window File menu drop down. If you look closely, you will see an underscore under at most one letter in each menu label and menu item label. When you are in this menu selection mode, pressing the letter with the underscore will activate that menu or menu command. Under some platforms, such as Windows XP, these underscores are not visible unless you activate menu selection mode.
In your plug-in manifest, you can specify which character in a menu's or menu item's label should have an underscore by preceding that character with the "&" character. For example, in the following declaration, the "&" before the letter "r" in the word "Favorites" causes that letter to have an underscore when you activate the menu selection mode (see Figure 6-13).
Figure 6-13. Plug-in manifest editor showing & for keyboard accessibility.
<action class="com.qualityeclipse.....OpenFavoritesViewActionDelegate" icon="icons/sample.gif" id="com.qualityeclipse.favorites.openFavoritesView" label="Open Favo&rites View" menubarPath="com.qualityeclipse.favorites.workbenchMenu/content" style="push" toolbarPath="Normal/additions" tooltip="Open the Favorites view in the current workbench page"/>
If you use this same approach with the Favorites menu declaration (see Section 6.2.1, Defining a workbench window menu, on page 209), you can use a sequence of keystrokes to open the Favorites view without touching the mouse.
Starting with this chapter, we will list IBM's relevant RFRS certification requirements and briefly discuss what is required to pass each test. We will also endeavor to make sure that the ongoing Favorites example complies with any relevant requirements. The rule definitions themselves are quoted with permission from IBM's official Ready for IBM Rational Software Integration Requirements document. To obtain more information about the RFRS program, see Appendix B, Ready for Rational Software, or visit the IBM Web site at www.developer.ibm.com/isv/rational/readyfor.html.