@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;
  }
  @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();
  }
 @Override
 public int toBeRenderedCount(MElementContainer<?> container) {
   int count = 0;
   for (MUIElement child : container.getChildren()) {
     if (child.isToBeRendered()) {
       count++;
     }
   }
   return count;
 }
 /** @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());
 }
Exemplo n.º 5
0
  @Override
  public Image getImage(Object element, Display display) {
    if (element instanceof MUIElement) {
      MUIElement uiElement = (MUIElement) element;
      if (uiElement.isToBeRendered() && uiElement.isVisible()) {
        return createImage(ResourceProvider.IMG_WizardDialog);
      } else {
        return createImage(ResourceProvider.IMG_Tbr_WizardDialog);
      }
    }

    return null;
  }
  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);
      }
    }
  }
        public void handleEvent(Event event) {
          Object changedObj = event.getProperty(UIEvents.EventTags.ELEMENT);
          if (!(changedObj instanceof MTrimmedWindow)) return;

          MTrimmedWindow window = (MTrimmedWindow) changedObj;
          if (window.getWidget() == null) return;

          String eventType = (String) event.getProperty(UIEvents.EventTags.TYPE);
          if (UIEvents.EventTypes.ADD.equals(eventType)) {
            MUIElement added = (MUIElement) event.getProperty(UIEvents.EventTags.NEW_VALUE);
            if (added.isToBeRendered()) createGui(added, window.getWidget(), window.getContext());
          } else if (UIEvents.EventTypes.REMOVE.equals(eventType)) {
            MUIElement removed = (MUIElement) event.getProperty(UIEvents.EventTags.OLD_VALUE);
            if (removed.getRenderer() != null) removeGui(removed);
          }
        }
        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 void bringToTop(MUIElement element) {
    if (element instanceof MApplication) {
      return;
    }

    MWindow window = getTopLevelWindowFor(element);
    if (window == element) {
      if (!element.isToBeRendered()) {
        element.setToBeRendered(true);
      }

      window.getParent().setSelectedElement(window);
    } else {
      showElementInWindow(window, element);
    }
    UIEvents.publishEvent(UIEvents.UILifeCycle.BRINGTOTOP, 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);
          }
        }
        public void handleEvent(Event event) {
          if (psTB.isDisposed()) {
            return;
          }

          MUIElement changedElement = (MUIElement) event.getProperty(UIEvents.EventTags.ELEMENT);

          if (psME == null || !(changedElement instanceof MPerspective)) return;

          MWindow perspWin = modelService.getTopLevelWindowFor(changedElement);
          MWindow switcherWin = modelService.getTopLevelWindowFor(psME);
          if (perspWin != switcherWin) return;

          MPerspective persp = (MPerspective) changedElement;
          if (!persp.getParent().isToBeRendered()) return;

          if (changedElement.isToBeRendered()) {
            addPerspectiveItem(persp);
          } else {
            removePerspectiveItem(persp);
          }
        }
  @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 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;
  }
        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);
          }
        }
  private void showElementRecursive(MUIElement element) {
    if (!element.isToBeRendered()) {
      return;
    }

    if (element instanceof MPlaceholder && element.getWidget() != null) {
      MPlaceholder ph = (MPlaceholder) element;
      MUIElement ref = ph.getRef();

      if (ref.getCurSharedRef() != ph) {
        ref.setCurSharedRef(ph);
        WPlaceholderWidget placeholder = (WPlaceholderWidget) ph.getWidget();
        @SuppressWarnings("unchecked")
        WLayoutedWidget<MUIElement> content = (WLayoutedWidget<MUIElement>) ref.getWidget();
        placeholder.setContent(content);
      }

      element = ref;
    }

    if (element instanceof MContext) {
      IEclipseContext context = ((MContext) element).getContext();
      if (context != null) {
        IEclipseContext newParentContext = modelService.getContainingContext(element);
        if (context.getParent() != newParentContext) {
          context.setParent(newParentContext);
        }
      }
    }

    if (element instanceof MWindow && element.getWidget() != null) {
      int visCount = 0;
      for (MUIElement kid : ((MWindow) element).getChildren()) {
        if (kid.isToBeRendered() && kid.isVisible()) visCount++;
      }
      if (visCount > 0) element.setVisible(true);
    }

    if (element instanceof MGenericStack) {
      MGenericStack<?> container = (MGenericStack<?>) element;
      MUIElement curSel = container.getSelectedElement();
      if (curSel == null && container.getChildren().size() > 0) {
        curSel = container.getChildren().get(0);
      }
      if (curSel != null) {
        showElementRecursive(curSel);
      }
    } else if (element instanceof MElementContainer<?>) {
      MElementContainer<?> container = (MElementContainer<?>) element;
      for (MUIElement childElement : container.getChildren().toArray(new MUIElement[0])) {
        showElementRecursive(childElement);
      }

      // OK, now process detached windows
      if (element instanceof MWindow) {
        for (MWindow w : ((MWindow) element).getWindows()) {
          showElementRecursive(w);
        }
      } else if (element instanceof MPerspective) {
        for (MWindow w : ((MPerspective) element).getWindows()) {
          showElementRecursive(w);
        }
      }
    }
  }
Exemplo n.º 16
0
  private void showElementRecursive(MUIElement element) {
    if (!element.isToBeRendered()) return;

    if (element instanceof MPlaceholder && element.getWidget() != null) {
      MPlaceholder ph = (MPlaceholder) element;
      MUIElement ref = ph.getRef();
      ref.setCurSharedRef(ph);

      ComponentContainer phComponent = (ComponentContainer) ph.getWidget();
      Component refComponent = (Component) ph.getRef().getWidget();
      phComponent.addComponent(refComponent);

      element = ref;

      // top right folder
      MPartStack topLeftStack = HierarchyUtils.findTopLeftFolder(ph.getRef());
      if (topLeftStack != null) {
        if (ph.getTags().contains(IPresentationEngine.MAXIMIZED))
          ((StackWidget) topLeftStack.getWidget()).setState(1);
        else if (ph.getTags().contains(IPresentationEngine.MINIMIZED))
          ((StackWidget) topLeftStack.getWidget()).setState(-1);
        else ((StackWidget) topLeftStack.getWidget()).setState(0);
      }
    }

    if (element instanceof MContext) {
      IEclipseContext context = ((MContext) element).getContext();
      if (context != null) {
        IEclipseContext newParentContext = modelService.getContainingContext(element);
        if (context.getParent() != newParentContext) {
          context.setParent(newParentContext);
        }
      }
    }

    // Show any floating windows
    if (element instanceof MWindow && element.getWidget() != null) {
      int visCount = 0;
      for (MUIElement kid : ((MWindow) element).getChildren()) {
        if (kid.isToBeRendered() && kid.isVisible()) visCount++;
      }
      if (visCount > 0) element.setVisible(true);
    }

    if (element instanceof MElementContainer<?>) {
      MElementContainer<?> container = (MElementContainer<?>) element;
      List<MUIElement> kids = new ArrayList<MUIElement>(container.getChildren());
      for (MUIElement childElement : kids) {
        showElementRecursive(childElement);
      }

      // OK, now process detached windows
      if (element instanceof MWindow) {
        for (MWindow w : ((MWindow) element).getWindows()) {
          showElementRecursive(w);
        }
      } else if (element instanceof MPerspective) {
        for (MWindow w : ((MPerspective) element).getWindows()) {
          showElementRecursive(w);
        }
      }
    }
  }