Jump to: navigation, search

Difference between revisions of "Menu Contributions/Radio Button Command"

(New page: == Command Definition == a == Handler == a == Menu Contribution == a)
 
Line 1: Line 1:
 +
You can create a command with a required parameter.  The parameter will be passed during every execution.
 +
 
== Command Definition ==
 
== Command Definition ==
  
a
+
You want to create a command that will be executed with a paramter.  The parameter in this example matches which of the radio buttons is selected.
 +
 
 +
<command
 +
      categoryId="org.eclipse.ui.category.help"
 +
      defaultHandler="z.ex.dropdown.internal.RadioHandler"
 +
      id="z.ex.dropdown.radio"
 +
      name="Radio Example">
 +
    <commandParameter
 +
          id="z.ex.dropdown.radio.info"
 +
          name="Radio Name"
 +
          optional="false">
 +
    </commandParameter>
 +
</command>
 +
 
  
 
== Handler ==
 
== Handler ==
  
a
+
The handler will receive the parameter.  It can then update its model (in my example my model is a local variable, but that might not be appropriate in command that can have multiple handlers).
 +
 
 +
package com.example.handlers.internal;
 +
 +
import java.util.Map;
 +
import org.eclipse.core.commands.AbstractHandler;
 +
import org.eclipse.core.commands.ExecutionEvent;
 +
import org.eclipse.core.commands.ExecutionException;
 +
import org.eclipse.ui.commands.ICommandService;
 +
import org.eclipse.ui.commands.IElementUpdater;
 +
import org.eclipse.ui.handlers.HandlerUtil;
 +
import org.eclipse.ui.menus.UIElement;
 +
 +
public class RadioHandler extends AbstractHandler implements IElementUpdater {
 +
 +
  private static final String PARM_INFO = "z.ex.dropdown.radio.info";
 +
  private String fCurrentValue;
 +
 +
  public Object execute(ExecutionEvent event) throws ExecutionException {
 +
    String parm = event.getParameter(PARM_INFO);
 +
    if (parm.equals(fCurrentValue)) {
 +
      return null; // in theory, we're already in the correct state
 +
    }
 +
   
 +
    // do whatever having "parm" active implies
 +
    fCurrentValue = parm;
 +
   
 +
   
 +
    // update our radio button states ... get the service from
 +
    // a place that's most appropriate
 +
    ICommandService service = (ICommandService) HandlerUtil
 +
        .getActiveWorkbenchWindowChecked(event).getService(
 +
            ICommandService.class);
 +
    service.refreshElements(event.getCommand().getId(), null);
 +
    return null;
 +
  }
 +
 +
  public void updateElement(UIElement element, Map parameters) {
 +
    String parm = (String) parameters.get(PARM_INFO);
 +
    if (parm != null) {
 +
      if (fCurrentValue != null && fCurrentValue.equals(parm)) {
 +
        element.setChecked(true);
 +
      } else {
 +
        element.setChecked(false);
 +
      }
 +
    }
 +
  }
 +
}
 +
 
 +
 
  
 
== Menu Contribution ==
 
== Menu Contribution ==
  
a
+
Then you add menu contributions with the specific parameters that you want:
 +
 
 +
      <menuContribution
 +
            locationURI="menu:help?after=additions">
 +
        <separator
 +
              name="z.ex.dropdown.menu.separator1"
 +
              visible="true">
 +
        </separator>
 +
        <command
 +
              commandId="z.ex.dropdown.radio"
 +
              id="z.ex.dropdown.menu.radio1"
 +
              label="Moe"
 +
              style="radio">
 +
            <parameter
 +
                  name="z.ex.dropdown.radio.info"
 +
                  value="Moe">
 +
            </parameter>
 +
        </command>
 +
        <command
 +
              commandId="z.ex.dropdown.radio"
 +
              id="z.ex.dropdown.menu.radio2"
 +
              label="Larry"
 +
              style="radio">
 +
            <parameter
 +
                  name="z.ex.dropdown.radio.info"
 +
                  value="Larry">
 +
            </parameter>
 +
        </command>
 +
        <command
 +
              commandId="z.ex.dropdown.radio"
 +
              id="z.ex.dropdown.menu.radio3"
 +
              label="Curly"
 +
              style="radio">
 +
            <parameter
 +
                  name="z.ex.dropdown.radio.info"
 +
                  value="Curly">
 +
            </parameter>
 +
        </command>
 +
        <separator
 +
              name="z.ex.dropdown.menu.separator2"
 +
              visible="true">
 +
        </separator>
 +
      </menuContribution>
 +
  </extension>

Revision as of 13:09, 10 October 2007

You can create a command with a required parameter. The parameter will be passed during every execution.

Command Definition

You want to create a command that will be executed with a paramter. The parameter in this example matches which of the radio buttons is selected.

<command
      categoryId="org.eclipse.ui.category.help"
      defaultHandler="z.ex.dropdown.internal.RadioHandler"
      id="z.ex.dropdown.radio"
      name="Radio Example">
   <commandParameter
         id="z.ex.dropdown.radio.info"
         name="Radio Name"
         optional="false">
   </commandParameter>
</command>


Handler

The handler will receive the parameter. It can then update its model (in my example my model is a local variable, but that might not be appropriate in command that can have multiple handlers).

package com.example.handlers.internal;

import java.util.Map;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.commands.IElementUpdater;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.menus.UIElement;

public class RadioHandler extends AbstractHandler implements IElementUpdater {

 private static final String PARM_INFO = "z.ex.dropdown.radio.info";
 private String fCurrentValue;

 public Object execute(ExecutionEvent event) throws ExecutionException {
   String parm = event.getParameter(PARM_INFO);
   if (parm.equals(fCurrentValue)) {
     return null; // in theory, we're already in the correct state
   }
   
   // do whatever having "parm" active implies
   fCurrentValue = parm;
   
   
   // update our radio button states ... get the service from
   // a place that's most appropriate
   ICommandService service = (ICommandService) HandlerUtil
       .getActiveWorkbenchWindowChecked(event).getService(
           ICommandService.class);
   service.refreshElements(event.getCommand().getId(), null);
   return null;
 }

 public void updateElement(UIElement element, Map parameters) {
   String parm = (String) parameters.get(PARM_INFO);
   if (parm != null) {
     if (fCurrentValue != null && fCurrentValue.equals(parm)) {
       element.setChecked(true);
     } else {
       element.setChecked(false);
     }
   }
 }
}


Menu Contribution

Then you add menu contributions with the specific parameters that you want:

     <menuContribution
           locationURI="menu:help?after=additions">
        <separator
              name="z.ex.dropdown.menu.separator1"
              visible="true">
        </separator>
        <command
              commandId="z.ex.dropdown.radio"
              id="z.ex.dropdown.menu.radio1"
              label="Moe"
              style="radio">
           <parameter
                 name="z.ex.dropdown.radio.info"
                 value="Moe">
           </parameter>
        </command>
        <command
              commandId="z.ex.dropdown.radio"
              id="z.ex.dropdown.menu.radio2"
              label="Larry"
              style="radio">
           <parameter
                 name="z.ex.dropdown.radio.info"
                 value="Larry">
           </parameter>
        </command>
        <command
              commandId="z.ex.dropdown.radio"
              id="z.ex.dropdown.menu.radio3"
              label="Curly"
              style="radio">
           <parameter
                 name="z.ex.dropdown.radio.info"
                 value="Curly">
           </parameter>
        </command>
        <separator
              name="z.ex.dropdown.menu.separator2"
              visible="true">
        </separator>
     </menuContribution>
  </extension>