@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.workbench.renderers.swt.SWTPartRenderer#processContents
   * (org.eclipse.e4.ui.model.application.ui.MElementContainer)
   */
  @Override
  public void processContents(MElementContainer<MUIElement> container) {
    // I can either simply stop processing, or we can walk the model
    // ourselves like the "old" days
    // EMF gives us null lists if empty
    if (container == null) return;

    Object obj = container;
    ToolBarManager parentManager = getManager((MToolBar) obj);
    if (parentManager == null) {
      return;
    }
    // Process any contents of the newly created ME
    List<MUIElement> parts = container.getChildren();
    if (parts != null) {
      MUIElement[] plist = parts.toArray(new MUIElement[parts.size()]);
      for (int i = 0; i < plist.length; i++) {
        MUIElement childME = plist[i];
        modelProcessSwitch(parentManager, (MToolBarElement) childME);
      }
    }
    parentManager.update(true);

    ToolBar tb = getToolbarFrom(container.getWidget());
    if (tb != null) {
      tb.pack(true);
      tb.getShell().layout(new Control[] {tb}, SWT.DEFER);
    }
  }
  private MElementContainer<? extends MUIElement> getLastContainer() {
    MElementContainer<? extends MUIElement> searchRoot = getContainer();
    @SuppressWarnings("unchecked")
    List<MUIElement> children = (List<MUIElement>) searchRoot.getChildren();
    if (children.size() == 0) {
      MPartStack stack = modelService.createModelElement(MPartStack.class);
      children.add(stack);
      return stack;
    }

    MElementContainer<?> lastContainer = getLastContainer(searchRoot, children);
    if (lastContainer instanceof MPartStack) {
      return lastContainer;
    }

    // No stacks found make one and add it
    MPartStack stack = modelService.createModelElement(MPartStack.class);
    stack.setElementId("CreatedByGetLastContainer"); // $NON-NLS-1$
    if (children.get(0) instanceof MPartSashContainer) {
      MPartSashContainer psc = (MPartSashContainer) children.get(0);
      psc.getChildren().add(stack);
    } else {
      // We need a sash so 'insert' the new stack
      modelService.insert(
          stack, (MPartSashContainerElement) children.get(0), EModelService.RIGHT_OF, 0.5f);
    }
    return stack;
  }
  @Override
  public int countRenderableChildren(MUIElement element) {
    if (!(element instanceof MElementContainer<?>)) {
      return 0;
    }

    @SuppressWarnings("unchecked")
    MElementContainer<MUIElement> container = (MElementContainer<MUIElement>) element;
    int count = 0;
    List<MUIElement> kids = container.getChildren();
    for (MUIElement kid : kids) {
      if (kid.isToBeRendered()) {
        count++;
      }
    }

    if (element instanceof MPerspective) {
      MPerspective perspective = (MPerspective) element;
      for (MWindow window : perspective.getWindows()) {
        if (window.isToBeRendered()) {
          count++;
        }
      }
    }
    return count;
  }
  private void combine(
      MPartSashContainerElement toInsert,
      MPartSashContainerElement relTo,
      MPartSashContainer newSash,
      boolean newFirst,
      float ratio) {
    MElementContainer<MUIElement> curParent = relTo.getParent();
    if (curParent == null) {
      // if relTo is a shared element, use its current placeholder
      MWindow win = getTopLevelWindowFor(relTo);
      relTo = findPlaceholderFor(win, relTo);
      curParent = relTo.getParent();
    }
    Assert.isLegal(relTo != null && curParent != null);
    int index = curParent.getChildren().indexOf(relTo);
    curParent.getChildren().remove(relTo);
    if (newFirst) {
      newSash.getChildren().add(toInsert);
      newSash.getChildren().add(relTo);
    } else {
      newSash.getChildren().add(relTo);
      newSash.getChildren().add(toInsert);
    }

    // Set up the container data before adding the new sash to the model
    // To raise the granularity assume 100% == 10,000
    int adjustedPct = (int) (ratio * 10000);
    toInsert.setContainerData(Integer.toString(adjustedPct));
    relTo.setContainerData(Integer.toString(10000 - adjustedPct));

    // add the new sash at the same location
    curParent.getChildren().add(index, newSash);
  }
  private static int getIndex(MElementContainer<?> menuModel, String positionInParent) {
    String id = null;
    String modifier = null;
    if (positionInParent != null && positionInParent.length() > 0) {
      String[] array = positionInParent.split("="); // $NON-NLS-1$
      modifier = array[0];
      // may have an invalid position, check for this
      if (array.length > 1) {
        id = array[1];
      }
    }
    if (id == null) {
      return menuModel.getChildren().size();
    }

    int idx = 0;
    int size = menuModel.getChildren().size();
    while (idx < size) {
      if (id.equals(menuModel.getChildren().get(idx).getElementId())) {
        if ("after".equals(modifier)) { // $NON-NLS-1$
          idx++;
        }
        return idx;
      }
      idx++;
    }
    return id.equals("additions") ? menuModel.getChildren().size() : -1; // $NON-NLS-1$
  }
Пример #7
0
  // -------------------------------------------------------
  // -------------------------------------------------------
  private void hideElementRecursive(MUIElement element) {
    if (element == null || element.getWidget() == null) return;

    if (element instanceof MPlaceholder) {
      MPlaceholder ph = (MPlaceholder) element;
      element = ph.getRef();
    }

    // Hide any floating windows
    if (element instanceof MWindow && element.getWidget() != null) {
      element.setVisible(false);
    }

    if (element instanceof MElementContainer<?>) {
      MElementContainer<?> container = (MElementContainer<?>) element;
      for (MUIElement childElement : container.getChildren()) {
        hideElementRecursive(childElement);
      }

      // OK, now process detached windows
      if (element instanceof MWindow) {
        for (MWindow w : ((MWindow) element).getWindows()) {
          hideElementRecursive(w);
        }
      } else if (element instanceof MPerspective) {
        for (MWindow w : ((MPerspective) element).getWindows()) {
          hideElementRecursive(w);
        }
      }
    }
  }
  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;
  }
Пример #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();
  }
Пример #10
0
  private void exchange_editor_with_selected_in_new_stack(
      MPart activeEditor,
      MElementContainer<MUIElement> oldEditorStack,
      MElementContainer<MUIElement> newStack) {
    int activeEditorIndex = oldEditorStack.getChildren().indexOf(activeEditor);
    MUIElement exchangedEditor = newStack.getSelectedElement();
    int exchangedEditorIndex = newStack.getChildren().indexOf(exchangedEditor);

    oldEditorStack.getChildren().add(activeEditorIndex, exchangedEditor);
    newStack.getChildren().add(exchangedEditorIndex, activeEditor);
  }
 /** @param parent */
 private void setStackVisibility(MElementContainer<MUIElement> parent) {
   for (MUIElement child : parent.getChildren()) {
     if (child.isToBeRendered() && child.isVisible()) {
       parent.setToBeRendered(true);
       return;
     }
   }
   parent.setToBeRendered(false);
   // continue modifying the visibility as the parent's parent may also
   // need to be hidden from the user
   setStackVisibility(parent.getParent());
 }
 private MElementContainer<MUIElement> findContainer(
     MElementContainer<MUIElement> container, MUIElement element) {
   for (MUIElement child : container.getChildren()) {
     if (child == element) {
       return container;
     } else if (child instanceof MPlaceholder) {
       MPlaceholder placeholder = (MPlaceholder) child;
       MUIElement ref = placeholder.getRef();
       if (ref == element) {
         return container;
       } else if (ref instanceof MElementContainer<?>) {
         @SuppressWarnings("unchecked")
         MElementContainer<MUIElement> ref2 = (MElementContainer<MUIElement>) ref;
         MElementContainer<MUIElement> match = findContainer(ref2, element);
         if (match != null) {
           return match;
         }
       }
     } else if (child instanceof MElementContainer<?>) {
       @SuppressWarnings("unchecked")
       MElementContainer<MUIElement> child2 = (MElementContainer<MUIElement>) child;
       MElementContainer<MUIElement> match = findContainer(child2, element);
       if (match != null) {
         return match;
       }
     }
   }
   return null;
 }
  @Override
  public void processContents(MElementContainer<MUIElement> element) {
    if (!(((MUIElement) element) instanceof MWindow)) {
      return;
    }
    MWindow mWindow = (MWindow) ((MUIElement) element);
    Shell shell = (Shell) element.getWidget();

    // Populate the main menu
    IPresentationEngine2 renderer =
        (IPresentationEngine2) context.get(IPresentationEngine.class.getName());
    if (mWindow.getMainMenu() != null) {
      renderer.createGui(mWindow.getMainMenu(), element);
      shell.setMenuBar((Menu) mWindow.getMainMenu().getWidget());
    }

    // create Detached Windows
    for (MWindow dw : mWindow.getWindows()) {
      renderer.createGui(dw, element);
    }

    // Populate the trim (if any)
    if (mWindow instanceof MTrimmedWindow) {
      MTrimmedWindow tWindow = (MTrimmedWindow) mWindow;
      for (MTrimBar trimBar : tWindow.getTrimBars()) {
        renderer.createGui(trimBar, element);
      }
    }

    shell.pack();
    shell.open();
  }
 private void addToManager(
     MenuManager parentManager, MMenuElement model, IContributionItem menuManager) {
   MElementContainer<MUIElement> parent = model.getParent();
   // technically this shouldn't happen
   if (parent == null) {
     parentManager.add(menuManager);
   } else {
     int index = parent.getChildren().indexOf(model);
     // shouldn't be -1, but better safe than sorry
     if (index > parentManager.getSize() || index == -1) {
       parentManager.add(menuManager);
     } else {
       parentManager.insert(index, menuManager);
     }
   }
 }
Пример #15
0
  public static int getPositionIndex(
      String positionInList, MElementContainer<MUIElement> container) {
    if (positionInList == null || positionInList.trim().length() == 0) {
      return -1;
    }
    PositionInfo posInfo = PositionInfo.parse(positionInList);
    if (posInfo == null) {
      return -1;
    }
    switch (posInfo.getPosition()) {
      case FIRST:
        return 0;

      case INDEX:
        return posInfo.getPositionReferenceAsInteger();

      case BEFORE:
      case AFTER:
        String elementId = posInfo.getPositionReference();
        List<MUIElement> siblings = container.getChildren();
        for (int i = 0; i < siblings.size(); i++) {
          MUIElement sibling = siblings.get(i);
          if (elementId.equals(sibling.getElementId())) {
            return posInfo.getPosition() == Position.BEFORE ? i : i + 1;
          }
        }

      case LAST:
      default:
        return -1;
    }
  }
  @Override
  public void processContents(MElementContainer<MUIElement> container) {
    IPresentationEngine renderer =
        (IPresentationEngine) context.get(IPresentationEngine.class.getName());
    ToolBar toolbar = (ToolBar) container.getWidget();

    boolean isFirst = true;
    for (MUIElement element : container.getChildren()) {
      Node node = (Node) renderer.createGui(element);
      if (node != null) {
        if (!isFirst) {
          toolbar.getItems().add(new Separator());
        }
        toolbar.getItems().add(node);
        isFirst = false;
      }
    }
  }
 @Override
 public int toBeRenderedCount(MElementContainer<?> container) {
   int count = 0;
   for (MUIElement child : container.getChildren()) {
     if (child.isToBeRendered()) {
       count++;
     }
   }
   return count;
 }
  private MElementContainer<? extends MUIElement> getLastContainer(
      MElementContainer<?> container, List<?> children) {
    if (children.isEmpty()) {
      return null;
    }

    for (int i = children.size() - 1; i > -1; i--) {
      Object muiElement = children.get(i);
      if (muiElement instanceof MElementContainer<?>) {
        MElementContainer<?> childContainer = (MElementContainer<?>) muiElement;
        MElementContainer<?> lastContainer =
            getLastContainer(childContainer, childContainer.getChildren());
        if (lastContainer != null) {
          return lastContainer;
        }
      }
    }
    return container;
  }
  private void addToLastContainer(String category, MPart part) {
    // OK, we haven't found an explicit placeholder;
    // If this is a multi-instance view see if there's a 'global' placeholder
    String partId = part.getElementId();
    int colonIndex = partId == null ? -1 : partId.indexOf(':');
    if (colonIndex >= 0) {
      String descId = part.getElementId().substring(0, colonIndex);
      descId += ":*"; // $NON-NLS-1$
      List<MPlaceholder> phList =
          modelService.findElements(
              workbenchWindow, descId, MPlaceholder.class, null, EModelService.PRESENTATION);
      if (phList.size() > 0) {
        MUIElement phParent = phList.get(0).getParent();
        if (phParent instanceof MPartStack) {
          MPartStack theStack = (MPartStack) phParent;
          int phIndex = theStack.getChildren().indexOf(phList.get(0));
          adjustPlaceholder(part);
          MPlaceholder placeholder = part.getCurSharedRef();
          if (placeholder == null) {
            theStack.getChildren().add(phIndex, part);
          } else {
            theStack.getChildren().add(phIndex, placeholder);
          }
          return;
        }
      }
    }

    @SuppressWarnings("unchecked")
    MElementContainer<MUIElement> lastContainer =
        (MElementContainer<MUIElement>) getLastContainer();
    MPlaceholder placeholder = part.getCurSharedRef();
    if (placeholder == null) {
      lastContainer.getChildren().add(part);
    } else {
      lastContainer.getChildren().add(placeholder);
    }

    if (category != null) {
      lastContainer.getTags().add(category);
    }
  }
 /**
  * Records the specified parent 's selected element in the activation history.
  *
  * @param parent the element whose selected element should be checked for activation history
  *     recording
  */
 private void recordSelectedActivation(MElementContainer<? extends MUIElement> parent) {
   MUIElement selectedElement = parent.getSelectedElement();
   if (selectedElement instanceof MPart) {
     partActivationHistory.append((MPart) selectedElement);
   } else if (selectedElement instanceof MPlaceholder) {
     MUIElement ref = ((MPlaceholder) selectedElement).getRef();
     if (ref instanceof MPart) {
       partActivationHistory.append((MPart) ref);
     }
   }
 }
  @Override
  public <T extends MUIElement> void move(
      T element, MElementContainer<? super T> newParent, int index, boolean leavePlaceholder) {
    // Cache where we were
    MElementContainer<MUIElement> curParent = element.getParent();
    int curIndex = curParent.getChildren().indexOf(element);

    // Move the model element
    if (index == -1) {
      newParent.getChildren().add(element);
    } else {
      newParent.getChildren().add(index, element);
    }

    if (leavePlaceholder) {
      MPlaceholder ph = MAdvancedFactory.INSTANCE.createPlaceholder();
      ph.setRef(element);
      curParent.getChildren().add(curIndex, ph);
    }
  }
  /**
   * "Container" here is: 1) a selected MPerspective, or, if none available 2) the MWindow for which
   * this part service is created, or, if not available, 3) the MApplication.
   */
  private MElementContainer<? extends MUIElement> getContainer() {
    MElementContainer<? extends MUIElement> outerContainer =
        (workbenchWindow != null) ? workbenchWindow : application;

    // see if we can narrow it down to the active perspective
    for (MElementContainer<? extends MUIElement> container = outerContainer; container != null; ) {
      if (container instanceof MPerspective) {
        return container;
      }
      Object child = container.getSelectedElement();
      if (child == null) {
        break;
      }
      if (child instanceof MElementContainer<?>) {
        container = (MElementContainer<?>) child;
      } else {
        break;
      }
    }
    return outerContainer;
  }
  @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 static int getIndex(MElementContainer<?> model, String positionInParent) {
    String id = null;
    String modifier = null;
    if (positionInParent != null && positionInParent.length() > 0) {
      String[] array = positionInParent.split("="); // $NON-NLS-1$
      modifier = array[0];
      id = array[1];
    }
    if (id == null) {
      return model.getChildren().size();
    }

    int idx = 0;
    int size = model.getChildren().size();
    while (idx < size) {
      if (id.equals(model.getChildren().get(idx).getElementId())) {
        if ("after".equals(modifier)) { // $NON-NLS-1$
          idx++;
        } else if ("endof".equals(modifier)) { // $NON-NLS-1$
          // Skip current menu item
          idx++;

          // Skip all menu items until next MenuSeparator is found
          while (idx < size
              && !(model.getChildren().get(idx) instanceof MToolBarSeparator
                  && model.getChildren().get(idx).getElementId() != null)) {
            idx++;
          }
        }
        return idx;
      }
      idx++;
    }
    return id.equals("additions") ? model.getChildren().size() : -1; // $NON-NLS-1$
  }
Пример #25
0
 public static void addPartToAppropriateContainer(
     MPart part, MPartDescriptor descriptor, MApplication application, EModelService service) {
   // first try and find the container specified by the id in the "parent" persisted state
   MElementContainer<MUIElement> container = null;
   String parentId = descriptor.getPersistedState().get(PARENT_ID);
   if (parentId != null) {
     @SuppressWarnings("rawtypes")
     List<MElementContainer> containers =
         service.findElements(application, parentId, MElementContainer.class, null);
     if (!containers.isEmpty()) {
       @SuppressWarnings("unchecked")
       MElementContainer<MUIElement> uncheckedContainer = containers.get(0);
       container = uncheckedContainer;
     }
   }
   // next try and find a sibling, and use the sibling's container
   if (container == null) {
     List<MPart> siblings =
         service.findElements(application, descriptor.getElementId(), MPart.class, null);
     for (MPart sibling : siblings) {
       if (sibling == part) {
         continue;
       }
       container = sibling.getParent();
       if (sibling.isToBeRendered()) {
         // prefer visible siblings
         break;
       }
     }
   }
   if (container != null) {
     String position = descriptor.getPersistedState().get(POSITION_ID);
     int index = getPositionIndex(position, container);
     if (index < 0 || index > container.getChildren().size()) {
       container.getChildren().add(part);
     } else {
       container.getChildren().add(index, part);
     }
   }
 }
 public static int indexForId(MElementContainer<MMenuElement> parentMenu, String id) {
   if (id == null || id.length() == 0) {
     return -1;
   }
   int i = 0;
   for (MMenuElement item : parentMenu.getChildren()) {
     if (id.equals(item.getElementId())) {
       return i;
     }
     i++;
   }
   return -1;
 }
Пример #27
0
 private void disconnectReferencedElementsFromPerspectiveWidgets(
     MElementContainer<? extends MUIElement> container) {
   for (MUIElement e : container.getChildren()) {
     if (e instanceof MPlaceholder) {
       MPlaceholder ph = (MPlaceholder) e;
       if (ph.isToBeRendered()) {
         ComponentContainer phComponent = (ComponentContainer) ph.getWidget();
         Component refComponent = (Component) ph.getRef().getWidget();
         phComponent.removeComponent(refComponent);
       }
     }
   }
 }
        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 hideElementRecursive(MUIElement element) {
    if (element == null || element.getWidget() == null) {
      return;
    }

    if (element instanceof MPlaceholder) {
      MPlaceholder ph = (MPlaceholder) element;
      element = ph.getRef();
    }

    // Hide any floating windows
    if (element instanceof MWindow && element.getWidget() != null) {
      element.setVisible(false);
    }

    if (element instanceof MGenericStack<?>) {
      // For stacks only the currently selected elements are being hidden
      MGenericStack<?> container = (MGenericStack<?>) element;
      MUIElement curSel = container.getSelectedElement();
      hideElementRecursive(curSel);
    } else if (element instanceof MElementContainer<?>) {
      MElementContainer<?> container = (MElementContainer<?>) element;
      for (MUIElement childElement : container.getChildren()) {
        hideElementRecursive(childElement);
      }

      // OK, now process detached windows
      if (element instanceof MWindow) {
        for (MWindow w : ((MWindow) element).getWindows()) {
          hideElementRecursive(w);
        }
      } else if (element instanceof MPerspective) {
        for (MWindow w : ((MPerspective) element).getWindows()) {
          hideElementRecursive(w);
        }
      }
    }
  }
  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);
      }
    }
  }