Search in sources :

Example 6 with Logger

use of org.eclipse.e4.core.services.log.Logger in project eclipse.platform.ui by eclipse-platform.

the class AbstractContributionItem method getMenu.

protected Menu getMenu(final MMenu mmenu, ToolItem toolItem) {
    Object obj = mmenu.getWidget();
    if (obj instanceof Menu && !((Menu) obj).isDisposed()) {
        return (Menu) obj;
    }
    // this is a temporary passthrough of the IMenuCreator
    if (RenderedElementUtil.isRenderedMenu(mmenu)) {
        obj = RenderedElementUtil.getContributionManager(mmenu);
        if (obj instanceof IContextFunction) {
            final IEclipseContext lclContext = getContext(mmenu);
            obj = ((IContextFunction) obj).compute(lclContext, null);
            RenderedElementUtil.setContributionManager(mmenu, obj);
        }
        if (obj instanceof IMenuCreator) {
            final IMenuCreator creator = (IMenuCreator) obj;
            final Menu menu = creator.getMenu(toolItem.getParent().getShell());
            if (menu != null) {
                toolItem.addDisposeListener(e -> {
                    if (menu != null && !menu.isDisposed()) {
                        creator.dispose();
                        mmenu.setWidget(null);
                    }
                });
                menu.setData(AbstractPartRenderer.OWNING_ME, menu);
                return menu;
            }
        }
    } else {
        final IEclipseContext lclContext = getContext(getModel());
        IPresentationEngine engine = lclContext.get(IPresentationEngine.class);
        obj = engine.createGui(mmenu, toolItem.getParent(), lclContext);
        if (obj instanceof Menu) {
            return (Menu) obj;
        }
        if (logger != null) {
            // $NON-NLS-1$
            logger.debug("Rendering returned " + obj);
        }
    }
    return null;
}
Also used : IContextFunction(org.eclipse.e4.core.contexts.IContextFunction) IMenuCreator(org.eclipse.jface.action.IMenuCreator) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) MMenu(org.eclipse.e4.ui.model.application.ui.menu.MMenu) Menu(org.eclipse.swt.widgets.Menu) IPresentationEngine(org.eclipse.e4.ui.workbench.IPresentationEngine)

Example 7 with Logger

use of org.eclipse.e4.core.services.log.Logger in project eclipse.platform.ui by eclipse-platform.

the class DirectContributionItem method executeItem.

@Override
protected void executeItem(Event trigger) {
    final IEclipseContext lclContext = getContext(getModel());
    if (!checkContribution(lclContext)) {
        return;
    }
    MContribution contrib = (MContribution) getModel();
    IEclipseContext staticContext = getStaticContext(trigger);
    Object result = ContextInjectionFactory.invoke(contrib.getObject(), Execute.class, getExecutionContext(lclContext), staticContext, missingExecute);
    if (result == missingExecute && logger != null) {
        // $NON-NLS-1$
        logger.error("Contribution is missing @Execute: " + contrib.getContributionURI());
    }
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) MContribution(org.eclipse.e4.ui.model.application.MContribution)

Example 8 with Logger

use of org.eclipse.e4.core.services.log.Logger in project eclipse.platform.ui by eclipse-platform.

the class MenuManagerShowProcessor method processDynamicElements.

/**
 * Process dynamic menu contributions provided by
 * {@link MDynamicMenuContribution} application model elements
 *
 * @param menuModel
 * @param menuManager
 */
private void processDynamicElements(MMenu menuModel, MenuManager menuManager) {
    MMenuElement[] menuElements = menuModel.getChildren().toArray(new MMenuElement[menuModel.getChildren().size()]);
    for (MMenuElement currentMenuElement : menuElements) {
        if (currentMenuElement instanceof MDynamicMenuContribution) {
            MDynamicMenuContribution dmc = (MDynamicMenuContribution) currentMenuElement;
            Object contribution = dmc.getObject();
            if (contribution == null) {
                IEclipseContext context = modelService.getContainingContext(menuModel);
                contribution = contributionFactory.create(dmc.getContributionURI(), context);
                dmc.setObject(contribution);
            }
            IEclipseContext dynamicMenuContext = EclipseContextFactory.create();
            ArrayList<MMenuElement> mel = new ArrayList<>();
            dynamicMenuContext.set(List.class, mel);
            dynamicMenuContext.set(MDynamicMenuContribution.class, dmc);
            IEclipseContext parentContext = modelService.getContainingContext(currentMenuElement);
            Object rc = ContextInjectionFactory.invoke(contribution, AboutToShow.class, parentContext, dynamicMenuContext, this);
            dynamicMenuContext.dispose();
            if (rc == this) {
                if (logger != null) {
                    // $NON-NLS-1$
                    logger.error("Missing @AboutToShow method in " + contribution);
                }
                continue;
            }
            if (mel.size() > 0) {
                int position = 0;
                while (position < menuModel.getChildren().size()) {
                    if (currentMenuElement == menuModel.getChildren().get(position)) {
                        position++;
                        break;
                    }
                    position++;
                }
                // and set the parent of the entries
                for (int j = 0; j < mel.size(); j++) {
                    MMenuElement menuElement = mel.get(j);
                    if (menuElement.getElementId() == null || menuElement.getElementId().length() < 1) {
                        menuElement.setElementId(currentMenuElement.getElementId() + "." + // $NON-NLS-1$
                        j);
                    }
                    menuModel.getChildren().add(position++, menuElement);
                    renderer.modelProcessSwitch(menuManager, menuElement);
                }
                currentMenuElement.getTransientData().put(DYNAMIC_ELEMENT_STORAGE_KEY, mel);
            }
        }
    }
}
Also used : MDynamicMenuContribution(org.eclipse.e4.ui.model.application.ui.menu.MDynamicMenuContribution) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) ArrayList(java.util.ArrayList) MMenuElement(org.eclipse.e4.ui.model.application.ui.menu.MMenuElement)

Example 9 with Logger

use of org.eclipse.e4.core.services.log.Logger in project eclipse.platform.ui by eclipse-platform.

the class PartRenderingEngine method run.

@Override
@Inject
@Optional
public Object run(final MApplicationElement uiRoot, final IEclipseContext runContext) {
    final Display display;
    if (runContext.get(Display.class) != null) {
        display = runContext.get(Display.class);
    } else {
        display = Display.getDefault();
        runContext.set(Display.class, display);
    }
    Realm.runWithDefault(DisplayRealm.getRealm(display), new Runnable() {

        @Override
        public void run() {
            initializeStyling(display, runContext);
            // Register an SWT resource handler
            runContext.set(IResourceUtilities.class, new ResourceUtility());
            // set up the keybinding manager
            KeyBindingDispatcher dispatcher = ContextInjectionFactory.make(KeyBindingDispatcher.class, runContext);
            runContext.set(KeyBindingDispatcher.class, dispatcher);
            keyListener = dispatcher.getKeyDownFilter();
            display.addFilter(SWT.KeyDown, keyListener);
            display.addFilter(SWT.Traverse, keyListener);
            // Show the initial UI
            // Create a 'limbo' shell (used to host controls that shouldn't
            // be in the current layout)
            Shell limbo = getLimboShell();
            runContext.set("limbo", limbo);
            // HACK!! we should loop until the display gets disposed...
            // ...then we listen for the last 'main' window to get disposed
            // and dispose the Display
            testShell = null;
            theApp = null;
            boolean spinOnce = true;
            if (uiRoot instanceof MApplication) {
                ShellActivationListener shellDialogListener = new ShellActivationListener((MApplication) uiRoot);
                display.addFilter(SWT.Activate, shellDialogListener);
                display.addFilter(SWT.Deactivate, shellDialogListener);
                // loop until the app closes
                spinOnce = false;
                theApp = (MApplication) uiRoot;
                // long startTime = System.currentTimeMillis();
                for (MWindow window : theApp.getChildren()) {
                    createGui(window);
                }
                // long endTime = System.currentTimeMillis();
                // System.out.println("Render: " + (endTime - startTime));
                // tell the app context we are starting so the splash is
                // torn down
                IApplicationContext ac = appContext.get(IApplicationContext.class);
                if (ac != null) {
                    ac.applicationRunning();
                    if (eventBroker != null) {
                        eventBroker.post(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE, theApp);
                    }
                }
            } else if (uiRoot instanceof MUIElement) {
                if (uiRoot instanceof MWindow) {
                    testShell = (Shell) createGui((MUIElement) uiRoot);
                } else {
                    // Special handling for partial models (for testing...)
                    testShell = new Shell(display, SWT.SHELL_TRIM);
                    createGui((MUIElement) uiRoot, testShell, null);
                }
            }
            // allow any early startup extensions to run
            Runnable earlyStartup = (Runnable) runContext.get(EARLY_STARTUP_HOOK);
            if (earlyStartup != null) {
                earlyStartup.run();
            }
            TestableObject testableObject = runContext.get(TestableObject.class);
            if (testableObject instanceof E4Testable) {
                ((E4Testable) testableObject).init(display, runContext.get(IWorkbench.class));
            }
            IEventLoopAdvisor advisor = runContext.getActiveLeaf().get(IEventLoopAdvisor.class);
            if (advisor == null) {
                advisor = new IEventLoopAdvisor() {

                    @Override
                    public void eventLoopIdle(Display display) {
                        display.sleep();
                    }

                    @Override
                    public void eventLoopException(Throwable exception) {
                        StatusReporter statusReporter = appContext.get(StatusReporter.class);
                        if (statusReporter != null) {
                            statusReporter.show(StatusReporter.ERROR, "Internal Error", exception);
                        } else if (logger != null) {
                            logger.error(exception);
                        }
                    }
                };
            }
            final IEventLoopAdvisor finalAdvisor = advisor;
            display.setErrorHandler(e -> {
                // recoverable, hand it to the event loop advisor
                if (e instanceof LinkageError || e instanceof AssertionError) {
                    handle(e, finalAdvisor);
                } else {
                    // Otherwise, rethrow it
                    throw e;
                }
            });
            display.setRuntimeExceptionHandler(e -> handle(e, finalAdvisor));
            // Spin the event loop until someone disposes the display
            while (((testShell != null && !testShell.isDisposed()) || (theApp != null && someAreVisible(theApp.getChildren()))) && !display.isDisposed()) {
                try {
                    if (!display.readAndDispatch()) {
                        runContext.processWaiting();
                        if (spinOnce) {
                            return;
                        }
                        advisor.eventLoopIdle(display);
                    }
                } catch (ThreadDeath th) {
                    throw th;
                } catch (Exception | Error err) {
                    handle(err, advisor);
                }
            }
            if (!spinOnce) {
                cleanUp();
            }
        }

        private void handle(Throwable ex, IEventLoopAdvisor advisor) {
            try {
                advisor.eventLoopException(ex);
            } catch (Throwable t) {
                if (t instanceof ThreadDeath) {
                    throw (ThreadDeath) t;
                }
                // couldn't handle the exception, print to console
                t.printStackTrace();
            }
        }
    });
    return IApplication.EXIT_OK;
}
Also used : ContextInjectionFactory(org.eclipse.e4.core.contexts.ContextInjectionFactory) UIEvents(org.eclipse.e4.ui.workbench.UIEvents) LogService(org.osgi.service.log.LogService) MApplicationElement(org.eclipse.e4.ui.model.application.MApplicationElement) IResourceUtilities(org.eclipse.e4.ui.workbench.IResourceUtilities) URL(java.net.URL) EventHandler(org.osgi.service.event.EventHandler) SWTKeySupport(org.eclipse.jface.bindings.keys.SWTKeySupport) CSSSWTEngineImpl(org.eclipse.e4.ui.css.swt.engine.CSSSWTEngineImpl) IScopeContext(org.eclipse.core.runtime.preferences.IScopeContext) MPartStack(org.eclipse.e4.ui.model.application.ui.basic.MPartStack) OSGiResourceLocator(org.eclipse.e4.ui.css.core.util.impl.resources.OSGiResourceLocator) IExtensionRegistry(org.eclipse.core.runtime.IExtensionRegistry) Composite(org.eclipse.swt.widgets.Composite) CSSStyleDeclaration(org.w3c.dom.css.CSSStyleDeclaration) Map(java.util.Map) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) IThemeManager(org.eclipse.e4.ui.css.swt.theme.IThemeManager) Focus(org.eclipse.e4.ui.di.Focus) Widget(org.eclipse.swt.widgets.Widget) IRendererFactory(org.eclipse.e4.ui.workbench.swt.factories.IRendererFactory) MApplication(org.eclipse.e4.ui.model.application.MApplication) MElementContainer(org.eclipse.e4.ui.model.application.ui.MElementContainer) Realm(org.eclipse.core.databinding.observable.Realm) IStylingEngine(org.eclipse.e4.ui.services.IStylingEngine) KeyBindingDispatcher(org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher) Activator(org.eclipse.e4.ui.internal.workbench.Activator) NLS(org.eclipse.osgi.util.NLS) CTabFolder(org.eclipse.swt.custom.CTabFolder) TestableObject(org.eclipse.ui.testing.TestableObject) Set(java.util.Set) EObject(org.eclipse.emf.ecore.EObject) Optional(org.eclipse.e4.core.di.annotations.Optional) MToolBar(org.eclipse.e4.ui.model.application.ui.menu.MToolBar) Display(org.eclipse.swt.widgets.Display) DefaultScope(org.eclipse.core.runtime.preferences.DefaultScope) List(java.util.List) InstanceScope(org.eclipse.core.runtime.preferences.InstanceScope) DisplayRealm(org.eclipse.jface.databinding.swt.DisplayRealm) IWorkbench(org.eclipse.e4.ui.workbench.IWorkbench) MTrimmedWindow(org.eclipse.e4.ui.model.application.ui.basic.MTrimmedWindow) SWT(org.eclipse.swt.SWT) Entry(java.util.Map.Entry) PostConstruct(javax.annotation.PostConstruct) IApplicationContext(org.eclipse.equinox.app.IApplicationContext) PersistState(org.eclipse.e4.ui.di.PersistState) EclipseContextFactory(org.eclipse.e4.core.contexts.EclipseContextFactory) MPerspective(org.eclipse.e4.ui.model.application.ui.advanced.MPerspective) SafeRunner(org.eclipse.core.runtime.SafeRunner) MContribution(org.eclipse.e4.ui.model.application.MContribution) StatusReporter(org.eclipse.e4.core.services.statusreporter.StatusReporter) IContributionFactory(org.eclipse.e4.core.services.contributions.IContributionFactory) HashMap(java.util.HashMap) Logger(org.eclipse.e4.core.services.log.Logger) MUIElement(org.eclipse.e4.ui.model.application.ui.MUIElement) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) KeyFormatterFactory(org.eclipse.jface.bindings.keys.formatting.KeyFormatterFactory) MPlaceholder(org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder) MPart(org.eclipse.e4.ui.model.application.ui.basic.MPart) EclipsePreferencesHelper(org.eclipse.e4.ui.css.swt.helpers.EclipsePreferencesHelper) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Listener(org.eclipse.swt.widgets.Listener) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) IApplication(org.eclipse.equinox.app.IApplication) MWindow(org.eclipse.e4.ui.model.application.ui.basic.MWindow) Named(javax.inject.Named) Shell(org.eclipse.swt.widgets.Shell) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) MMenu(org.eclipse.e4.ui.model.application.ui.menu.MMenu) E4Workbench(org.eclipse.e4.ui.internal.workbench.E4Workbench) ShellEvent(org.eclipse.swt.events.ShellEvent) MContext(org.eclipse.e4.ui.model.application.ui.MContext) IOException(java.io.IOException) IThemeEngine(org.eclipse.e4.ui.css.swt.theme.IThemeEngine) InjectionException(org.eclipse.e4.core.di.InjectionException) IPresentationEngine(org.eclipse.e4.ui.workbench.IPresentationEngine) ShellAdapter(org.eclipse.swt.events.ShellAdapter) MGenericStack(org.eclipse.e4.ui.model.application.ui.MGenericStack) InvalidRegistryObjectException(org.eclipse.core.runtime.InvalidRegistryObjectException) FileLocator(org.eclipse.core.runtime.FileLocator) WidgetElement(org.eclipse.e4.ui.css.swt.dom.WidgetElement) EModelService(org.eclipse.e4.ui.workbench.modeling.EModelService) Element(org.w3c.dom.Element) EventTopic(org.eclipse.e4.core.di.extensions.EventTopic) Platform(org.eclipse.core.runtime.Platform) IEventBroker(org.eclipse.e4.core.services.events.IEventBroker) Event(org.osgi.service.event.Event) Control(org.eclipse.swt.widgets.Control) InputStream(java.io.InputStream) IApplicationContext(org.eclipse.equinox.app.IApplicationContext) MWindow(org.eclipse.e4.ui.model.application.ui.basic.MWindow) TestableObject(org.eclipse.ui.testing.TestableObject) KeyBindingDispatcher(org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher) Shell(org.eclipse.swt.widgets.Shell) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) IResourceUtilities(org.eclipse.e4.ui.workbench.IResourceUtilities) MUIElement(org.eclipse.e4.ui.model.application.ui.MUIElement) Display(org.eclipse.swt.widgets.Display) MApplication(org.eclipse.e4.ui.model.application.MApplication) StatusReporter(org.eclipse.e4.core.services.statusreporter.StatusReporter) Inject(javax.inject.Inject) Optional(org.eclipse.e4.core.di.annotations.Optional)

Example 10 with Logger

use of org.eclipse.e4.core.services.log.Logger in project eclipse.platform.ui by eclipse-platform.

the class PartRenderingEngine method safeRemoveGui.

private void safeRemoveGui(MUIElement element) {
    if (removeRoot == null)
        removeRoot = element;
    // We call 'hideChild' *before* checking if the actual element
    // has been rendered in order to pick up cases of 'lazy loading'
    MUIElement parent = element.getParent();
    AbstractPartRenderer parentRenderer = parent != null ? getRendererFor(parent) : null;
    if (parentRenderer != null) {
        parentRenderer.hideChild(element.getParent(), element);
    }
    AbstractPartRenderer renderer = getRendererFor(element);
    // If the element hasn't been rendered then this is a NO-OP
    if (renderer != null) {
        if (element instanceof MElementContainer<?>) {
            @SuppressWarnings("unchecked") MElementContainer<MUIElement> container = (MElementContainer<MUIElement>) element;
            MUIElement selectedElement = container.getSelectedElement();
            List<MUIElement> children = container.getChildren();
            // Bug 458460: Operate on a copy in case child nulls out parent
            for (MUIElement child : new ArrayList<>(children)) {
                // remove stuff in the "back" first
                if (child != selectedElement) {
                    removeGui(child);
                }
            }
            if (selectedElement != null && children.contains(selectedElement)) {
                // now remove the selected element
                removeGui(selectedElement);
            }
        }
        if (element instanceof MPerspective) {
            MPerspective perspective = (MPerspective) element;
            for (MWindow subWindow : perspective.getWindows()) {
                removeGui(subWindow);
            }
        } else if (element instanceof MWindow) {
            MWindow window = (MWindow) element;
            for (MWindow subWindow : window.getWindows()) {
                removeGui(subWindow);
            }
            if (window instanceof MTrimmedWindow) {
                MTrimmedWindow trimmedWindow = (MTrimmedWindow) window;
                for (MUIElement trimBar : trimmedWindow.getTrimBars()) {
                    removeGui(trimBar);
                }
            }
        }
        if (element instanceof MContribution) {
            MContribution contribution = (MContribution) element;
            Object client = contribution.getObject();
            IEclipseContext parentContext = renderer.getContext(element);
            if (parentContext != null && client != null) {
                try {
                    ContextInjectionFactory.invoke(client, PersistState.class, parentContext, null);
                } catch (Exception e) {
                    if (logger != null) {
                        logger.error(e);
                    }
                }
            }
        }
        renderer.disposeWidget(element);
        // unset the client object
        if (element instanceof MContribution) {
            MContribution contribution = (MContribution) element;
            Object client = contribution.getObject();
            IEclipseContext parentContext = renderer.getContext(element);
            if (parentContext != null && client != null) {
                try {
                    ContextInjectionFactory.uninject(client, parentContext);
                } catch (Exception e) {
                    if (logger != null) {
                        logger.error(e);
                    }
                }
            }
            contribution.setObject(null);
        }
        // dispose the context
        if (element instanceof MContext) {
            clearContext((MContext) element);
        }
    }
    if (element instanceof MPlaceholder) {
        MPlaceholder ph = (MPlaceholder) element;
        if (ph.getRef() != null && ph.getRef().getCurSharedRef() == ph) {
            ph.getRef().setCurSharedRef(null);
        }
    }
    if (removeRoot == element)
        removeRoot = null;
}
Also used : MPlaceholder(org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder) ArrayList(java.util.ArrayList) 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) IOException(java.io.IOException) InjectionException(org.eclipse.e4.core.di.InjectionException) InvalidRegistryObjectException(org.eclipse.core.runtime.InvalidRegistryObjectException) MPerspective(org.eclipse.e4.ui.model.application.ui.advanced.MPerspective) MTrimmedWindow(org.eclipse.e4.ui.model.application.ui.basic.MTrimmedWindow) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) MUIElement(org.eclipse.e4.ui.model.application.ui.MUIElement) TestableObject(org.eclipse.ui.testing.TestableObject) EObject(org.eclipse.emf.ecore.EObject)

Aggregations

IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)12 IOException (java.io.IOException)9 Logger (org.eclipse.e4.core.services.log.Logger)7 EObject (org.eclipse.emf.ecore.EObject)7 ArrayList (java.util.ArrayList)6 InjectionException (org.eclipse.e4.core.di.InjectionException)5 MApplication (org.eclipse.e4.ui.model.application.MApplication)5 MWindow (org.eclipse.e4.ui.model.application.ui.basic.MWindow)5 List (java.util.List)4 IPresentationEngine (org.eclipse.e4.ui.workbench.IPresentationEngine)4 Display (org.eclipse.swt.widgets.Display)4 Shell (org.eclipse.swt.widgets.Shell)4 File (java.io.File)3 Set (java.util.Set)3 IExtensionRegistry (org.eclipse.core.runtime.IExtensionRegistry)3 Platform (org.eclipse.core.runtime.Platform)3 ContextInjectionFactory (org.eclipse.e4.core.contexts.ContextInjectionFactory)3 IContributionFactory (org.eclipse.e4.core.services.contributions.IContributionFactory)3 E4Workbench (org.eclipse.e4.ui.internal.workbench.E4Workbench)3 MContribution (org.eclipse.e4.ui.model.application.MContribution)3