@Override
 public void childRendered(MElementContainer<MUIElement> parentElement, MUIElement element) {
   super.childRendered(parentElement, element);
   ToolBar toolbar = (ToolBar) getUIContainer(element);
   if (toolbar != null && !toolbar.isDisposed()) {
     toolbar.getShell().layout(new Control[] {toolbar}, SWT.DEFER);
   }
 }
  /*
   * (non-Javadoc)
   *
   * @see
   * org.eclipse.e4.ui.internal.workbench.swt.AbstractPartRenderer#hideChild
   * (org.eclipse.e4.ui.model.application.MElementContainer,
   * org.eclipse.e4.ui.model.application.MUIElement)
   */
  @Override
  public void hideChild(MElementContainer<MUIElement> parentElement, MUIElement child) {
    super.hideChild(parentElement, child);

    // Since there's no place to 'store' a child that's not in a menu
    // we'll blow it away and re-create on an add
    Widget widget = (Widget) child.getWidget();
    if (widget != null && !widget.isDisposed()) widget.dispose();
  }
 /*
  * (non-Javadoc)
  *
  * @see
  * org.eclipse.e4.ui.workbench.renderers.swt.PartFactory#hookControllerLogic
  * (org.eclipse.e4.ui.model.application.MPart)
  */
 @Override
 public void hookControllerLogic(final MUIElement me) {
   super.hookControllerLogic(me);
   if (!(me instanceof MPart)) {
     return;
   }
   Widget widget = (Widget) me.getWidget();
   if (widget instanceof Composite) {
     widget.addListener(SWT.Activate, activationListener);
   }
 }
 @Override
 public void processContents(MElementContainer<MUIElement> container) {
   super.processContents(container);
   IEclipseContext ctx = getContext(container);
   ExpressionContext eContext = new ExpressionContext(ctx);
   ArrayList<MToolBarContribution> toContribute = new ArrayList<MToolBarContribution>();
   MElementContainer<?> toolbarObj = container;
   MToolBar toolbarModel = (MToolBar) toolbarObj;
   ContributionsAnalyzer.gatherToolBarContributions(
       toolbarModel,
       application.getToolBarContributions(),
       toolbarModel.getElementId(),
       toContribute,
       eContext);
   addToolBarContributions(toolbarModel, toContribute, ctx, eContext, pendingCleanup);
 }
  @Override
  public void hideChild(MElementContainer<MUIElement> parentElement, MUIElement child) {
    super.hideChild(parentElement, child);

    // Since there's no place to 'store' a child that's not in a menu
    // we'll blow it away and re-create on an add
    Widget widget = (Widget) child.getWidget();
    if (widget != null && !widget.isDisposed()) {
      widget.dispose();
    }
    ToolBar toolbar = (ToolBar) getUIContainer(child);
    if (toolbar != null && !toolbar.isDisposed()) {
      toolbar.getShell().layout(new Control[] {toolbar}, SWT.DEFER);
    }
    disposeToolbarIfNecessary(parentElement);
  }
  @Override
  public void disposeWidget(MUIElement element) {
    if (element instanceof MPart) {
      MPart part = (MPart) element;
      MToolBar toolBar = part.getToolbar();
      if (toolBar != null) {
        Widget widget = (Widget) toolBar.getWidget();
        if (widget != null) {
          unbindWidget(toolBar);
          widget.dispose();
        }
      }

      for (MMenu menu : part.getMenus()) {
        engine.removeGui(menu);
      }
    }
    super.disposeWidget(element);
  }
 @Override
 public void hideChild(MElementContainer<MUIElement> parentElement, MUIElement child) {
   super.hideChild(parentElement, child);
   // only handle the disposal of this element if it was actually rendered
   // by the engine
   if (child.getRenderer() != null) {
     // Since there's no place to 'store' a child that's not in a menu
     // we'll blow it away and re-create on an add
     Widget widget = (Widget) child.getWidget();
     if (widget != null && !widget.isDisposed()) {
       widget.dispose();
     }
     ToolBar toolbar = (ToolBar) getUIContainer(child);
     if (toolbar != null && !toolbar.isDisposed()) {
       toolbar.pack(true);
       toolbar.getShell().layout(new Control[] {toolbar}, SWT.DEFER);
     }
     // disposeToolbarIfNecessary(parentElement);
   }
 }
  /*
   * (non-Javadoc)
   *
   * @see
   * org.eclipse.e4.ui.workbench.renderers.swt.SWTPartRenderer#processContents
   * (org.eclipse.e4.ui.model.application.ui.MElementContainer)
   */
  @Override
  public void processContents(MElementContainer<MUIElement> container) {
    if (container.getChildren().size() == 0) {
      Object obj = container.getWidget();
      if (obj instanceof MenuItem) {
        MenuItem mi = (MenuItem) obj;
        if (mi.getMenu() == null) {
          mi.setMenu(new Menu(mi));
        }
        Menu menu = mi.getMenu();
        MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
        menuItem.setText(MenuManagerRendererFilter.NUL_MENU_ITEM);
        menuItem.setEnabled(false);
      }
    }

    super.processContents(container);

    Object obj = container.getWidget();
    Object menuObj = container;
    if ((obj instanceof Menu)
        && (((Menu) obj).getStyle() & SWT.BAR) != 0
        && (menuObj instanceof MMenu)) {
      MMenu menuModel = (MMenu) menuObj;
      IEclipseContext ctx = getContext(container);
      ExpressionContext eContext = new ExpressionContext(ctx);
      ArrayList<MMenuContribution> toContribute = new ArrayList<MMenuContribution>();
      ContributionsAnalyzer.gatherMenuContributions(
          menuModel,
          application.getMenuContributions(),
          menuModel.getElementId(),
          toContribute,
          eContext,
          false);
      addMenuBarContributions(menuModel, toContribute, ctx, eContext);
    }
  }
  /*
   * (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);
  }
 @Override
 public void init(IEclipseContext context) {
   super.init(context);
   application = context.get(MApplication.class);
 }
 @Override
 public void postProcess(MUIElement element) {
   super.postProcess(element);
   disposeToolbarIfNecessary(element);
 }