Search in sources :

Example 6 with MContribution

use of org.eclipse.e4.ui.model.application.MContribution in project eclipse.platform.ui by eclipse-platform.

the class HeadlessContextPresentationEngine method createGui.

@Override
public Object createGui(MUIElement element, Object parentWidget, IEclipseContext parentContext) {
    MUIElement current = element;
    while (current != null) {
        if (!current.isToBeRendered()) {
            return null;
        }
        if (current.getCurSharedRef() != null) {
            current = current.getCurSharedRef();
        } else {
            current = current.getParent();
        }
    }
    MUIElement parent = element.getParent();
    if (element.getCurSharedRef() != null) {
        parent = element.getCurSharedRef().getParent();
    }
    if (!(parent instanceof MApplication)) {
        // if the element is not under the application, it should have a
        // parent widget
        Assert.isNotNull(parentWidget);
    }
    if (element.getWidget() != null) {
        if (element instanceof MContext) {
            IEclipseContext context = ((MContext) element).getContext();
            if (context.getParent() != parentContext) {
                context.setParent(parentContext);
            }
        }
        return element.getWidget();
    }
    element.setRenderer(this);
    Object widget = new Object();
    element.setWidget(widget);
    if (element instanceof MContext) {
        MContext mcontext = (MContext) element;
        IEclipseContext createdContext = mcontext.getContext();
        if (createdContext == null) {
            String contextName = element.getClass().getInterfaces()[0].getName() + // $NON-NLS-1$
            " eclipse context";
            createdContext = (parentContext != null) ? parentContext.createChild(contextName) : EclipseContextFactory.create(contextName);
            populateModelInterfaces(mcontext, createdContext, element.getClass().getInterfaces());
            for (String variable : mcontext.getVariables()) {
                createdContext.declareModifiable(variable);
            }
            mcontext.setContext(createdContext);
            if (element instanceof MContribution && createContributions) {
                MContribution contribution = (MContribution) element;
                String uri = contribution.getContributionURI();
                if (uri != null) {
                    Object clientObject = contributionFactory.create(uri, createdContext);
                    contribution.setObject(clientObject);
                }
            }
            if (parentContext != null && parentContext.getActiveChild() == null) {
                createdContext.activate();
            }
        } else if (createdContext.getParent() != parentContext) {
            createdContext.setParent(parentContext);
        }
    }
    if (element instanceof MGenericStack) {
        MGenericStack<?> container = (MGenericStack<?>) element;
        MUIElement active = container.getSelectedElement();
        if (active != null) {
            createGui(active, container, getParentContext(active));
        } else {
            List<?> children = container.getChildren();
            if (!children.isEmpty()) {
                ((MElementContainer) element).setSelectedElement((MUIElement) children.get(0));
            }
        }
    } else if (element instanceof MElementContainer<?>) {
        for (Object child : ((MElementContainer<?>) element).getChildren()) {
            if (child instanceof MUIElement) {
                createGui((MUIElement) child, element, getParentContext((MUIElement) child));
                if (child instanceof MContext) {
                    IEclipseContext childContext = ((MContext) child).getContext();
                    IEclipseContext pContext = getParentContext((MUIElement) child);
                    if (childContext != null && pContext.getActiveChild() == null) {
                        childContext.activate();
                    }
                }
            }
        }
        if (element instanceof MWindow) {
            MWindow window = (MWindow) element;
            for (MWindow childWindow : window.getWindows()) {
                createGui(childWindow, element, window.getContext());
            }
        }
        if (element instanceof MPerspective) {
            MPerspective perspective = (MPerspective) element;
            for (MWindow childWindow : perspective.getWindows()) {
                createGui(childWindow, element, perspective.getContext());
            }
        }
    } else if (element instanceof MPlaceholder) {
        MPlaceholder placeholder = (MPlaceholder) element;
        MUIElement ref = placeholder.getRef();
        if (ref != null) {
            ref.setCurSharedRef(placeholder);
            ref.setToBeRendered(true);
            createGui(ref);
            List<MPlaceholder> placeholders = renderedPlaceholders.get(ref);
            if (placeholders == null) {
                placeholders = new ArrayList<>();
                renderedPlaceholders.put(ref, placeholders);
            } else if (placeholders.contains(placeholder)) {
                return null;
            }
            placeholders.add(placeholder);
        }
    }
    return widget;
}
Also used : MPlaceholder(org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder) MContext(org.eclipse.e4.ui.model.application.ui.MContext) MContribution(org.eclipse.e4.ui.model.application.MContribution) MElementContainer(org.eclipse.e4.ui.model.application.ui.MElementContainer) MWindow(org.eclipse.e4.ui.model.application.ui.basic.MWindow) MPerspective(org.eclipse.e4.ui.model.application.ui.advanced.MPerspective) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) MUIElement(org.eclipse.e4.ui.model.application.ui.MUIElement) EObject(org.eclipse.emf.ecore.EObject) MGenericStack(org.eclipse.e4.ui.model.application.ui.MGenericStack) MApplication(org.eclipse.e4.ui.model.application.MApplication)

Aggregations

IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)6 MContribution (org.eclipse.e4.ui.model.application.MContribution)6 EObject (org.eclipse.emf.ecore.EObject)4 IOException (java.io.IOException)2 InvalidRegistryObjectException (org.eclipse.core.runtime.InvalidRegistryObjectException)2 InjectionException (org.eclipse.e4.core.di.InjectionException)2 MContext (org.eclipse.e4.ui.model.application.ui.MContext)2 MElementContainer (org.eclipse.e4.ui.model.application.ui.MElementContainer)2 MUIElement (org.eclipse.e4.ui.model.application.ui.MUIElement)2 MPerspective (org.eclipse.e4.ui.model.application.ui.advanced.MPerspective)2 MPlaceholder (org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder)2 MWindow (org.eclipse.e4.ui.model.application.ui.basic.MWindow)2 TestableObject (org.eclipse.ui.testing.TestableObject)2 ArrayList (java.util.ArrayList)1 MApplication (org.eclipse.e4.ui.model.application.MApplication)1 MGenericStack (org.eclipse.e4.ui.model.application.ui.MGenericStack)1 MTrimmedWindow (org.eclipse.e4.ui.model.application.ui.basic.MTrimmedWindow)1