コード例 #1
0
  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;
  }