Skip to content

Package: ControlGenerator

ControlGenerator

nameinstructionbranchcomplexitylinemethod
addControls(EClass, VElement, Set)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
addControls(EClass, VElement, Set, boolean)
M: 0 C: 84
100%
M: 1 C: 11
92%
M: 1 C: 6
86%
M: 0 C: 19
100%
M: 0 C: 1
100%
createFeatureSegment(String)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
createLegacyDmr(EStructuralFeature, List)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
createSegmentDmr(EStructuralFeature, List)
M: 0 C: 25
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
generateAllControls(VView)
M: 30 C: 70
70%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 4 C: 19
83%
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: * Lucas Koehler - Extend for segment tooling
14: *
15: *******************************************************************************/
16: package org.eclipse.emf.ecp.view.internal.editor.handler;
17:
18: import java.io.IOException;
19: import java.util.Collections;
20: import java.util.HashMap;
21: import java.util.LinkedHashSet;
22: import java.util.List;
23: import java.util.Map;
24: import java.util.Set;
25:
26: import org.eclipse.core.runtime.IStatus;
27: import org.eclipse.core.runtime.Status;
28: import org.eclipse.emf.common.util.URI;
29: import org.eclipse.emf.ecore.EClass;
30: import org.eclipse.emf.ecore.EReference;
31: import org.eclipse.emf.ecore.EStructuralFeature;
32: import org.eclipse.emf.ecore.resource.Resource;
33: import org.eclipse.emf.ecore.resource.ResourceSet;
34: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
35: import org.eclipse.emf.ecore.util.EcoreUtil;
36: import org.eclipse.emf.ecore.xmi.XMLResource;
37: import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
38: import org.eclipse.emf.ecp.view.internal.editor.controls.Activator;
39: import org.eclipse.emf.ecp.view.spi.editor.controls.Helper;
40: import org.eclipse.emf.ecp.view.spi.model.VContainer;
41: import org.eclipse.emf.ecp.view.spi.model.VControl;
42: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
43: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReferenceSegment;
44: import org.eclipse.emf.ecp.view.spi.model.VElement;
45: import org.eclipse.emf.ecp.view.spi.model.VFeatureDomainModelReferenceSegment;
46: import org.eclipse.emf.ecp.view.spi.model.VFeaturePathDomainModelReference;
47: import org.eclipse.emf.ecp.view.spi.model.VView;
48: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
49: import org.eclipse.emfforms.spi.ide.view.segments.ToolingModeUtil;
50:
51: /** Generates and sets a list of controls to a {@link VView}. */
52: public final class ControlGenerator {
53:
54:         private ControlGenerator() {
55:
56:         }
57:
58:         /**
59:          * Create controls and set them to the view.
60:          *
61:          * @param rootClass the rootClass for identifying the path
62:          * @param compositeToFill the {@link VElement} to fill , must be of type {@link VView} or {@link VContainer}
63:          * @param features the list of features to create
64:          */
65:         public static void addControls(EClass rootClass, VElement compositeToFill, Set<EStructuralFeature> features) {
66:                 addControls(rootClass, compositeToFill, features, ToolingModeUtil.isSegmentToolingEnabled());
67:         }
68:
69:         /**
70:          * Create controls and set them to the view.
71:          *
72:          * @param rootClass the rootClass for identifying the path
73:          * @param compositeToFill the {@link VElement} to fill , must be of type {@link VView} or {@link VContainer}
74:          * @param features the list of features to create
75:          * @param segmentTooling <code>true</code> if segment based DMRs should be generated
76:          */
77:         static void addControls(EClass rootClass, VElement compositeToFill, Set<EStructuralFeature> features,
78:                 boolean segmentTooling) {
79:•                if (!VContainer.class.isInstance(compositeToFill) && !VView.class.isInstance(compositeToFill)) {
80:                         return;
81:                 }
82:                 final Map<EClass, EReference> childParentReferenceMap = new HashMap<EClass, EReference>();
83:                 Helper.getReferenceMap(rootClass, childParentReferenceMap);
84:
85:•                for (final EStructuralFeature feature : features) {
86:                         final VControl control = VViewFactory.eINSTANCE.createControl();
87:                         control.setName("Control " + feature.getName()); //$NON-NLS-1$
88:                         control.setReadonly(false);
89:
90:                         final List<EReference> bottomUpPath = Helper.getReferencePath(rootClass,
91:                                 feature.getEContainingClass(), childParentReferenceMap);
92:                         // How to create the dmr depends on the tooling mode (legacy or segments)
93:•                        final VDomainModelReference modelReference = segmentTooling
94:                                 ? createSegmentDmr(feature, bottomUpPath)
95:                                 : createLegacyDmr(feature, bottomUpPath);
96:
97:                         control.setDomainModelReference(modelReference);
98:
99:                         // add to the composite
100:•                        if (VContainer.class.isInstance(compositeToFill)) {
101:                                 ((VContainer) compositeToFill).getChildren().add(control);
102:•                        } else if (VView.class.isInstance(compositeToFill)) {
103:                                 ((VView) compositeToFill).getChildren().add(control);
104:                         }
105:                 }
106:         }
107:
108:         private static VDomainModelReference createLegacyDmr(EStructuralFeature domainFeature,
109:                 List<EReference> bottomUpPath) {
110:                 final VFeaturePathDomainModelReference dmr = VViewFactory.eINSTANCE.createFeaturePathDomainModelReference();
111:                 dmr.getDomainModelEReferencePath().addAll(bottomUpPath);
112:                 dmr.setDomainModelEFeature(domainFeature);
113:                 return dmr;
114:         }
115:
116:         private static VDomainModelReference createSegmentDmr(EStructuralFeature domainFeature,
117:                 List<EReference> bottomUpPath) {
118:                 final VDomainModelReference dmr = VViewFactory.eINSTANCE.createDomainModelReference();
119:                 bottomUpPath.stream()
120:                         .map(EReference::getName)
121:                         .map(ControlGenerator::createFeatureSegment)
122:                         .forEach(dmr.getSegments()::add);
123:                 dmr.getSegments().add(createFeatureSegment(domainFeature.getName()));
124:                 return dmr;
125:         }
126:
127:         private static VDomainModelReferenceSegment createFeatureSegment(String featureName) {
128:                 final VFeatureDomainModelReferenceSegment segment = VViewFactory.eINSTANCE
129:                         .createFeatureDomainModelReferenceSegment();
130:                 segment.setDomainModelFeature(featureName);
131:                 return segment;
132:         }
133:
134:         /**
135:          * Create all the controls and set them to the given view.
136:          *
137:          * @param view the view for which the controls are created for
138:          */
139:         public static void generateAllControls(final VView view) {
140:                 // load resource
141:                 final URI uri = EcoreUtil.getURI(view);
142:                 final Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
143:                 final Map<String, Object> m = reg.getExtensionToFactoryMap();
144:                 m.put("*", new XMIResourceFactoryImpl()); //$NON-NLS-1$
145:
146:                 final ResourceSet resSet = new ResourceSetImpl();
147:                 final Resource resource = resSet.getResource(uri, true);
148:                 try {
149:                         resource.load(null);
150:                 } catch (final IOException e) {
151:                         Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
152:                 }
153:                 // resolve the proxies
154:                 int rsSize = resSet.getResources().size();
155:                 EcoreUtil.resolveAll(resSet);
156:•                while (rsSize != resSet.getResources().size()) {
157:                         EcoreUtil.resolveAll(resSet);
158:                         rsSize = resSet.getResources().size();
159:                 }
160:                 final VView vview = (VView) resource.getContents().get(0);
161:
162:                 final EClass rootEClass = vview.getRootEClass();
163:                 final Set<EStructuralFeature> mySet = new LinkedHashSet<EStructuralFeature>(
164:                         rootEClass.getEAllStructuralFeatures());
165:                 addControls(rootEClass, (VView) resource.getContents().get(0), mySet);
166:                 try {
167:                         resource.save(Collections.singletonMap(XMLResource.OPTION_ENCODING, "UTF-8")); //$NON-NLS-1$
168:                 } catch (final IOException e) {
169:                         Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
170:                 }
171:         }
172: }