Skip to content

Package: ProxyResolverViewService

ProxyResolverViewService

nameinstructionbranchcomplexitylinemethod
ProxyResolverViewService()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
dispose()
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%
findProxiesAndResolve(VView)
M: 0 C: 23
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 6
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: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
resolveProxy(VViewProxy)
M: 6 C: 65
92%
M: 4 C: 4
50%
M: 4 C: 1
20%
M: 3 C: 19
86%
M: 0 C: 1
100%
resolveView(VViewProxy)
M: 0 C: 30
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2014 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: * Johannes Faltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.internal.viewproxy.resolver;
15:
16: import java.util.List;
17:
18: import org.eclipse.emf.common.util.TreeIterator;
19: import org.eclipse.emf.ecore.EObject;
20: import org.eclipse.emf.ecore.EStructuralFeature;
21: import org.eclipse.emf.ecore.util.EcoreUtil;
22: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
23: import org.eclipse.emf.ecp.view.spi.context.ViewModelService;
24: import org.eclipse.emf.ecp.view.spi.model.VElement;
25: import org.eclipse.emf.ecp.view.spi.model.VView;
26: import org.eclipse.emf.ecp.view.spi.model.VViewModelProperties;
27: import org.eclipse.emf.ecp.view.spi.model.util.ViewModelPropertiesHelper;
28: import org.eclipse.emf.ecp.view.spi.provider.ViewProviderHelper;
29: import org.eclipse.emf.ecp.view.spi.vertical.model.VVerticalFactory;
30: import org.eclipse.emf.ecp.view.spi.vertical.model.VVerticalLayout;
31: import org.eclipse.emf.ecp.view.spi.viewproxy.model.VViewProxy;
32:
33: /**
34: * {@link ViewModelService} which replaces all {@link VViewProxy} occurrences in the view model with its resolved
35: * contents.
36: *
37: * @author jfaltermeier
38: *
39: */
40: public class ProxyResolverViewService implements ViewModelService {
41:
42:         /**
43:          * Id for proxy view models.
44:          */
45:         public static final String VIEW_MODEL_ID = "proxyId"; //$NON-NLS-1$
46:
47:         private ViewModelContext context;
48:
49:         /**
50:          * {@inheritDoc}
51:          *
52:          * @see org.eclipse.emf.ecp.view.spi.context.ViewModelService#instantiate(org.eclipse.emf.ecp.view.spi.context.ViewModelContext)
53:          */
54:         @Override
55:         public void instantiate(ViewModelContext context) {
56:                 this.context = context;
57:                 final VView viewModel = (VView) context.getViewModel();
58:                 findProxiesAndResolve(viewModel);
59:         }
60:
61:         /**
62:          * {@inheritDoc}
63:          *
64:          * @see org.eclipse.emf.ecp.view.spi.context.ViewModelService#dispose()
65:          */
66:         @Override
67:         public void dispose() {
68:                 context = null;
69:         }
70:
71:         /**
72:          * {@inheritDoc}
73:          *
74:          * @see org.eclipse.emf.ecp.view.spi.context.ViewModelService#getPriority()
75:          */
76:         @Override
77:         public int getPriority() {
78:                 // Must be lower than the LocalizationViewModelService's priority because otherwise labels of elements defined
79:                 // in the proxy view are not localized.
80:                 // Should be lower than the DmrToSegments service so that segments are generated for the loaded proxy views.
81:                 return -2000;
82:         }
83:
84:         /**
85:          * Finds and resolves view proxies in the given view.
86:          *
87:          * @param view the view
88:          * @return whether resolvement was successful
89:          */
90:         /* package */boolean findProxiesAndResolve(VView view) {
91:                 final TreeIterator<EObject> iterator = view.eAllContents();
92:•                while (iterator.hasNext()) {
93:                         final EObject current = iterator.next();
94:•                        if (VViewProxy.class.isInstance(current)) {
95:                                 resolveProxy(VViewProxy.class.cast(current));
96:                         }
97:                 }
98:                 return true;
99:         }
100:
101:         private void resolveProxy(VViewProxy proxy) {
102:                 final VView view = resolveView(proxy);
103:                 final EStructuralFeature eContainingFeature = proxy.eContainingFeature();
104:                 final EObject eContainer = proxy.eContainer();
105:                 final Object objectContainer = eContainer.eGet(eContainingFeature);
106:                 int index = -1;
107:•                if (eContainingFeature.isMany()) {
108:                         final List<?> objectList = (List<?>) objectContainer;
109:                         index = objectList.indexOf(proxy);
110:                 }
111:                 EcoreUtil.remove(eContainer, eContainingFeature, proxy);
112:•                if (view == null) {
113:                         return;
114:                 }
115:•                if (!findProxiesAndResolve(view)) {
116:                         return;
117:                 }
118:                 final VVerticalLayout layout = VVerticalFactory.eINSTANCE.createVerticalLayout();
119:                 layout.getChildren().addAll(view.getChildren());
120:•                if (eContainingFeature.isMany()) {
121:                         // EMF API
122:                         @SuppressWarnings("unchecked")
123:                         final List<EObject> list = (List<EObject>) eContainer.eGet(eContainingFeature);
124:                         list.add(index, layout);
125:                 } else {
126:                         eContainer.eSet(eContainingFeature, layout);
127:                 }
128:
129:                 layout.getAttachments().addAll(proxy.getAttachments());
130:         }
131:
132:         private VView resolveView(VViewProxy proxy) {
133:                 final EObject eObject = context.getDomainModel();
134:                 final VElement viewModel = context.getViewModel();
135:                 final VViewModelProperties properties = ViewModelPropertiesHelper.getInhertitedPropertiesOrEmpty(viewModel);
136:•                final String id = proxy.getId() == null ? "" : proxy.getId(); //$NON-NLS-1$
137:                 properties.addNonInheritableProperty(VIEW_MODEL_ID, id);
138:                 final VView view = ViewProviderHelper.getView(eObject, properties);
139:                 return view;
140:         }
141: }