Skip to content

Package: DmrToSegmentsViewService

DmrToSegmentsViewService

nameinstructionbranchcomplexitylinemethod
DmrToSegmentsViewService(EMFFormsViewContext)
M: 0 C: 28
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
addSegmentsIfNecessary(VDomainModelReference)
M: 0 C: 16
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
lambda$0(List, EObject)
M: 0 C: 9
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
notifyAdd(Notifier)
M: 0 C: 8
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
notifyChange(ModelChangeNotification)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
notifyRemove(Notifier)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2018 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: * Lcuas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.core.services.segments;
15:
16: import java.util.LinkedList;
17: import java.util.List;
18:
19: import org.eclipse.emf.common.notify.Notifier;
20: import org.eclipse.emf.ecp.view.spi.model.ModelChangeAddRemoveListener;
21: import org.eclipse.emf.ecp.view.spi.model.ModelChangeNotification;
22: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
23: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReferenceSegment;
24: import org.eclipse.emf.ecp.view.spi.model.VElement;
25: import org.eclipse.emfforms.spi.core.services.segments.EMFFormsSegmentGenerator;
26: import org.eclipse.emfforms.spi.core.services.view.EMFFormsViewContext;
27:
28: /**
29: * <p>
30: * A view service that converts all {@link VDomainModelReference domain model references} of a view context to
31: * {@link VDomainModelReferenceSegment segments}. The generated segments are added to their respective DMRs.
32: * </p>
33: * <p>
34: * <strong>Note:</strong> This behavior can be activated by setting the program argument
35: * {@link org.eclipse.emfforms.spi.core.services.segments.RuntimeModeUtil#SEGMENT_GENERATION -enableSegmentGeneration}.
36: * </p>
37: *
38: * @author Lucas Koehler
39: *
40: */
41: public class DmrToSegmentsViewService implements ModelChangeAddRemoveListener {
42:
43:         private final EMFFormsSegmentGenerator segmentGenerator;
44:
45:         /**
46:          * Creates a new instance.
47:          *
48:          * @param viewContext The {@link EMFFormsViewContext} that created me
49:          */
50:         public DmrToSegmentsViewService(EMFFormsViewContext viewContext) {
51:                 viewContext.registerViewChangeListener(this);
52:                 segmentGenerator = viewContext.getService(EMFFormsSegmentGenerator.class);
53:
54:                 final VElement view = viewContext.getViewModel();
55:                 // We need to read the dmrs into a separate list because we add the segments in place and this might break the
56:                 // iteration of the TreeIterator returned by view.eAllContents().
57:                 final List<VDomainModelReference> allDmrs = new LinkedList<>();
58:                 view.eAllContents().forEachRemaining(object -> {
59:•                        if (object instanceof VDomainModelReference) {
60:                                 allDmrs.add((VDomainModelReference) object);
61:                         }
62:                 });
63:                 allDmrs.forEach(this::addSegmentsIfNecessary);
64:         }
65:
66:         /**
67:          * Checks whether a {@link VDomainModelReference} already uses {@link VDomainModelReferenceSegment segments}. If it
68:          * does not, generate the segments and add them to the DMR.
69:          *
70:          * @param dmr The {@link VDomainModelReference DMR} to check and add the segments to if applicable
71:          */
72:         private void addSegmentsIfNecessary(VDomainModelReference dmr) {
73:•                if (!dmr.getSegments().isEmpty()) {
74:                         // DMRs that already use segments must not be changed
75:                         return;
76:                 }
77:                 final List<VDomainModelReferenceSegment> segments = segmentGenerator.generateSegments(dmr);
78:                 dmr.getSegments().addAll(segments);
79:         }
80:
81:         @Override
82:         public void notifyChange(ModelChangeNotification notification) {
83:                 // Nothing to do
84:         }
85:
86:         @Override
87:         public void notifyAdd(Notifier notifier) {
88:•                if (notifier instanceof VDomainModelReference) {
89:                         addSegmentsIfNecessary((VDomainModelReference) notifier);
90:                 }
91:         }
92:
93:         @Override
94:         public void notifyRemove(Notifier notifier) {
95:                 // Nothing to do
96:         }
97: }