Skip to main content
Jump to: navigation, search

FAQ How can I change the name or tooltip of my action?

Revision as of 16:27, 14 March 2006 by Claffra (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Actions contributed via XML have statically defined names, images, and tooltips. Often, you want to change these attributes dynamically, based on the current selection or some other state. Owing to the lazy-loading nature of the platform, you can’t do this until your action has been run once. A common solution is to specify generic attributes in the XML and then make attributes more dynamic after the action has been run. For example, the action to enable or disable breakpoints is called Toggle Breakpoint when the platform is first started. After it has been run once, the action dynamically sets its name to be either Enable Breakpoint or Disable Breakpoint, depending on whether the selected breakpoint is enabled. Here is an example action that implements this behavior:

   class ToggleAction 
         implements IWorkbenchWindowActionDelegate {
      private boolean state = false;
      public void run(IAction action) {
         state = !state;
         String name = state ? "True Action" : "False Action";

You can change many more action properties, including the action’s image, accelerator key, and enablement. Look at the methods on IAction to see what other properties can be changed.

It is also common to update action properties when the selection changes. Although the preceding example uses a workbench window action delegate, which is not notified of selection changes by default, the action can register itself as a selection listener in the init method and then update its state during the selectionChanged callback.

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.

Back to the top