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);
    }
  }
        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);
          }
        }
 private void replacePlaceholder(MPlaceholder ph) {
   MPart part = createModelElement(MPart.class);
   part.setElementId(ph.getElementId());
   part.getTransientData()
       .put(
           IPresentationEngine.OVERRIDE_ICON_IMAGE_KEY,
           ImageDescriptor.getMissingImageDescriptor().createImage());
   String label = (String) ph.getTransientData().get(TAG_LABEL);
   if (label != null) {
     part.setLabel(label);
   } else {
     part.setLabel(getLabel(ph.getElementId()));
   }
   part.setContributionURI(COMPATIBILITY_VIEW_URI);
   part.setCloseable(true);
   MElementContainer<MUIElement> curParent = ph.getParent();
   int curIndex = curParent.getChildren().indexOf(ph);
   curParent.getChildren().remove(curIndex);
   curParent.getChildren().add(curIndex, part);
   if (curParent.getSelectedElement() == ph) {
     curParent.setSelectedElement(part);
   }
 }
  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 void hidePart(MPart part, boolean force) {
    if (isInContainer(part)) {
      MPlaceholder sharedRef = part.getCurSharedRef();
      MUIElement toBeRemoved = getRemoveTarget(part);
      MElementContainer<MUIElement> parent = getParent(toBeRemoved);
      List<MUIElement> children = parent.getChildren();

      // check if we're a placeholder but not actually the shared ref of the part
      if (toBeRemoved != part && toBeRemoved instanceof MPlaceholder && sharedRef != toBeRemoved) {
        toBeRemoved.setToBeRendered(false);

        // if so, not much to do, remove ourselves if necessary but that's it
        if (force || part.getTags().contains(REMOVE_ON_HIDE_TAG)) {
          parent.getChildren().remove(toBeRemoved);
        }
        return;
      }

      boolean isActiveChild = isActiveChild(part);
      MPart activationCandidate = null;
      // check if we're the active child
      if (isActiveChild) {
        // get the activation candidate if we are
        activationCandidate = partActivationHistory.getNextActivationCandidate(getParts(), part);
      }

      MPerspective thePersp = modelService.getPerspectiveFor(toBeRemoved);
      boolean needNewSel =
          thePersp == null || !thePersp.getTags().contains("PerspClosing"); // $NON-NLS-1$
      if (needNewSel) {
        if (parent.getSelectedElement() == toBeRemoved) {
          // if we're the selected element and we're going to be hidden, need to select
          // something else
          MUIElement candidate = partActivationHistory.getSiblingSelectionCandidate(part);
          candidate =
              candidate == null
                  ? null
                  : candidate.getCurSharedRef() == null ? candidate : candidate.getCurSharedRef();
          if (candidate != null && children.contains(candidate)) {
            parent.setSelectedElement(candidate);
          } else {
            for (MUIElement child : children) {
              if (child != toBeRemoved && child.isToBeRendered()) {
                parent.setSelectedElement(child);
                break;
              }
            }
          }
        }

        if (activationCandidate == null) {
          // nothing else to activate and we're the active child, deactivate
          if (isActiveChild) {
            part.getContext().deactivate();
          }
        } else {
          // activate our candidate
          activate(activationCandidate);
        }
      }

      if (toBeRemoved != null) {
        toBeRemoved.setToBeRendered(false);
      } else {
        part.setToBeRendered(false);
      }

      if (parent.getSelectedElement() == toBeRemoved) {
        parent.setSelectedElement(null);
      }

      if (force || part.getTags().contains(REMOVE_ON_HIDE_TAG)) {
        children.remove(toBeRemoved);
      }
      // remove ourselves from the activation history also since we're being hidden
      partActivationHistory.forget(getWindow(), part, toBeRemoved == part);
    }
  }
        public void handleEvent(Event event) {

          Object changedObj = event.getProperty(UIEvents.EventTags.ELEMENT);
          if (!(changedObj instanceof MElementContainer<?>)) return;

          MElementContainer<MUIElement> changedElement = (MElementContainer<MUIElement>) changedObj;
          boolean isApplication = changedObj instanceof MApplication;

          boolean menuChild = changedObj instanceof MMenu;
          // If the parent isn't in the UI then who cares?
          AbstractPartRenderer renderer = getRendererFor(changedElement);
          if ((!isApplication && renderer == null) || menuChild) return;

          String eventType = (String) event.getProperty(UIEvents.EventTags.TYPE);
          if (UIEvents.EventTypes.ADD.equals(eventType)) {
            Activator.trace(Policy.DEBUG_RENDERER, "Child Added", null); // $NON-NLS-1$
            MUIElement added = (MUIElement) event.getProperty(UIEvents.EventTags.NEW_VALUE);

            // OK, we have a new -visible- part we either have to create
            // it or host it under the correct parent. Note that we
            // explicitly do *not* render non-selected elements in
            // stacks (to support lazy loading).
            boolean isStack = changedObj instanceof MGenericStack<?>;
            boolean hasWidget = added.getWidget() != null;
            boolean isSelected = added == changedElement.getSelectedElement();
            boolean renderIt = !isStack || hasWidget || isSelected;
            if (renderIt) {
              // NOTE: createGui will call 'childAdded' if successful
              Object w = createGui(added);
              if (w instanceof Control && !(w instanceof Shell)) {
                final Control ctrl = (Control) w;
                fixZOrder(added);
                if (!ctrl.isDisposed()) {
                  ctrl.getShell().layout(new Control[] {ctrl}, SWT.DEFER);
                }
              }
            } else {
              if (renderer != null && added.isToBeRendered())
                renderer.childRendered(changedElement, added);
            }

            // If the element being added is a placeholder, check to see if
            // it's 'globally visible' and, if so, remove all other
            // 'local' placeholders referencing the same element.
            int newLocation = modelService.getElementLocation(added);
            if (newLocation == EModelService.IN_SHARED_AREA
                || newLocation == EModelService.OUTSIDE_PERSPECTIVE) {
              MWindow topWin = modelService.getTopLevelWindowFor(added);
              modelService.hideLocalPlaceholders(topWin, null);
            }
          } else if (UIEvents.EventTypes.REMOVE.equals(eventType)) {
            Activator.trace(Policy.DEBUG_RENDERER, "Child Removed", null); // $NON-NLS-1$
            MUIElement removed = (MUIElement) event.getProperty(UIEvents.EventTags.OLD_VALUE);
            // Removing invisible elements is a NO-OP as far as the
            // renderer is concerned
            if (!removed.isToBeRendered()) return;

            if (removed.getWidget() instanceof Control) {
              Control ctrl = (Control) removed.getWidget();
              ctrl.setLayoutData(null);
              ctrl.getParent().layout(new Control[] {ctrl}, SWT.CHANGED | SWT.DEFER);
            }

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

            if (renderer != null) renderer.hideChild(changedElement, removed);
          }
        }