Skip to content

Package: ViewProviderHelper

ViewProviderHelper

nameinstructionbranchcomplexitylinemethod
getView(EObject, VViewModelProperties)
M: 4 C: 24
86%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 8
80%
M: 0 C: 1
100%
getView(EObject, VViewModelProperties, Collection)
M: 6 C: 30
83%
M: 3 C: 3
50%
M: 3 C: 1
25%
M: 3 C: 11
79%
M: 0 C: 1
100%
static {...}
M: 0 C: 9
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 4
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: * EclipseSource Muenchen - initial API and implementation
13: * Christian W. Damus - bug 547787
14: *
15: *******************************************************************************/
16: package org.eclipse.emf.ecp.view.spi.provider;
17:
18: import java.util.Collection;
19: import java.util.Collections;
20:
21: import org.eclipse.emf.ecore.EObject;
22: import org.eclipse.emf.ecp.view.spi.model.VView;
23: import org.eclipse.emf.ecp.view.spi.model.VViewModelProperties;
24: import org.osgi.framework.Bundle;
25: import org.osgi.framework.BundleContext;
26: import org.osgi.framework.FrameworkUtil;
27: import org.osgi.framework.ServiceReference;
28:
29: /**
30: * Util class for retrieving a {@link VView} based on an {@link EObject}.
31: *
32: * @author Eugen Neufeld
33: * @since 1.2
34: *
35: */
36: public final class ViewProviderHelper {
37:         static {
38:                 final Bundle bundle = FrameworkUtil.getBundle(ViewProviderHelper.class);
39:•                if (bundle != null) {
40:                         bundleContext = bundle.getBundleContext();
41:                 }
42:         }
43:         private static BundleContext bundleContext;
44:
45:         private ViewProviderHelper() {
46:         }
47:
48:         /**
49:          * This allows to retrieve a {@link VView} based on an {@link EObject}. This method reads all {@link IViewProvider
50:          * IViewProviders} and searches for the best fitting. If none can be found, then null is returned.
51:          *
52:          * @param eObject the {@link EObject} to find a {@link VView} for
53:          * @param properties the {@link VViewModelProperties properties}. May be <code>null</code>
54:          * @return a view model for the given {@link EObject} or null if no suited provider could be found
55:          * @since 1.7
56:          */
57:         public static VView getView(EObject eObject, VViewModelProperties properties) {
58:•                if (bundleContext == null) {
59:                         return null;
60:                 }
61:                 final ServiceReference<EMFFormsViewService> serviceReference = bundleContext
62:                         .getServiceReference(EMFFormsViewService.class);
63:•                if (serviceReference == null) {
64:                         return null;
65:                 }
66:                 final EMFFormsViewService viewService = bundleContext.getService(serviceReference);
67:                 final VView view = viewService.getView(eObject, properties);
68:                 bundleContext.ungetService(serviceReference);
69:                 return view;
70:         }
71:
72:         /**
73:          * Retrieve a {@link VView} for a domain model {@code object} from the most confident of my
74:          * {@linkplain EMFFormsViewService#addProvider(IViewProvider) registered view providers}.
75:          *
76:          * @param object the domain model object for which a view is to be requested
77:          * @param properties the {@link VViewModelProperties properties} for providing the view, that
78:          * may or may not include matching filters
79:          * @param requiredKeys a subset (possibly empty) of the keys in the {@code properties} that
80:          * must be matched by any view model that I would provide. If any of these keys does not match
81:          * a view model, then that view model must not be provided. Otherwise, it may just be less
82:          * preferred than some other view model that does match
83:          *
84:          * @return a view model for the given domain model {@code object} or {@code null} if no
85:          * suitable provider could be found to provide one
86:          *
87:          * @since 1.22
88:          */
89:         public static VView getView(EObject object, VViewModelProperties properties, Collection<String> requiredKeys) {
90:                 VView result = null;
91:
92:•                if (bundleContext == null) {
93:                         return result;
94:                 }
95:
96:                 final ServiceReference<EMFFormsFilteredViewService> serviceReference = bundleContext
97:                         .getServiceReference(EMFFormsFilteredViewService.class);
98:•                if (serviceReference == null) {
99:                         return result;
100:                 }
101:
102:                 final EMFFormsFilteredViewService viewService = bundleContext.getService(serviceReference);
103:                 try {
104:•                        if (requiredKeys == null) {
105:                                 requiredKeys = Collections.emptySet();
106:                         }
107:                         result = viewService.getView(object, properties, requiredKeys);
108:                 } finally {
109:                         bundleContext.ungetService(serviceReference);
110:                 }
111:
112:                 return result;
113:         }
114:
115: }