private Object safeCreateGui(MUIElement element) {
    // Obtain the necessary parent widget
    Object parent = null;
    MUIElement parentME = element.getParent();
    if (parentME == null) parentME = (MUIElement) ((EObject) element).eContainer();
    if (parentME != null) {
      AbstractPartRenderer renderer = getRendererFor(parentME);
      if (renderer != null) {
        if (!element.isVisible()) {
          parent = getLimboShell();
        } else {
          parent = renderer.getUIContainer(element);
        }
      }
    }

    // Obtain the necessary parent context
    IEclipseContext parentContext = null;
    if (element.getCurSharedRef() != null) {
      MPlaceholder ph = element.getCurSharedRef();
      parentContext = getContext(ph.getParent());
    } else if (parentContext == null && element.getParent() != null) {
      parentContext = getContext(element.getParent());
    } else if (parentContext == null && element.getParent() == null) {
      parentContext = getContext((MUIElement) ((EObject) element).eContainer());
    }

    return safeCreateGui(element, parent, parentContext);
  }
  @Override
  public boolean isLastEditorStack(MUIElement stack) {
    if (!(stack instanceof MPartStack)) {
      return false;
    }

    // is it in the shared area?
    MUIElement parent = stack.getParent();
    while (parent != null && !(parent instanceof MArea)) {
      parent = parent.getParent();
    }
    if (parent == null) {
      return false;
    }

    // OK, it's in the area, is it the last TBR one ?
    MArea area = (MArea) parent;
    List<MPartStack> stacks = findElements(area, null, MPartStack.class, null);
    int count = 0;
    for (MPartStack aStack : stacks) {
      if (aStack.isToBeRendered()) {
        count++;
      }
    }
    return count < 2 && stack.isToBeRendered();
  }
Exemplo n.º 3
0
  /**
   * Cancel the drag operation. The default implementation will return the dragElement to its
   * original location in the model.
   */
  public void cancelDrag() {
    if (dragPH == null) return;

    // if the dragElement is *not* directly after the placeholder we have to return it there
    List<MUIElement> phParentsKids = dragPH.getParent().getChildren();
    if (phParentsKids.indexOf(dragElement) != phParentsKids.indexOf(dragPH) + 1) {
      dragElement.setToBeRendered(false);
      if (dragElement.getParent() != null)
        dragElement.getParent().getChildren().remove(dragElement);
      phParentsKids.add(phParentsKids.indexOf(dragPH) + 1, dragElement);
      dragElement.setVisible(true);
      dragElement.setToBeRendered(true);
    }
  }
  /*
   * (non-Javadoc)
   *
   * @see
   * org.eclipse.e4.ui.workbench.renderers.AbstractPartRenderer#getUIContainer
   * (org.eclipse.e4.ui.model.application.MUIElement)
   */
  @Override
  public Object getUIContainer(MUIElement element) {
    if (!(element instanceof MMenuElement)) return null;

    if (element.getParent().getWidget() instanceof MenuItem) {
      MenuItem mi = (MenuItem) element.getParent().getWidget();
      if (mi.getMenu() == null) {
        mi.setMenu(new Menu(mi));
      }
      return mi.getMenu();
    }

    return super.getUIContainer(element);
  }
  private MContext getParentWithContext(MUIElement part) {
    MElementContainer<MUIElement> parent = part.getParent();
    MUIElement intermediate = parent;
    if (intermediate == null) {
      intermediate = part;
    } else {
      while (parent != null) {
        if (parent instanceof MContext) {
          if (((MContext) parent).getContext() != null) return (MContext) parent;
        }
        intermediate = parent;
        parent = parent.getParent();
      }
    }

    MPlaceholder placeholder = modelService.findPlaceholderFor(getWindow(), intermediate);
    parent = placeholder.getParent();
    while (parent != null) {
      if (parent instanceof MContext) {
        if (((MContext) parent).getContext() != null) return (MContext) parent;
      }
      parent = parent.getParent();
    }
    return null;
  }
  @Override
  public boolean isPartVisible(MPart part) {
    if (isInContainer(part)) {
      MUIElement element = part;
      MElementContainer<?> parent = part.getParent();
      if (parent == null) {
        // might be a shared part
        element = part.getCurSharedRef();
        if (element == null) {
          return false;
        }

        parent = element.getParent();
        if (parent == null) {
          return false;
        }
      }

      if (parent instanceof MPartStack) {
        return parent.getSelectedElement() == element;
      }

      return element.isVisible();
    }
    return false;
  }
  /*
   * (non-Javadoc)
   *
   * @see
   * org.eclipse.e4.ui.internal.workbench.swt.AbstractPartRenderer#createWidget
   * (org.eclipse.e4.ui.model.application.ui.MUIElement, java.lang.Object)
   */
  @Override
  public Object createWidget(final MUIElement element, Object parent) {
    if (!(element instanceof MToolBar) || !(parent instanceof Composite)) return null;

    final MToolBar toolbarModel = (MToolBar) element;
    ToolBar newTB = createToolbar(toolbarModel, (Composite) parent);
    bindWidget(element, newTB);
    processContribution(toolbarModel, toolbarModel.getElementId());

    Control renderedCtrl = newTB;
    MUIElement parentElement = element.getParent();
    if (parentElement instanceof MTrimBar) {
      element.getTags().add("Draggable"); // $NON-NLS-1$

      setCSSInfo(element, newTB);

      boolean vertical = false;
      MTrimBar bar = (MTrimBar) parentElement;
      vertical = bar.getSide() == SideValue.LEFT || bar.getSide() == SideValue.RIGHT;
      IEclipseContext parentContext = getContextForParent(element);
      CSSRenderingUtils cssUtils = parentContext.get(CSSRenderingUtils.class);
      if (cssUtils != null) {
        renderedCtrl = (Composite) cssUtils.frameMeIfPossible(newTB, null, vertical, true);
      }
    }

    return renderedCtrl;
  }
  private ToolBar createToolbar(final MUIElement element, Composite parent) {
    int orientation = getOrientation(element);

    ToolBarManager manager = getManager((MToolBar) element);
    if (manager == null) {
      manager = new ToolBarManager(orientation | SWT.WRAP | SWT.FLAT | SWT.RIGHT);
      IContributionManagerOverrides overrides = null;
      MApplicationElement parentElement = element.getParent();
      if (parentElement == null) {
        parentElement = (MApplicationElement) ((EObject) element).eContainer();
      }

      if (parentElement != null) {
        overrides =
            (IContributionManagerOverrides)
                parentElement.getTransientData().get(IContributionManagerOverrides.class.getName());
      }

      manager.setOverrides(overrides);
      linkModelToManager((MToolBar) element, manager);
    }
    ToolBar bar = manager.createControl(parent);
    bar.setData(manager);
    bar.setData(AbstractPartRenderer.OWNING_ME, element);
    bar.getShell().layout(new Control[] {bar}, SWT.DEFER);
    bar.addDisposeListener(
        new DisposeListener() {
          public void widgetDisposed(DisposeEvent e) {
            cleanUp((MToolBar) element);
          }
        });
    return bar;
  }
Exemplo n.º 9
0
  protected Command computeCommand() {
    MUIElement selectedPart = model;

    if (selectedPart instanceof MPart) {
      MGenericStack<MUIElement> partStack = findParentStack();
      if (partStack != null) {
        selectedPart = partStack;
      }
    }

    MElementContainer<MUIElement> parent = partStack.getParent();
    List<MUIElement> children = parent.getChildren();
    int index = children.indexOf(partStack);
    if (parent instanceof MGenericTile<?>) {
      MGenericTile<?> genericTile = (MGenericTile<?>) parent;
      int modelIndex = children.indexOf(selectedPart);
      if (modelIndex == 0 && index == 1 && children.size() == 2 && genericTile.isHorizontal()) {
        return UnexecutableCommand.INSTANCE;
      }
    }

    CompoundCommand result = new CompoundCommand();

    MPartSashContainer newSash = MBasicFactory.INSTANCE.createPartSashContainer();
    String preferData = partStack.getContainerData();
    newSash.setContainerData(preferData);
    newSash.setHorizontal(true);

    if (selectedPart instanceof MPartStack) {
      if (selectedPart.getParent() == null) {
        selectedPart.setContainerData(preferData);
        result.add(CommandFactory.createAddChildCommand(newSash, selectedPart, 0));
      } else {
        result.add(new ChangeParentCommand(newSash, selectedPart, 0));
        if (!preferData.equals(selectedPart.getContainerData())) {
          result.add(
              new ApplyAttributeSettingCommand(
                  (EObject) selectedPart, "containerData", preferData));
        }
      }
    } else if (selectedPart instanceof MPart) {
      MPart part = (MPart) selectedPart;
      MPartStack createPartStack = MBasicFactory.INSTANCE.createPartStack();
      createPartStack.setContainerData(preferData);
      if (part.getParent() != null) {
        result.add(new ChangeParentCommand(createPartStack, part, 0));
      } else {
        result.add(CommandFactory.createAddChildCommand(createPartStack, part, 0));
      }
      result.add(CommandFactory.createAddChildCommand(newSash, createPartStack, 0));
    }

    result.add(new ChangeParentCommand(newSash, partStack, 1));

    result.add(CommandFactory.createAddChildCommand(parent, newSash, index));
    return result.unwrap();
  }
  @Override
  public void dragEnter(MUIElement dragElement, DnDInfo info) {
    super.dragEnter(dragElement, info);

    clientBounds = dropCTF.getClientArea();
    clientBounds = Display.getCurrent().map(dropCTF, null, clientBounds);
    ctfBounds = dropCTF.getBounds();
    ctfBounds = Display.getCurrent().map(dropCTF.getParent(), null, ctfBounds);

    // Find the root of the dropStack's sash structure
    outerRelTo = dropStack.getParent();
    if (outerRelTo instanceof MPartSashContainer) {
      while (outerRelTo != null && !(outerRelTo.getWidget() instanceof Composite))
        outerRelTo = outerRelTo.getParent();
    }

    // If the stack is in an MArea or a Perspective then allow 'outer' docking
    if (outerRelTo instanceof MArea) {
      // If the relTo is in the MArea then use its 'curSharedRef'
      outerRelTo = outerRelTo.getCurSharedRef();
    } else if (outerRelTo instanceof MPartSashContainer) {
      MUIElement relToParent = outerRelTo.getParent();
      if (relToParent instanceof MArea) {
        outerRelTo = relToParent.getCurSharedRef();
      } else if (relToParent instanceof MPerspective) {
        outerRelTo = relToParent.getParent(); // PerspectiveStack
      } else {
        outerRelTo = null;
      }
    } else {
      outerRelTo = null;
    }

    if (outerRelTo != null) {
      Composite outerComposite = (Composite) outerRelTo.getWidget();
      ocBounds = outerComposite.getBounds();
      ocBounds = Display.getCurrent().map(outerComposite.getParent(), null, ocBounds);
    } else {
      ocBounds = null;
    }

    getDockLocation(info);
    showFeedback(curDockLocation);
  }
 int getOrientation(final MUIElement element) {
   MUIElement theParent = element.getParent();
   if (theParent instanceof MTrimBar) {
     MTrimBar trimContainer = (MTrimBar) theParent;
     SideValue side = trimContainer.getSide();
     if (side.getValue() == SideValue.LEFT_VALUE || side.getValue() == SideValue.RIGHT_VALUE)
       return SWT.VERTICAL;
   }
   return SWT.HORIZONTAL;
 }
 @Override
 public Object getUIContainer(MUIElement childElement) {
   Composite intermediate = (Composite) super.getUIContainer(childElement);
   if (intermediate == null || intermediate.isDisposed()) {
     return null;
   }
   ToolBar toolbar = findToolbar(intermediate);
   if (toolbar == null) {
     toolbar = createToolbar(childElement.getParent(), intermediate);
   }
   return toolbar;
 }
Exemplo n.º 13
0
  /**
   * Start a drag operation on the given element.
   *
   * @param element The element to drag
   */
  public void dragStart(DnDInfo info) {
    // cache a placeholder where the element started (NOTE: this also prevents the parent from
    // being auto-removed by going 'empty'
    if (dragElement.getParent() != null) {
      if (dragElement instanceof MStackElement)
        dragPH = AdvancedFactoryImpl.eINSTANCE.createPlaceholder();
      else if (dragElement instanceof MPartStack)
        dragPH = BasicFactoryImpl.eINSTANCE.createPartSashContainer();
      else if (dragElement instanceof MTrimElement)
        dragPH = MenuFactoryImpl.eINSTANCE.createToolControl();

      dragPH.setElementId(DRAG_PLACEHOLDER_ID);
      dragPH.setToBeRendered(false);

      List<MUIElement> kids = dragElement.getParent().getChildren();
      kids.add(kids.indexOf(dragElement), dragPH);
    }

    dropAgent = dndManager.getDropAgent(dragElement, info);
    if (dropAgent != null) dropAgent.dragEnter(dragElement, info);
  }
  private void createElement(MUIElement element) {
    if (modelService.isHostedElement(element, workbenchWindow)) {
      // assume the client has full control
      return;
    }

    MPlaceholder placeholder = element.getCurSharedRef();
    if (placeholder != null) {
      element.setToBeRendered(true);
      element = placeholder;
    }

    // render this element
    element.setToBeRendered(true);

    // render all of its parents
    MUIElement parentWindow = workbenchWindow;
    // determine the top parent that needs to be forcibly created
    MUIElement target = null;
    MElementContainer<MUIElement> parent = element.getParent();
    while (parent != null && parent != parentWindow) {
      parent.setToBeRendered(true);
      if (parent.getWidget() == null) {
        target = parent;
      }
      parent = parent.getParent();
    }
    if (target != null) {
      // force the element's parent hierarchy to be created
      engine.createGui(target);
    }
    // ask the engine to create the element
    if (element.getWidget() == null) engine.createGui(element);

    parent = element.getParent();
    if (parent != null && parent.getChildren().size() == 1) {
      // if we're the only child, set ourselves as the selected element
      parent.setSelectedElement(element);
    }
  }
 /**
  * Returns the parent container of the specified element. If one cannot be found, a check will be
  * performed to see whether the element is being represented by a placeholder, if it is, the
  * placeholder's parent will be returned, if any.
  *
  * @param element the element to query
  * @return the element's parent container, or the parent container of the specified element's
  *     current placeholder, if it has one
  */
 private MElementContainer<MUIElement> getParent(MUIElement element) {
   MElementContainer<MUIElement> parent = element.getParent();
   if (parent == null) {
     MPlaceholder placeholder = element.getCurSharedRef();
     if (placeholder == null) {
       @SuppressWarnings("unchecked")
       MElementContainer<MUIElement> container = (MElementContainer<MUIElement>) getContainer();
       return findContainer(container, element);
     }
     return placeholder.getParent();
   }
   return parent;
 }
  private void showElementInWindow(MWindow window, MUIElement element) {
    MUIElement parent = element.getParent();
    if (parent == null) {
      MPlaceholder ph = findPlaceholderFor(window, element);
      if (ph != null) {
        element = ph;
        parent = element.getParent();
      }
    }

    if (parent == null && element instanceof MWindow) {
      // no parent but is a window, could be a detached window then
      parent = (MUIElement) ((EObject) element).eContainer();
      if (parent != null) {
        // Force the element to be rendered
        if (!element.isToBeRendered()) {
          element.setToBeRendered(true);
        }

        if (window != parent) {
          showElementInWindow(window, parent);
        }
      }
    } else if (parent != null) {
      // Force the element to be rendered
      if (!element.isToBeRendered()) {
        element.setToBeRendered(true);
      }

      @SuppressWarnings("unchecked")
      MElementContainer<MUIElement> container = (MElementContainer<MUIElement>) parent;
      container.setSelectedElement(element);
      if (window != parent) {
        showElementInWindow(window, parent);
      }
    }
  }
 @Override
 public Object getUIContainer(MUIElement element) {
   if (element instanceof MToolBar) {
     MUIElement container = (MUIElement) ((EObject) element).eContainer();
     MUIElement parent = container.getParent();
     if (parent == null) {
       MPlaceholder placeholder = container.getCurSharedRef();
       if (placeholder != null) {
         return placeholder.getParent().getWidget();
       }
     } else {
       return parent.getWidget();
     }
   }
   return super.getUIContainer(element);
 }
  @Override
  public boolean isHostedElement(MUIElement element, MWindow hostWindow) {
    MUIElement curElement = element;
    while (curElement != null && !curElement.getTags().contains(HOSTED_ELEMENT)) {
      if (curElement.getCurSharedRef() != null) {
        curElement = curElement.getCurSharedRef();
      } else {
        curElement = curElement.getParent();
      }
    }

    if (curElement == null) {
      return false;
    }

    return hostWindow.getSharedElements().contains(curElement);
  }
        public void handleEvent(Event event) {
          MUIElement changedElement = (MUIElement) event.getProperty(UIEvents.EventTags.ELEMENT);
          MUIElement parent = changedElement.getParent();
          if (parent == null) {
            parent = (MUIElement) ((EObject) changedElement).eContainer();
            if (parent == null) {
              return;
            }
          }

          AbstractPartRenderer renderer = (AbstractPartRenderer) parent.getRenderer();
          if (renderer == null || parent instanceof MToolBar) return;

          // Re-parent the control based on the visible state
          if (changedElement.isVisible()) {
            if (changedElement.isToBeRendered()) {
              if (changedElement.getWidget() instanceof Control) {
                // Ensure that the control is under its 'real' parent if
                // it's visible
                Composite realComp = (Composite) renderer.getUIContainer(changedElement);
                Control ctrl = (Control) changedElement.getWidget();
                ctrl.setParent(realComp);
                fixZOrder(changedElement);
              }

              if (parent instanceof MElementContainer<?>) {
                renderer.childRendered((MElementContainer<MUIElement>) parent, changedElement);
              }
            }
          } else {
            // Put the control under the 'limbo' shell
            if (changedElement.getWidget() instanceof Control) {
              Control ctrl = (Control) changedElement.getWidget();

              if (!(ctrl instanceof Shell)) {
                ctrl.getShell().layout(new Control[] {ctrl}, SWT.DEFER);
              }

              ctrl.setParent(getLimboShell());
            }

            if (parent instanceof MElementContainer<?>) {
              renderer.hideChild((MElementContainer<MUIElement>) parent, changedElement);
            }
          }
        }
  @Override
  public boolean canDrop(MUIElement dragElement, DnDInfo info) {
    if (!(dragElement instanceof MStackElement) && !(dragElement instanceof MPartStack))
      return false;

    dropStack = null;

    // Hack! allow splitting the 'empty' editor area stack
    if (info.curElement instanceof MPartStack) {
      MPartStack stack = (MPartStack) info.curElement;
      if (dndManager.getModelService().isLastEditorStack(stack)) dropStack = stack;
    }

    if (dropStack == null) {
      if (!(info.curElement instanceof MStackElement)
          && !dndManager.getModelService().isLastEditorStack(info.curElement)) return false;

      // Detect placeholders
      MUIElement parent = info.curElement.getParent();
      if (info.curElement instanceof MPart && info.curElement.getCurSharedRef() != null)
        parent = info.curElement.getCurSharedRef().getParent();

      if (!(parent instanceof MPartStack) || !(parent.getWidget() instanceof CTabFolder))
        return false;

      dropStack = (MPartStack) parent;
    }

    // You can only drag MParts from window to window
    if (!(dragElement instanceof MPart)) {
      EModelService ms = dndManager.getModelService();
      MWindow dragElementWin = ms.getTopLevelWindowFor(dragElement);
      MWindow dropWin = ms.getTopLevelWindowFor(dropStack);
      if (dragElementWin != dropWin) return false;
    }

    // We can't split ourselves with if the element being dragged is the only element in the
    // stack (we check for '2' because the dragAgent puts a Drag Placeholder in the stack)
    MUIElement dragParent = dragElement.getParent();
    if (dragParent == dropStack && dropStack.getChildren().size() == 2) return false;

    dropCTF = (CTabFolder) dropStack.getWidget();

    return true;
  }
Exemplo n.º 21
0
  /**
   * Restore the DragAgent to a state where it will be ready to start a new drag
   *
   * @param performDrop determines if a drop operation should be performed if possible
   */
  public void dragFinished(boolean performDrop, DnDInfo info) {
    boolean isNoDrop =
        dndManager.getDragShell().getCursor()
            == Display.getCurrent().getSystemCursor(SWT.CURSOR_NO);
    if (performDrop && dropAgent != null && !isNoDrop) {
      dropAgent.drop(dragElement, info);
    } else {
      cancelDrag();
    }

    if (dropAgent != null) dropAgent.dragLeave(dragElement, info);

    if (dragPH == null) return;

    if (dragPH != null) {
      dragPH.getParent().getChildren().remove(dragPH);
      dragPH = null;
    }

    dragElement = null;
  }
  @Override
  public void bringToTop(MPart part) {
    if (isInContainer(part)) {
      MUIElement currentElement = part;
      MElementContainer<MUIElement> parent = part.getParent();
      if (parent == null) {
        currentElement = modelService.findPlaceholderFor(getWindow(), part);
        parent = currentElement.getParent();
      }

      // If the part is in the same stack as the currently active part then activate it
      // instead
      MElementContainer<MUIElement> activeParent =
          activePart != null ? activePart.getParent() : null;
      if (activePart != null && activeParent == null) {
        MPlaceholder activePH = modelService.findPlaceholderFor(getWindow(), activePart);
        if (activePH != null) {
          activeParent = activePH.getParent();
        }
      }
      if (parent == activeParent && part != activePart) {
        activate(part);
        return;
      }

      MUIElement oldSelectedElement = parent.getSelectedElement();

      delegateBringToTop(part);

      // check to make sure that the currently selected element is actually valid
      if (oldSelectedElement != currentElement
          && parent.getChildren().contains(oldSelectedElement)
          && parent instanceof MGenericStack<?>) {
        if (oldSelectedElement instanceof MPlaceholder) {
          oldSelectedElement = ((MPlaceholder) oldSelectedElement).getRef();
        }
        internalFixContext(part, oldSelectedElement);
      }
    }
  }
        public void handleEvent(Event event) {

          MUIElement changedElement = (MUIElement) event.getProperty(UIEvents.EventTags.ELEMENT);
          MElementContainer<?> parent = changedElement.getParent();

          // Handle Detached Windows
          if (parent == null) {
            parent = (MElementContainer<?>) ((EObject) changedElement).eContainer();
          }

          boolean menuChild = parent instanceof MMenu;

          // If the parent isn't displayed who cares?
          if (!(parent instanceof MApplication)
              && (parent == null || parent.getWidget() == null || menuChild)) return;

          if (changedElement.isToBeRendered()) {
            Activator.trace(Policy.DEBUG_RENDERER, "visible -> true", null); // $NON-NLS-1$

            // Note that the 'createGui' protocol calls 'childAdded'
            Object w = createGui(changedElement);
            if (w instanceof Control && !(w instanceof Shell)) {
              fixZOrder(changedElement);
            }
          } else {
            Activator.trace(Policy.DEBUG_RENDERER, "visible -> false", null); // $NON-NLS-1$

            // Ensure that the element about to be removed is not the
            // selected element
            if (parent.getSelectedElement() == changedElement) parent.setSelectedElement(null);

            // Un-maximize the element before tearing it down
            if (changedElement.getTags().contains(MAXIMIZED))
              changedElement.getTags().remove(MAXIMIZED);

            // Note that the 'removeGui' protocol calls 'childRemoved'
            removeGui(changedElement);
          }
        }
  protected void fixZOrder(MUIElement element) {
    MElementContainer<MUIElement> parent = element.getParent();
    if (parent == null) {
      Object container = ((EObject) element).eContainer();
      if (container instanceof MElementContainer<?>) {
        parent = (MElementContainer<MUIElement>) container;
      }
    }
    if (parent == null || !(element.getWidget() instanceof Control)) return;

    Control elementCtrl = (Control) element.getWidget();
    Control prevCtrl = null;
    for (MUIElement kid : parent.getChildren()) {
      if (kid == element) {
        if (prevCtrl != null) elementCtrl.moveBelow(prevCtrl);
        else elementCtrl.moveAbove(null);
        break;
      } else if (kid.getWidget() instanceof Control) {
        prevCtrl = (Control) kid.getWidget();
      }
    }

    Object widget = parent.getWidget();
    if (widget instanceof Composite) {
      Composite composite = (Composite) widget;
      if (composite.getShell() == elementCtrl.getShell()) {
        Composite temp = elementCtrl.getParent();
        while (temp != composite) {
          if (temp == null) {
            return;
          }
          temp = temp.getParent();
        }

        composite.layout(true, true);
      }
    }
  }
  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;
  }
  private void safeRemoveGui(MUIElement element) {
    if (removeRoot == null) removeRoot = element;
    renderedElements.remove(element);

    // We call 'hideChild' *before* checking if the actual element
    // has been rendered in order to pick up cases of 'lazy loading'
    MUIElement parent = element.getParent();
    AbstractPartRenderer parentRenderer = parent != null ? getRendererFor(parent) : null;
    if (parentRenderer != null) {
      parentRenderer.hideChild(element.getParent(), element);
    }

    AbstractPartRenderer renderer = getRendererFor(element);

    // If the element hasn't been rendered then this is a NO-OP
    if (renderer != null) {

      if (element instanceof MElementContainer<?>) {
        MElementContainer<MUIElement> container = (MElementContainer<MUIElement>) element;
        MUIElement selectedElement = container.getSelectedElement();
        List<MUIElement> children = container.getChildren();
        for (MUIElement child : children) {
          // remove stuff in the "back" first
          if (child != selectedElement) {
            removeGui(child);
          }
        }

        if (selectedElement != null && children.contains(selectedElement)) {
          // now remove the selected element
          removeGui(selectedElement);
        }
      }

      if (element instanceof MPerspective) {
        MPerspective perspective = (MPerspective) element;
        for (MWindow subWindow : perspective.getWindows()) {
          removeGui(subWindow);
        }
      } else if (element instanceof MWindow) {
        MWindow window = (MWindow) element;
        for (MWindow subWindow : window.getWindows()) {
          removeGui(subWindow);
        }

        if (window instanceof MTrimmedWindow) {
          MTrimmedWindow trimmedWindow = (MTrimmedWindow) window;
          for (MUIElement trimBar : trimmedWindow.getTrimBars()) {
            removeGui(trimBar);
          }
        }
      }

      if (element instanceof MContribution) {
        MContribution contribution = (MContribution) element;
        Object client = contribution.getObject();
        IEclipseContext parentContext = renderer.getContext(element);
        if (parentContext != null && client != null) {
          try {
            ContextInjectionFactory.invoke(client, PersistState.class, parentContext, null);
          } catch (Exception e) {
            if (logger != null) {
              logger.error(e);
            }
          }
        }
      }

      renderer.disposeWidget(element);

      // unset the client object
      if (element instanceof MContribution) {
        MContribution contribution = (MContribution) element;
        Object client = contribution.getObject();
        IEclipseContext parentContext = renderer.getContext(element);
        if (parentContext != null && client != null) {
          try {
            ContextInjectionFactory.uninject(client, parentContext);
          } catch (Exception e) {
            if (logger != null) {
              logger.error(e);
            }
          }
        }
        contribution.setObject(null);
      }

      // dispose the context
      if (element instanceof MContext) {
        clearContext((MContext) element);
      }
    }

    if (removeRoot == element) removeRoot = null;
  }