Skip to content

Package: EMFFormsContextProviderImpl

EMFFormsContextProviderImpl

nameinstructionbranchcomplexitylinemethod
EMFFormsContextProviderImpl()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
dispose()
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getContext()
M: 0 C: 23
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
getPriority()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
instantiate(ViewModelContext)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$1(IEclipseContext)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
setContext(IEclipseContext)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2019 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Lucas Koehler - initial API and implementation
13: * Christian W. Damus - bug 548592
14: ******************************************************************************/
15: package org.eclipse.emfforms.internal.swt.core.di;
16:
17: import java.util.HashMap;
18: import java.util.Map;
19:
20: import org.eclipse.e4.core.contexts.EclipseContextFactory;
21: import org.eclipse.e4.core.contexts.IEclipseContext;
22: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
23: import org.eclipse.emfforms.spi.core.services.view.EMFFormsViewContext;
24: import org.eclipse.emfforms.spi.swt.core.di.EMFFormsContextProvider;
25: import org.osgi.framework.FrameworkUtil;
26:
27: /**
28: * Basic implementation of {@link EMFFormsContextProvider}.
29: *
30: * @author Lucas Koehler
31: *
32: */
33: public class EMFFormsContextProviderImpl implements EMFFormsContextProvider {
34:
35:         private static final String CONTEXT_NAME = "EMF Forms View Context"; //$NON-NLS-1$
36:
37:         // Track contexts that have been set in us to dispose with the view context.
38:         // We need to keep them as long as clients may still be using them.
39:         // Map the external context to our child context
40:         private final Map<IEclipseContext, IEclipseContext> eclipseContexts = new HashMap<>();
41:
42:         // The current Eclipse context to provide to clients
43:         private IEclipseContext eclipseContext;
44:         private ViewModelContext viewModelContext;
45:
46:         @Override
47:         public void instantiate(ViewModelContext context) {
48:                 viewModelContext = context;
49:         }
50:
51:         @Override
52:         public void dispose() {
53:                 eclipseContexts.values().forEach(IEclipseContext::dispose);
54:                 eclipseContexts.clear();
55:                 viewModelContext = null;
56:         }
57:
58:         @Override
59:         public int getPriority() {
60:                 return 0;
61:         }
62:
63:         @Override
64:         public IEclipseContext getContext() {
65:•                if (eclipseContext == null) {
66:                         // Was the view context created with an initial Eclipse context?
67:                         IEclipseContext bootstrapContext = (IEclipseContext) viewModelContext
68:                                 .getContextValue(IEclipseContext.class.getName());
69:•                        if (bootstrapContext == null) {
70:                                 bootstrapContext = EclipseContextFactory
71:                                         .getServiceContext(FrameworkUtil.getBundle(EMFFormsContextProviderImpl.class).getBundleContext());
72:                         }
73:                         setContext(bootstrapContext);
74:                 }
75:                 return eclipseContext;
76:         }
77:
78:         @Override
79:         public void setContext(IEclipseContext eclipseContext) {
80:                 // We may have seen this context before
81:                 this.eclipseContext = eclipseContexts.computeIfAbsent(eclipseContext,
82:                         ctx -> {
83:                                 final IEclipseContext child = ctx.createChild(CONTEXT_NAME);
84:
85:                                 // Put the view model context in the Eclipse context
86:                                 child.set(EMFFormsViewContext.class, viewModelContext);
87:
88:                                 return child;
89:                         });
90:         }
91:
92: }