public IEclipseContext getActiveLeaf() {
   IEclipseContext activeContext = this;
   IEclipseContext child = getActiveChild();
   while (child != null) {
     activeContext = child;
     child = child.getActiveChild();
   }
   return activeContext;
 }
  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();
    }
  }
  public void testContextChildren() {
    final MWindow window = createWindowWithOneView();
    wb = new E4Workbench(window, appContext);

    Widget topWidget = (Widget) window.getWidget();
    assertTrue(topWidget instanceof Shell);
    Shell shell = (Shell) topWidget;
    assertEquals("MyWindow", shell.getText());

    // should get the window context
    IEclipseContext child = appContext.getActiveChild();
    assertNotNull(child);
    assertEquals(window.getContext(), child);

    MPart modelPart = getContributedPart(window);
    assertNotNull(modelPart);
    assertEquals(window, modelPart.getParent().getParent().getParent());

    // "activate" the part, same as (in theory) an
    // SWT.Activate event.
    AbstractPartRenderer factory = (AbstractPartRenderer) modelPart.getRenderer();
    factory.activate(modelPart);

    IEclipseContext next = child.getActiveChild();
    while (next != null) {
      child = next;
      next = child.getActiveChild();
      if (next == child) {
        fail("Cycle detected in part context");
        break;
      }
    }
    assertFalse(window.getContext() == child);

    MPart contextPart = (MPart) child.get(MPart.class.getName());

    assertNotNull(contextPart);
    assertEquals(window, contextPart.getParent().getParent().getParent());
  }
  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();
    }
  }
        public void selectionChanged(MPart part, Object selection) {
          selection = createCompatibilitySelection(selection);
          context.set(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);

          IEclipseContext applicationContext = application.getContext();
          if (applicationContext.getActiveChild() == context) {
            application.getContext().set(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
          }

          Object client = part.getObject();
          if (client instanceof CompatibilityPart) {
            IWorkbenchPart workbenchPart = ((CompatibilityPart) client).getPart();
            notifyListeners(part.getElementId(), workbenchPart, (ISelection) selection);
          }
        }
  /**
   * Updates the selection of the workbench window with that of the active part's.
   *
   * @param activePart the currently active part
   */
  public void updateSelection(IWorkbenchPart activePart) {
    if (activePart != null) {
      ISelectionProvider selectionProvider = activePart.getSite().getSelectionProvider();
      if (selectionProvider != null) {
        ISelection selection = selectionProvider.getSelection();
        context.set(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);

        IEclipseContext applicationContext = application.getContext();
        if (applicationContext.getActiveChild() == context) {
          application.getContext().set(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
        }

        notifyListeners(activePart.getSite().getId(), activePart, selection);
        notifyPostSelectionListeners(activePart.getSite().getId(), activePart, selection);
      }
    }
  }
  /*
   * (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 void testGet_ActiveChild2() throws Exception {
    IEclipseContext context = getActiveChildContext(application);

    assertNotNull(context.getActiveChild());
  }
  @Override
  public void testGet_ActiveChild() throws Exception {
    IEclipseContext context = application.getContext();

    assertNotNull(context.getActiveChild());
  }