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;
  }
  private void resetPerspectiveModel(
      MPerspective persp, MWindow window, boolean removeSharedPlaceholders) {
    if (persp == null) {
      return;
    }

    if (removeSharedPlaceholders) {
      // Remove any views (Placeholders) from the shared area
      EPartService ps = window.getContext().get(EPartService.class);
      List<MArea> areas = findElements(window, null, MArea.class, null);
      if (areas.size() == 1) {
        MArea area = areas.get(0);

        // Strip out the placeholders in visible stacks
        List<MPlaceholder> phList = findElements(area, null, MPlaceholder.class, null);
        for (MPlaceholder ph : phList) {
          ps.hidePart((MPart) ph.getRef());
          ph.getParent().getChildren().remove(ph);
        }

        // Prevent shared stacks ids from clashing with the ones in the perspective
        List<MPartStack> stacks = findElements(area, null, MPartStack.class, null);
        for (MPartStack stack : stacks) {
          String generatedId = "PartStack@" + Integer.toHexString(stack.hashCode()); // $NON-NLS-1$
          stack.setElementId(generatedId);
        }

        // Also remove any min/max tags on the area (or its placeholder)
        MUIElement areaPresentation = area;
        if (area.getCurSharedRef() != null) {
          areaPresentation = area.getCurSharedRef();
        }

        areaPresentation.getTags().remove(IPresentationEngine.MAXIMIZED);
        areaPresentation.getTags().remove(IPresentationEngine.MINIMIZED);
        areaPresentation.getTags().remove(IPresentationEngine.MINIMIZED_BY_ZOOM);
      }
    }

    // Remove any minimized stacks for this perspective
    List<MTrimBar> bars = findElements(window, null, MTrimBar.class, null);
    List<MToolControl> toRemove = new ArrayList<>();
    for (MTrimBar bar : bars) {
      for (MUIElement barKid : bar.getChildren()) {
        if (!(barKid instanceof MToolControl)) {
          continue;
        }
        String id = barKid.getElementId();
        if (id != null && id.contains(persp.getElementId())) {
          toRemove.add((MToolControl) barKid);
        }
      }
    }

    for (MToolControl toolControl : toRemove) {
      // Close any open fast view
      toolControl.setToBeRendered(false);
      toolControl.getParent().getChildren().remove(toolControl);
    }
  }
  @Override
  public void hideLocalPlaceholders(MWindow window, MPerspective perspective) {
    List<MPlaceholder> globals =
        findElements(window, null, MPlaceholder.class, null, OUTSIDE_PERSPECTIVE | IN_SHARED_AREA);

    // Iterate across the perspective(s) removing any 'local' placeholders
    List<MPerspective> persps = new ArrayList<>();
    if (perspective != null) {
      persps.add(perspective);
    } else {
      persps = findElements(window, null, MPerspective.class, null);
    }

    for (MPerspective persp : persps) {
      List<MPlaceholder> locals =
          findElements(persp, null, MPlaceholder.class, null, IN_ANY_PERSPECTIVE);
      for (MPlaceholder local : locals) {
        for (MPlaceholder global : globals) {
          if (global.getRef() == local.getRef()) {
            local.setToBeRendered(false);
            MElementContainer<MUIElement> localParent = local.getParent();
            setStackVisibility(localParent);
          }
        }
      }
    }
  }
  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);
  }
 /**
  * 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 adjustPlaceholder(MPart part) {
   if (isShared(part)) {
     MPlaceholder placeholder = part.getCurSharedRef();
     // if this part doesn't have any placeholders, we need to make one
     if (placeholder == null
         // alternatively, if it has one but it's not in the current container, then we
         // need to spawn another one as we don't want to reuse the same one and end up
         // shifting that placeholder to the current container during the add operation
         || (placeholder.getParent() != null && !isInContainer(placeholder))) {
       placeholder = createSharedPart(part);
       part.setCurSharedRef(placeholder);
     }
   }
 }
 /**
  * Records the specified parent part's selected element in the activation history if the parent is
  * a stack.
  *
  * @param part the part whose parent's selected element should be checked for activation history
  *     recording
  */
 private void recordStackActivation(MPart part) {
   MElementContainer<? extends MUIElement> parent = part.getParent();
   if (parent instanceof MGenericStack) {
     recordSelectedActivation(parent);
   } else if (parent == null) {
     MPlaceholder placeholder = part.getCurSharedRef();
     if (placeholder != null) {
       parent = placeholder.getParent();
       if (parent instanceof MGenericStack) {
         recordSelectedActivation(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 void handleEvent(Event event) {
          // no need to do anything if we have no listeners
          if (!listeners.isEmpty()) {
            Object oldSelected = event.getProperty(UIEvents.EventTags.OLD_VALUE);
            if (oldSelected instanceof MPlaceholder) {
              oldSelected = ((MPlaceholder) oldSelected).getRef();
            }

            MPlaceholder placeholder = null;
            Object selected = event.getProperty(UIEvents.EventTags.NEW_VALUE);
            if (selected instanceof MPlaceholder) {
              placeholder = (MPlaceholder) selected;
              selected = placeholder.getRef();
            }

            MPart oldSelectedPart = oldSelected instanceof MPart ? (MPart) oldSelected : null;
            MPart selectedPart = selected instanceof MPart ? (MPart) selected : null;

            if (oldSelectedPart != null && getParts().contains(selectedPart)) {
              firePartHidden(oldSelectedPart);
            }

            if (selectedPart != null
                && selectedPart.isToBeRendered()
                && getParts().contains(selectedPart)) {
              // ask the renderer to create this part
              if (placeholder == null) {
                if (selectedPart.getParent().getRenderer() != null) {
                  engine.createGui(selectedPart);
                  firePartVisible(selectedPart);
                  firePartBroughtToTop(selectedPart);
                }
              } else if (placeholder.getParent().getRenderer() != null) {
                engine.createGui(placeholder);
                firePartVisible(selectedPart);
                firePartBroughtToTop(selectedPart);
              }
            }
          }
        }
  @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);
      }
    }
  }
 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);
   }
 }