/*
  * (non-Javadoc)
  *
  * @see org.eclipse.jface.action.ContributionItem#dispose()
  */
 public void dispose() {
   if (elementRef != null) {
     commandService.unregisterElement(elementRef);
     elementRef = null;
   }
   if (commandListener != null) {
     command.getCommand().removeCommandListener(commandListener);
     commandListener = null;
   }
   command = null;
   commandService = null;
   disposeOldImages();
   super.dispose();
 }
 /*
  * (non-Javadoc)
  *
  * @see org.eclipse.jface.action.ContributionItem#isEnabled()
  */
 public boolean isEnabled() {
   if (command != null) {
     return command.getCommand().isEnabled();
   }
   return false;
 }
  /*
   * (non-Javadoc)
   *
   * @see org.eclipse.jface.action.ContributionItem#update(java.lang.String)
   */
  public void update(String id) {
    if (widget != null) {
      if (widget instanceof MenuItem) {
        MenuItem item = (MenuItem) widget;

        String text = label;
        if (text == null) {
          if (command != null) {
            try {
              text = command.getCommand().getName();
            } catch (NotDefinedException e) {
              WorkbenchPlugin.log(
                  "Update item failed " //$NON-NLS-1$
                      + getId(),
                  e);
            }
          }
        }
        // TODO: [bm] key bindings
        //				text = updateMnemonic(text);
        //
        //				String keyBindingText = null;
        //				if (command != null) {
        //					TriggerSequence[] bindings = bindingService
        //							.getActiveBindingsFor(command);
        //					if (bindings.length > 0) {
        //						keyBindingText = bindings[0].format();
        //					}
        //				}
        //				if (text != null) {
        //					if (keyBindingText == null) {
        item.setText(text);
        //					} else {
        //						item.setText(text + '\t' + keyBindingText);
        //					}
        //				}

        updateIcons();
        if (item.getSelection() != checkedState) {
          item.setSelection(checkedState);
        }

        boolean shouldBeEnabled = isEnabled();
        if (item.getEnabled() != shouldBeEnabled) {
          item.setEnabled(shouldBeEnabled);
        }
      } else if (widget instanceof ToolItem) {
        ToolItem item = (ToolItem) widget;

        if (icon != null) {
          updateIcons();
        } else if (label != null) {
          item.setText(label);
        }

        if (tooltip != null) item.setToolTipText(tooltip);
        else {
          String text = label;
          if (text == null) {
            if (command != null) {
              try {
                text = command.getCommand().getName();
              } catch (NotDefinedException e) {
                WorkbenchPlugin.log(
                    "Update item failed " //$NON-NLS-1$
                        + getId(),
                    e);
              }
            }
          }
          if (text != null) {
            item.setToolTipText(text);
          }
        }

        if (item.getSelection() != checkedState) {
          item.setSelection(checkedState);
        }

        boolean shouldBeEnabled = isEnabled();
        if (item.getEnabled() != shouldBeEnabled) {
          item.setEnabled(shouldBeEnabled);
        }
      }
    }
  }
  /**
   * Create a CommandContributionItem to place in a ContributionManager.
   *
   * @param serviceLocator a service locator that is most appropriate for this contribution.
   *     Typically the local {@link IWorkbenchWindow} or {@link IWorkbenchPartSite} will be
   *     sufficient.
   * @param id The id for this item. May be <code>null</code>. Items without an id cannot be
   *     referenced later.
   * @param commandId A command id for a defined command. Must not be <code>null</code>.
   * @param parameters A map of strings to strings which represent parameter names to values. The
   *     parameter names must match those in the command definition.
   * @param icon An icon for this item. May be <code>null</code>.
   * @param disabledIcon A disabled icon for this item. May be <code>null</code>.
   * @param hoverIcon A hover icon for this item. May be <code>null</code>.
   * @param label A label for this item. May be <code>null</code>.
   * @param mnemonic A mnemonic for this item to be applied to the label. May be <code>null</code>.
   * @param tooltip A tooltip for this item. May be <code>null</code>. Tooltips are currently only
   *     valid for toolbar contributions.
   * @param style The style of this menu contribution. See the STYLE_* contants.
   */
  public CommandContributionItem(
      IServiceLocator serviceLocator,
      String id,
      String commandId,
      Map parameters,
      ImageDescriptor icon,
      ImageDescriptor disabledIcon,
      ImageDescriptor hoverIcon,
      String label,
      String mnemonic,
      String tooltip,
      int style) {
    super(id);
    this.icon = icon;
    this.disabledIcon = disabledIcon;
    this.hoverIcon = hoverIcon;
    this.label = label;
    this.mnemonic = mnemonic;
    this.tooltip = tooltip;
    this.style = style;
    menuService = (IMenuService) serviceLocator.getService(IMenuService.class);
    commandService = (ICommandService) serviceLocator.getService(ICommandService.class);
    handlerService = (IHandlerService) serviceLocator.getService(IHandlerService.class);
    //		bindingService = (IBindingService) serviceLocator
    //				.getService(IBindingService.class);
    createCommand(commandId, parameters);

    if (command != null) {
      try {
        UIElement callback =
            new UIElement(serviceLocator) {

              public void setChecked(boolean checked) {
                CommandContributionItem.this.setChecked(checked);
              }

              public void setDisabledIcon(ImageDescriptor desc) {
                CommandContributionItem.this.setDisabledIcon(desc);
              }

              public void setHoverIcon(ImageDescriptor desc) {
                CommandContributionItem.this.setHoverIcon(desc);
              }

              public void setIcon(ImageDescriptor desc) {
                CommandContributionItem.this.setIcon(desc);
              }

              public void setText(String text) {
                CommandContributionItem.this.setText(text);
              }

              public void setTooltip(String text) {
                CommandContributionItem.this.setTooltip(text);
              }

              public void setDropDownId(String id) {
                dropDownMenuOverride = id;
              }
            };
        elementRef = commandService.registerElementForCommand(command, callback);
        command.getCommand().addCommandListener(getCommandListener());
        setImages(serviceLocator);
      } catch (NotDefinedException e) {
        WorkbenchPlugin.log(
            "Unable to register menu item \""
                + getId() //$NON-NLS-1$
                + "\", command \""
                + commandId
                + "\" not defined"); //$NON-NLS-1$ //$NON-NLS-2$
      }
    }
  }