Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Menu Contributions/Update checked state"

 
(Update the toolbar item checked state)
 
Line 1: Line 1:
 
= Update the toolbar item checked state =
 
= Update the toolbar item checked state =
  
The active handler can update any UI elements registered against the its command. It does this by requesting the ICommandService to refresh any registered UIElements.
+
The active handler can update any UI elements registered against the its command. It does this by requesting the <tt>ICommandService</tt> to refresh any registered <tt>UIElement</tt>s.
  
As a handler becomes active and implement IElementUpdater like org.eclipse.ui.tests.menus.ToggleContextHandler, the command service calls <code>public void updateElement(UIElement element, Map parameters)</code> for every UIElement registered against the command.
+
As a handler becomes active and implement <tt>IElementUpdater</tt> like <tt>org.eclipse.ui.tests.menus.ToggleContextHandler</tt>, the command service calls <code>public void updateElement(UIElement element, Map parameters)</code> for every <tt>UIElement</tt> registered against the command.
  
public class ToggleContextHandler extends AbstractHandler implements
+
<source lang="java">
IElementUpdater {
+
public class ToggleContextHandler extends AbstractHandler implements
private static final String TOGGLE_ID = "toggleContext.contextId";
+
    IElementUpdater {
// ...
+
  private static final String TOGGLE_ID = "toggleContext.contextId";
public void updateElement(UIElement element, Map parameters) {
+
  // ...
// the checked state depends on if we have an activation for that
+
  public void updateElement(UIElement element, Map parameters) {
// context ID or not
+
    // the checked state depends on if we have an activation for that
String contextId = (String) parameters.get(TOGGLE_ID);
+
    // context ID or not
element.setChecked(contextActivations.get(contextId) != null);
+
    String contextId = (String) parameters.get(TOGGLE_ID);
}
+
    element.setChecked(contextActivations.get(contextId) != null);
}
+
  }
 +
}
 +
</source>

Latest revision as of 18:29, 7 November 2011

Update the toolbar item checked state

The active handler can update any UI elements registered against the its command. It does this by requesting the ICommandService to refresh any registered UIElements.

As a handler becomes active and implement IElementUpdater like org.eclipse.ui.tests.menus.ToggleContextHandler, the command service calls public void updateElement(UIElement element, Map parameters) for every UIElement registered against the command.

public class ToggleContextHandler extends AbstractHandler implements
    IElementUpdater {
  private static final String TOGGLE_ID = "toggleContext.contextId";
  // ...
  public void updateElement(UIElement element, Map parameters) {
    // the checked state depends on if we have an activation for that
    // context ID or not
    String contextId = (String) parameters.get(TOGGLE_ID);
    element.setChecked(contextActivations.get(contextId) != null);
  }
}

Back to the top