Difference between revisions of "FAQ How do I make key bindings work in an RCP application?"

From Eclipsepedia

Jump to: navigation, search
 
(See Also:)
 
(2 intermediate revisions by one user not shown)
Line 1: Line 1:
When actions are contributed via the <tt>actionSets</tt> extension
+
When actions are contributed via the <tt>actionSets</tt> extension point, key bindings are configured by associating the action with a declarative command. In this case, no code is required to hook the action to the key binding. However, if you programmatically create actions in an RCP application, you have to register actions yourself.  This requires two steps.  First, you need to specify the command ID for your action.  If you are using built-in actions from an action factory, they usually have the command ID already set.  If you create your own action, as a subclass of <tt>Action</tt>, you need to set the command ID yourself by calling the <tt>setActionDefinitionId</tt> method inherited from <tt>Action</tt>. Typically this is done from your action&#146;s constructor.
point, key bindings are configured by associating the action with a  
+
declarative command. In this case,
+
no code is required to hook the action to the key binding.
+
However, if you programmatically create actions in an RCP application,  
+
you have to register actions yourself.  This requires two steps.  First,
+
you need to specify the command ID for your action.  If you are using
+
built-in actions from an action factory, they usually have the command ID
+
already set.  If you create your own action, as a subclass of <tt>Action</tt>,
+
you need to set the command ID yourself by
+
calling the <tt>setActionDefinitionId</tt> method inherited from <tt>Action</tt>.
+
Typically this is done from your action&#146;s constructor.
+
  
 
+
Now that your action is linked to a command, you need to register the action with the platform.  You should do this the first time the platform calls your implementation of <tt>WorkbenchAdvisor.fillActionBars</tt>:
Now that your action is linked to a command, you need to register the
+
action with the platform.  You should do this the first time the platform
+
calls your implementation of
+
<tt>WorkbenchAdvisor.fillActionBars</tt>:
+
 
<pre>
 
<pre>
 
   public void fillActionBars(IWorkbenchWindow window,
 
   public void fillActionBars(IWorkbenchWindow window,
Line 28: Line 13:
 
   }
 
   }
 
</pre>
 
</pre>
The method <tt>registerGlobalAction</tt> will let the platform know
+
The method <tt>registerGlobalAction</tt> will let the platform know that your action exists.  When the key binding is invoked by the user, it will now be able locate and run your action.
that your action exists.  When the key binding is invoked by the user,
+
it will now be able locate and run your action.
+
 
+
  
 
== See Also: ==
 
== See Also: ==
 +
*[[FAQ What is the difference between a command and an action?]]
 +
*[[FAQ How do I associate an action with a command?]]
 +
*[[FAQ How do I provide a keyboard shortcut for my action?]]
 +
*[[FAQ What is the difference between a command and an action?]]
  
[[FAQ_What_is_the_difference_between_a_command_and_an_action%3F]]
+
{{Template:FAQ_Tagline}}
 
+
[[FAQ_How_do_I_associate_an_action_with_a_command%3F]]
+
 
+
[[FAQ_How_do_I_provide_a_keyboard_shortcut_for_my_action%3F]]
+
 
+
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>
+

Latest revision as of 12:26, 16 April 2008

When actions are contributed via the actionSets extension point, key bindings are configured by associating the action with a declarative command. In this case, no code is required to hook the action to the key binding. However, if you programmatically create actions in an RCP application, you have to register actions yourself. This requires two steps. First, you need to specify the command ID for your action. If you are using built-in actions from an action factory, they usually have the command ID already set. If you create your own action, as a subclass of Action, you need to set the command ID yourself by calling the setActionDefinitionId method inherited from Action. Typically this is done from your action’s constructor.

Now that your action is linked to a command, you need to register the action with the platform. You should do this the first time the platform calls your implementation of WorkbenchAdvisor.fillActionBars:

   public void fillActionBars(IWorkbenchWindow window,
      IActionBarConfigurer configurer, int flags) {
      ...
      if (maximizeAction == null) {
         maximizeAction = ActionFactory.MAXIMIZE.create(window);
         configurer.registerGlobalAction(maximizeAction);
      }
      menu.add(maximizeAction);
   }

The method registerGlobalAction will let the platform know that your action exists. When the key binding is invoked by the user, it will now be able locate and run your action.

[edit] See Also:


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.