Time for action – changing contexts

The context is the location in which this binding is valid. For commands that are visible everywhere—typically the kind of options in the default menu—they can be associated with the org.eclipse.ui.contexts.window context. If the command should also be invoked from dialogs as well, then the org.eclipse.ui.context.dialogAndWindow context would be used instead.

  1. Open the plugin.xml file of the clock.ui project.
  2. To enable the command only for Java editors, go to the plugin.xml tab, and modify the contextId as follows:
    <extension point="org.eclipse.ui.bindings">
      <key commandId="com.packtpub.e4.clock.ui.command.hello"
           sequence="M1+9"
           //The following  commented line needs to be removed
           /*contextId="org.eclipse.ui.contexts.window"*/
           contextId="org.eclipse.jdt.ui.javaEditorScope"
           schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"/>
    </extension>
  3. Run the Eclipse instance, and create a Java project, a test Java class, and an empty text file.
  4. Open both of these in editors. When the focus is on the Java editor, the Cmd + 9 or Ctrl + 9 operation will run the command, but when the focus is on the text editor, the keybinding will have no effect.

Unfortunately, it also highlights the fact that just because the keybinding is disabled when in the Java scope, it doesn't disable the underlying command.

Note

If there is no change in behavior, try cleaning the workspace of the test instance at launch, by going to the Run | Run ... menu, and choosing Clear on the workspace. This is sometimes necessary when making changes to the plugin.xml file, as some extensions are cached and may lead to strange behavior.

What just happened?

Context scopes allow bindings to be valid for certain situations, such as when a Java editor is open. This allows the same keybinding to be used for different situations, such as a Format operation—which may have a different effect in a Java editor than an XML editor, for instance.

Since scopes are hierarchical, they can be specifically targeted for the contexts in which they may be used. The Java editor context is a subcontext of the general text editor, which in turn is a subcontext of the window context, which in turn is a subcontext of the windowAndDialog context.

The available contexts can be seen by editing the plugin.xml file in the plug-in editor; in the extensions tab the binding shows an editor window with a form:

What just happened?

Clicking on the Browse… button next to the contextId brings up a dialog, which presents the available contexts:

What just happened?

It's also possible to find out all the contexts programmatically or via the running OSGi instance, by navigating to Window | Show View | Console, and then using New Host OSGi Console in the drop-down menu, and then running the following code snippet:

osgi> pt -v org.eclipse.ui.contexts

Extension point: org.eclipse.ui.contexts [from org.eclipse.ui]

Extension(s):
-------------------
null [from org.eclipse.ant.ui]
   <context>
      name = Editing Ant Buildfiles
      description = Editing Ant Buildfiles Context
      parentId = org.eclipse.ui.textEditorScope
      id = org.eclipse.ant.ui.AntEditorScope
   </context>

null [from org.eclipse.compare]
   <context>
      name = Comparing in an Editor
      description = Comparing in an Editor
      parentId = org.eclipse.ui.contexts.window
      id = org.eclipse.compare.compareEditorScope
   </context>
..................Content has been hidden....................

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