/**
     * Returns the specified type of image.
     *
     * <p>All images are registered in the image registry of the {@link Activator}.
     *
     * @param type either {@link ICommandImageService#TYPE_DEFAULT} or {@link
     *     ICommandImageService#TYPE_DISABLED}
     * @return the image
     */
    private Image getImage(int type) {
      String key = myCommandId;
      switch (type) {
        case ICommandImageService.TYPE_DEFAULT:
          key += "-default";
          break;
        case ICommandImageService.TYPE_DISABLED:
          key += "-disabled";
          break;
        case ICommandImageService.TYPE_HOVER:
          key += "-hover";
          break;
        default:
          LogUtils.error(this, "Unknown type: " + type);
          break;
      }
      if (getToolBarImages().containsKey(key)) return getToolBarImages().get(key);

      final ImageDescriptor id =
          myCommandImageService.getImageDescriptor(
              myCommandId, type, ICommandImageService.IMAGE_STYLE_TOOLBAR);
      if (id == null) {
        getToolBarImages().put(key, null);
        return null;
      }
      final ImageRegistry imageRegistry = Activator.getDefault().getImageRegistry();
      imageRegistry.put(key, id);
      final Image image = imageRegistry.get(key);

      TOOLBAR_IMAGES.put(key, image);
      return image;
    }
 private static Image getImage(String path) {
   final ImageRegistry imageRegistry = Activator.getDefault().getImageRegistry();
   if (imageRegistry.getDescriptor(path) == null) {
     imageRegistry.put(path, AbstractUIPlugin.imageDescriptorFromPlugin(Activator.ID, path));
   }
   return imageRegistry.get(path);
 }
 @Override
 public void selectionChanged(SelectionChangedEvent event) {
   if (Activator.getDefault().TRACE_SOURCE_PROVIDER_VERBOSE) {
     LogUtils.debug(this, "new selection: " + event.getSelection());
     if (event.getSelection().isEmpty()) {
       LogUtils.debug(this, "empty");
     }
   }
   reportSourceChanges(myPreviousValueEvent);
 }
  @Override
  public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
    if (Activator.getDefault().TRACE_PROPERTY_TESTERS) {
      LogUtils.debug(this, Constants.PREFIX + property + "(" + receiver + ")");
    }
    if (!(receiver instanceof IManager)) {
      LogUtils.error(this, "Receiver not IManager: " + receiver);
      return false;
    }

    if (Constants.PROPERTY_CAN_UNDO.equals(property))
      return EditingDomainUtils.getCommandStack().canUndo();
    if (Constants.PROPERTY_CAN_REDO.equals(property))
      return EditingDomainUtils.getCommandStack().canRedo();
    if (Constants.PROPERTY_IS_SAVE_NEEDED.equals(property))
      return EditingDomainUtils.getCommandStack().isSaveNeeded();

    return false;
  }
  /** @param newState */
  private void handleContextChanges(final Map<String, Object> newState) {
    /*
     * If inside a container binding, then activate the proper context
     */
    final boolean cb =
        newState.get(Constants.SOURCES_ACTIVE_CONTAINER_BINDING)
            != IEvaluationContext.UNDEFINED_VARIABLE;
    if (cb && myContainerContextContextActivation == null) {
      myContainerContextContextActivation =
          myContextService.activateContext(Constants.CONTAINER_CONTEXT_ID);
      if (Activator.getDefault().TRACE_CONTEXTS) {
        LogUtils.debug(this, "activated " + Constants.CONTAINER_CONTEXT_ID);
      }
    }
    if (!cb && myContainerContextContextActivation != null) {
      myContextService.deactivateContext(myContainerContextContextActivation);
      myContainerContextContextActivation = null;
      if (Activator.getDefault().TRACE_CONTEXTS) {
        LogUtils.debug(this, "deactivated " + Constants.CONTAINER_CONTEXT_ID);
      }
    }

    /*
     * If inside an value binding, then activate the proper context
     */
    final boolean vb =
        !cb
            && newState.get(Constants.SOURCES_ACTIVE_BINDING)
                != IEvaluationContext.UNDEFINED_VARIABLE;
    if (vb && myWidgetContextContextActivation == null) {
      myWidgetContextContextActivation =
          myContextService.activateContext(Constants.WIDGET_CONTEXT_ID);
      if (Activator.getDefault().TRACE_CONTEXTS) {
        LogUtils.debug(this, "activated " + Constants.WIDGET_CONTEXT_ID);
      }
    }
    if (!vb && myWidgetContextContextActivation != null) {
      myContextService.deactivateContext(myWidgetContextContextActivation);
      myWidgetContextContextActivation = null;
      if (Activator.getDefault().TRACE_CONTEXTS) {
        LogUtils.debug(this, "deactivated " + Constants.WIDGET_CONTEXT_ID);
      }
    }

    /*
     * Also activate the base context - this does not seem to happen automatically ???
     */
    final boolean bc =
        myContainerContextContextActivation != null || myWidgetContextContextActivation != null;
    if (bc && myBaseContextContextActivation == null) {
      myBaseContextContextActivation =
          myContextService.activateContext(Constants.COMMON_CONTEXT_ID);
      if (Activator.getDefault().TRACE_CONTEXTS) {
        LogUtils.debug(this, "activated " + Constants.COMMON_CONTEXT_ID);
      }
    }
    if (!bc && myBaseContextContextActivation != null) {
      myContextService.deactivateContext(myBaseContextContextActivation);
      myBaseContextContextActivation = null;
      if (Activator.getDefault().TRACE_CONTEXTS) {
        LogUtils.debug(this, "deactivated " + Constants.COMMON_CONTEXT_ID);
      }
    }
  }
  /**
   * Checks if the current state have changed, and reports these.
   *
   * @param event the current event
   * @return the resulting map
   */
  public Map<String, Object> reportSourceChanges(Event event) {
    if (Activator.getDefault().TRACE_SOURCE_PROVIDER_VERBOSE
        && Activator.getDefault().TRACE_EVENTS_SWT) {
      LogUtils.debug(
          this, (event == myPreviousValueEvent ? "REPLAY " : "") + TSSWTUtils.toString(event));
    }

    /*
     * If the event is FocusIn for the very same widget as last time, but with x,y=0,0, then...
     * ignore it... It is seen whenever the current shell is send to front.
     */
    if (event.type == SWT.FocusIn && event.widget == myLastWidget && event.x == 0 && event.y == 0)
      return myCurrentState;

    /*
     * Ignore all key up events, except those that navigate...
     */
    // if (event.type == SWT.KeyUp && (SWT.KEYCODE_BIT & event.keyCode) == 0) {
    // return myOldState;
    // }

    final Map<String, Object> newState = getCurrentState(event);
    myLastWidget = event.widget;

    /*
     * Update the current state with changes - keeping them in newState as well.
     */
    for (final Iterator<Map.Entry<String, Object>> is = newState.entrySet().iterator();
        is.hasNext(); ) {
      final Map.Entry<String, Object> i = is.next();
      final String s = i.getKey();
      final Object n = i.getValue();

      final Object o = myCurrentState.get(s);
      if (BasicUtils.equals(n, o)) {
        is.remove();
      } else {
        myCurrentState.put(s, n);
      }
    }

    if (!newState.isEmpty()) {
      /*
       * Reset the property testers as well, when any of the values changes
       */
      newState.put(Constants.PREFIX + Constants.PROPERTY_CAN_DELETE, true);
      newState.put(Constants.PREFIX + Constants.PROPERTY_CAN_DELETE_SELECTED_OBJECTS, true);

      if (Activator.getDefault().TRACE_SOURCE_PROVIDER) {
        final StringBuilder sb = new StringBuilder("Binding sources change(" + myLastBinding + ")");
        for (final Map.Entry<String, Object> i : newState.entrySet()) {
          final String s = i.getKey();
          sb.append("\n  ").append(s).append('=');
          final Object v = i.getValue();
          if (v == null) {
            sb.append("<null>");
          } else if (v == IEvaluationContext.UNDEFINED_VARIABLE) {
            sb.append("<undef>");
          } else {
            sb.append('\'')
                .append(v.toString())
                .append('\'')
                .append(" [")
                .append(ClassUtils.getLastClassName(v))
                .append(']');
          }
        }
        LogUtils.debug(this, sb.toString());
      }

      fireSourceChanged(ISources.ACTIVE_CURRENT_SELECTION, newState);

      /*
       * TODO: describe why
       *
       * TODO: only when changing?
       */
      final Object activeBindingObject = myCurrentState.get(Constants.SOURCES_ACTIVE_BINDING);
      if (activeBindingObject instanceof IValueBinding) {
        final IValueBinding vb = (IValueBinding) activeBindingObject;
        IManagerRunnable.Factory.asyncExec(
            "update",
            vb,
            new Runnable() {
              @Override
              public void run() {
                if (vb.isDisposed()) return;
                vb.updateBinding();
              }
            });
      }

      /*
       * And lastly, update the active contexts
       */
      handleContextChanges(myCurrentState);
    }

    return myCurrentState;
  }