@Override
  public void switchPerspective(MPerspective perspective) {
    Assert.isNotNull(perspective);
    MWindow window = getWindow();
    if (window != null && isInContainer(window, perspective)) {
      perspective.getParent().setSelectedElement(perspective);
      List<MPart> newPerspectiveParts =
          modelService.findElements(perspective, null, MPart.class, null);
      // if possible, keep the same active part across perspective switches
      if (newPerspectiveParts.contains(activePart)
          && partActivationHistory.isValid(perspective, activePart)) {
        MPart target = activePart;
        IEclipseContext activeChild = activePart.getContext().getParent().getActiveChild();
        if (activeChild != null) {
          activeChild.deactivate();
        }
        if (target.getContext() != null
            && target.getContext().get(MPerspective.class) != null
            && target.getContext().get(MPerspective.class).getContext()
                == perspective.getContext()) {
          target.getContext().activateBranch();
        } else {
          perspective.getContext().activate();
        }

        modelService.bringToTop(target);
        activate(target, true, false);
        return;
      }

      MPart newActivePart = perspective.getContext().getActiveLeaf().get(MPart.class);
      if (newActivePart == null) {
        // whatever part was previously active can no longer be found, find another one
        MPart candidate = partActivationHistory.getActivationCandidate(perspective);
        if (candidate != null) {
          modelService.bringToTop(candidate);
          activate(candidate, true, false);
          return;
        }
      }

      // there seems to be no parts in this perspective, just activate it as is then
      if (newActivePart == null) {
        modelService.bringToTop(perspective);
        perspective.getContext().activate();
      } else {
        if ((modelService.getElementLocation(newActivePart) & EModelService.IN_SHARED_AREA) != 0) {
          if (newActivePart.getParent().getSelectedElement() != newActivePart) {
            newActivePart = (MPart) newActivePart.getParent().getSelectedElement();
          }
        }
        activate(newActivePart, true, false);
      }
    }
  }
  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();
    }
  }
  /*
   * (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);
  }