private void clearContext(MContext contextME) {
    MContext ctxt = (MContext) contextME;
    IEclipseContext lclContext = ctxt.getContext();
    if (lclContext != null) {
      IEclipseContext parentContext = lclContext.getParent();
      IEclipseContext child = parentContext.getActiveChild();
      if (child == lclContext) {
        child.deactivate();
      }

      ctxt.setContext(null);
      lclContext.dispose();
    }
  }
  private void internalFixContext(MPart part, MUIElement oldSelectedElement) {
    if (oldSelectedElement == null) {
      return;
    }

    MContext parentPart = getParentWithContext(oldSelectedElement);
    if (parentPart == null) {
      // technically this shouldn't happen as there should be an MWindow somewhere
      return;
    }
    IEclipseContext parentContext = parentPart.getContext();
    IEclipseContext oldContext = getSubContext(oldSelectedElement);
    Object child = parentContext.getActiveChild();
    if (child == null || oldContext == null || child == oldContext) {
      if (part == null) {
        // TBD this should not be necessary; deactivation is missing somewhere
        IEclipseContext currentActive = parentContext.getActiveChild();
        if (currentActive != null) currentActive.deactivate();
      } else part.getContext().activate();
    }
  }
  private static void populateModelInterfaces(
      MContext contextModel, IEclipseContext context, Class<?>[] interfaces) {
    for (Class<?> intf : interfaces) {
      Activator.trace(
          Policy.DEBUG_CONTEXTS,
          "Adding "
              + intf.getName()
              + " for " //$NON-NLS-1$ //$NON-NLS-2$
              + contextModel.getClass().getName(),
          null);
      context.set(intf.getName(), contextModel);

      populateModelInterfaces(contextModel, context, intf.getInterfaces());
    }
  }
  /*
   * (non-Javadoc)
   *
   * @see
   * org.eclipse.e4.ui.workbench.renderers.swt.SWTPartRenderer#disposeWidget
   * (org.eclipse.e4.ui.model.application.ui.MUIElement)
   */
  @Override
  public void disposeWidget(MUIElement element) {
    MPlaceholder ph = (MPlaceholder) element;
    MUIElement refElement = ph.getRef();
    Control refCtrl = (Control) refElement.getWidget();

    // Remove the element ref from the rendered list
    List<MPlaceholder> refs = renderedMap.get(refElement);
    refs.remove(ph);

    IEclipseContext curContext = modelService.getContainingContext(ph);

    if (refs.size() == 0) {
      // Ensure that the image is the 'original' image for this
      // part. See bug 347471 for details
      if (refElement instanceof MPart) {
        MPart thePart = (MPart) refElement;
        String imageURI = thePart.getIconURI();
        thePart.setIconURI(null);
        thePart.setIconURI(imageURI);
      }

      renderingEngine.removeGui(refElement);
    } else {
      // Ensure that the dispose of the element reference doesn't cascade
      // to dispose the 'real' part
      if (refCtrl != null && !refCtrl.isDisposed()) {
        MPlaceholder currentRef = refElement.getCurSharedRef();
        if (currentRef == ph) {
          // Find another *rendered* ref to pass the part on to
          for (MPlaceholder aPH : refs) {
            Composite phComp = (Composite) aPH.getWidget();
            if (phComp == null || phComp.isDisposed()) continue;

            // Reparent the context(s) (if any)
            IEclipseContext newParentContext = modelService.getContainingContext(aPH);
            List<MContext> allContexts =
                modelService.findElements(refElement, null, MContext.class, null);
            for (MContext ctxtElement : allContexts) {
              IEclipseContext theContext = ctxtElement.getContext();
              // this may be null if it hasn't been rendered yet
              if (theContext != null) {
                if (theContext.getParent() == curContext) {
                  // about to reparent the context, if we're
                  // the active child of the current parent,
                  // deactivate ourselves first
                  if (curContext.getActiveChild() == theContext) {
                    theContext.deactivate();
                  }
                  theContext.setParent(newParentContext);
                }
              }
            }

            // reset the 'cur' ref
            refElement.setCurSharedRef(aPH);

            // Reparent the widget
            refCtrl.setParent(phComp);
            break;
          }
        } else if (currentRef != null) {
          Composite phComp = (Composite) currentRef.getWidget();
          if (phComp == null || phComp.isDisposed()) {
            super.disposeWidget(element);
            return;
          }

          // Reparent the context(s) (if any)
          IEclipseContext newParentContext = modelService.getContainingContext(currentRef);
          List<MContext> allContexts =
              modelService.findElements(refElement, null, MContext.class, null);
          for (MContext ctxtElement : allContexts) {
            IEclipseContext theContext = ctxtElement.getContext();
            // this may be null if it hasn't been rendered yet
            if (theContext != null && theContext.getParent() == curContext) {
              // about to reparent the context, if we're the
              // active child of the current parent, deactivate
              // ourselves first
              if (curContext.getActiveChild() == theContext) {
                theContext.deactivate();
              }
              theContext.setParent(newParentContext);
            }
          }
        }
      }
    }

    super.disposeWidget(element);
  }
  public Object safeCreateGui(
      MUIElement element, Object parentWidget, IEclipseContext parentContext) {
    if (!element.isToBeRendered()) return null;

    if (!renderedElements.contains(element)) renderedElements.add(element);

    // no creates while processing a remove
    if (removeRoot != null) {
      return null;
    }

    Object currentWidget = element.getWidget();
    if (currentWidget != null) {
      if (currentWidget instanceof Control) {
        Control control = (Control) currentWidget;
        // make sure the control is visible
        control.setVisible(true);

        if (parentWidget instanceof Composite) {
          Composite currentParent = control.getParent();
          if (currentParent != parentWidget) {
            // check if the original parent was a tab folder
            if (currentParent instanceof CTabFolder) {
              CTabFolder folder = (CTabFolder) currentParent;
              // if we used to be the tab folder's top right
              // control, unset it
              if (folder.getTopRight() == control) {
                folder.setTopRight(null);
              }
            }

            // the parents are different so we should reparent it
            control.setParent((Composite) parentWidget);
          }
        }
      }

      // Reparent the context (or the kid's context)
      if (element instanceof MContext) {
        IEclipseContext ctxt = ((MContext) element).getContext();
        if (ctxt != null) ctxt.setParent(parentContext);
      } else {
        List<MContext> childContexts =
            modelService.findElements(element, null, MContext.class, null);
        for (MContext c : childContexts) {
          // Ensure that we only reset the context of our direct
          // children
          MUIElement kid = (MUIElement) c;
          MUIElement parent = kid.getParent();
          if (parent == null && kid.getCurSharedRef() != null)
            parent = kid.getCurSharedRef().getParent();
          if (!(element instanceof MPlaceholder) && parent != element) continue;

          if (c.getContext() != null && c.getContext().getParent() != parentContext) {
            c.getContext().setParent(parentContext);
          }
        }
      }

      // Now that we have a widget let the parent (if any) know
      if (element.getParent() instanceof MUIElement) {
        MElementContainer<MUIElement> parentElement = element.getParent();
        AbstractPartRenderer parentRenderer = getRendererFor(parentElement);
        if (parentRenderer != null) parentRenderer.childRendered(parentElement, element);
      }
      return element.getWidget();
    }

    if (element instanceof MContext) {
      MContext ctxt = (MContext) element;
      // Assert.isTrue(ctxt.getContext() == null,
      // "Before rendering Context should be null");
      if (ctxt.getContext() == null) {
        IEclipseContext lclContext = parentContext.createChild(getContextName(element));
        populateModelInterfaces(ctxt, lclContext, element.getClass().getInterfaces());
        ctxt.setContext(lclContext);

        // System.out.println("New Context: " + lclContext.toString()
        // + " parent: " + parentContext.toString());

        // make sure the context knows about these variables that have
        // been defined in the model
        for (String variable : ctxt.getVariables()) {
          lclContext.declareModifiable(variable);
        }

        Map<String, String> props = ctxt.getProperties();
        for (String key : props.keySet()) {
          lclContext.set(key, props.get(key));
        }

        E4Workbench.processHierarchy(element);
      }
    }

    // Create a control appropriate to the part
    Object newWidget = createWidget(element, parentWidget);

    // Remember that we've created the control
    if (newWidget != null) {
      AbstractPartRenderer renderer = getRendererFor(element);

      // Have the renderer hook up any widget specific listeners
      renderer.hookControllerLogic(element);

      // Process its internal structure through the renderer that created
      // it
      if (element instanceof MElementContainer) {
        renderer.processContents((MElementContainer<MUIElement>) element);
      }

      // Allow a final chance to set up
      renderer.postProcess(element);

      // Now that we have a widget let the parent (if any) know
      if (element.getParent() instanceof MUIElement) {
        MElementContainer<MUIElement> parentElement = element.getParent();
        AbstractPartRenderer parentRenderer = getRendererFor(parentElement);
        if (parentRenderer != null) parentRenderer.childRendered(parentElement, element);
      }
    } else {
      // failed to create the widget, dispose its context if necessary
      if (element instanceof MContext) {
        MContext ctxt = (MContext) element;
        IEclipseContext lclContext = ctxt.getContext();
        if (lclContext != null) {
          lclContext.dispose();
          ctxt.setContext(null);
        }
      }
    }

    return newWidget;
  }