@Override
  public MPlaceholder createSharedPart(String id, boolean force) {
    MWindow sharedWindow = getWindow();
    // Do we already have the part to share?
    MPart sharedPart = null;

    // check for existing parts if necessary
    if (!force) {
      for (MUIElement element : sharedWindow.getSharedElements()) {
        if (element.getElementId().equals(id)) {
          sharedPart = (MPart) element;
          break;
        }
      }
    }

    if (sharedPart == null) {
      MPartDescriptor descriptor = modelService.getPartDescriptor(id);
      sharedPart = createPart(descriptor);
      if (sharedPart == null) {
        return null;
      }

      // Replace the id to ensure that multi-instance parts work correctly
      sharedPart.setElementId(id);

      sharedWindow.getSharedElements().add(sharedPart);
    }

    return createSharedPart(sharedPart);
  }
  @Override
  public void hostElement(
      MUIElement element, MWindow hostWindow, Object uiContainer, IEclipseContext hostContext) {
    // This is subtle; unless the element is hooked into the model it won't fire events
    hostWindow.getSharedElements().add(element);
    element.getTags().add(HOSTED_ELEMENT);

    IPresentationEngine renderer = hostWindow.getContext().get(IPresentationEngine.class);
    renderer.createGui(element, uiContainer, hostContext);
  }
  @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);
  }
        @Override
        public void handleEvent(Event event) {
          final MUIElement changedElement = (MUIElement) event.getProperty(EventTags.ELEMENT);
          if (!changedElement.getTags().contains(HOSTED_ELEMENT)) {
            return;
          }

          if (changedElement.getWidget() != null) {
            return;
          }

          EObject eObj = (EObject) changedElement;
          if (!(eObj.eContainer() instanceof MWindow)) {
            return;
          }

          MWindow hostingWindow = (MWindow) eObj.eContainer();
          hostingWindow.getSharedElements().remove(changedElement);
          changedElement.getTags().remove(HOSTED_ELEMENT);
        }
  @Override
  public boolean isPartOrPlaceholderInPerspective(String elementId, MPerspective perspective) {
    List<MPart> findElements = modelService.findElements(perspective, elementId, MPart.class, null);
    if (!findElements.isEmpty()) {
      MPart part = findElements.get(0);

      // if that is a shared part, check the placeholders
      if (workbenchWindow.getSharedElements().contains(part)) {
        List<MPlaceholder> placeholders =
            modelService.findElements(perspective, elementId, MPlaceholder.class, null);
        for (MPlaceholder mPlaceholder : placeholders) {
          if (mPlaceholder.isVisible() && mPlaceholder.isToBeRendered()) {
            return true;
          }
        }
        return false;
      }
      // not a shared part
      return part.isVisible() && part.isToBeRendered();
    }
    return false;
  }