Skip to content

Package: DmrSelectorSegmentDmrControlSWTRenderer

DmrSelectorSegmentDmrControlSWTRenderer

nameinstructionbranchcomplexitylinemethod
DmrSelectorSegmentDmrControlSWTRenderer(VControl, ViewModelContext, ReportService, EMFFormsDatabindingEMF)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
addEcorePathToTemplate(String)
M: 26 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
createSWTControl(Composite)
M: 33 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
dispose()
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getText(Object)
M: 119 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 22 C: 0
0%
M: 1 C: 0
0%
lambda$0(EStructuralFeature)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
linkValue(Shell)
M: 106 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 27 C: 0
0%
M: 1 C: 0
0%
setDmrAndRootEClass(EObject, VDomainModelReference, EClass)
M: 0 C: 44
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 10
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: * Eugen - initial API and implementation
13: * Lucas Koehler - adapted to segments
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.view.template.internal.tooling.controls;
16:
17: import java.io.IOException;
18: import java.util.LinkedList;
19: import java.util.List;
20: import java.util.Optional;
21:
22: import javax.inject.Inject;
23:
24: import org.eclipse.core.databinding.observable.IObserving;
25: import org.eclipse.core.databinding.observable.value.IObservableValue;
26: import org.eclipse.core.resources.IFile;
27: import org.eclipse.emf.common.command.Command;
28: import org.eclipse.emf.common.command.CompoundCommand;
29: import org.eclipse.emf.common.notify.AdapterFactory;
30: import org.eclipse.emf.common.util.EList;
31: import org.eclipse.emf.databinding.IEMFValueProperty;
32: import org.eclipse.emf.ecore.EClass;
33: import org.eclipse.emf.ecore.EObject;
34: import org.eclipse.emf.ecore.EPackage;
35: import org.eclipse.emf.ecore.EReference;
36: import org.eclipse.emf.ecp.ide.spi.util.EcoreHelper;
37: import org.eclipse.emf.ecp.view.internal.editor.controls.EditableEReferenceLabelControlSWTRenderer;
38: import org.eclipse.emf.ecp.view.internal.editor.handler.CreateSegmentDmrWizard;
39: import org.eclipse.emf.ecp.view.internal.editor.handler.FeatureSegmentGenerator;
40: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
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.template.model.VTViewTemplate;
45: import org.eclipse.emf.ecp.view.template.selector.domainmodelreference.model.VTDomainModelReferenceSelector;
46: import org.eclipse.emf.ecp.view.template.selector.domainmodelreference.model.VTDomainmodelreferencePackage;
47: import org.eclipse.emf.edit.command.SetCommand;
48: import org.eclipse.emf.edit.domain.EditingDomain;
49: import org.eclipse.emf.edit.provider.AdapterFactoryItemDelegator;
50: import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
51: import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory;
52: import org.eclipse.emfforms.spi.common.report.AbstractReport;
53: import org.eclipse.emfforms.spi.common.report.ReportService;
54: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
55: import org.eclipse.emfforms.spi.core.services.databinding.emf.EMFFormsDatabindingEMF;
56: import org.eclipse.jface.window.Window;
57: import org.eclipse.jface.wizard.WizardDialog;
58: import org.eclipse.swt.widgets.Composite;
59: import org.eclipse.swt.widgets.Control;
60: import org.eclipse.swt.widgets.Shell;
61:
62: /**
63: * Control for setting a segment based DomainModelReference in the DomainModelReferenceSelector.
64: *
65: * @author Eugen Neufeld
66: * @author Lucas Koehler
67: *
68: */
69: public class DmrSelectorSegmentDmrControlSWTRenderer extends EditableEReferenceLabelControlSWTRenderer {
70:
71:         private AdapterFactoryItemDelegator adapterFactoryItemDelegator;
72:         private final EMFFormsDatabindingEMF databindingEMF;
73:         private ComposedAdapterFactory composedAdapterFactory;
74:
75:         /**
76:          * @param vElement the view model element to be rendered
77:          * @param viewContext the view context
78:          * @param reportService the {@link ReportService}
79:          * @param databindingEMF the {@link EMFFormsDatabindingEMF}
80:          */
81:         @Inject
82:         public DmrSelectorSegmentDmrControlSWTRenderer(VControl vElement, ViewModelContext viewContext,
83:                 ReportService reportService, EMFFormsDatabindingEMF databindingEMF) {
84:                 super(vElement, viewContext, reportService);
85:                 this.databindingEMF = databindingEMF;
86:         }
87:
88:         @Override
89:         protected Control createSWTControl(Composite parent) throws DatabindingFailedException {
90:                 final Control control = super.createSWTControl(parent);
91:
92:                 composedAdapterFactory = new ComposedAdapterFactory(new AdapterFactory[] {
93:                         new ReflectiveItemProviderAdapterFactory(),
94:                         new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE) });
95:                 adapterFactoryItemDelegator = new AdapterFactoryItemDelegator(composedAdapterFactory);
96:
97:                 return control;
98:         }
99:
100:         @Override
101:         protected void linkValue(Shell shell) {
102:                 final CreateSegmentDmrWizard dmrWizard = new CreateSegmentDmrWizard("Create Selector Domain Model Reference", //$NON-NLS-1$
103:                         sf -> null, new FeatureSegmentGenerator(), null, false);
104:                 final WizardDialog wd = new WizardDialog(shell, dmrWizard);
105:                 wd.setBlockOnOpen(true);
106:                 final int open = wd.open();
107:•                if (Window.CANCEL == open) {
108:                         return;
109:                 }
110:
111:                 IObservableValue<?> observableValue;
112:                 try {
113:                         observableValue = getModelValue();
114:                 } catch (final DatabindingFailedException ex) {
115:                         showLinkValueFailedMessageDialog(shell, ex);
116:                         return;
117:                 }
118:                 final EObject eObject = (EObject) ((IObserving) observableValue).getObserved();
119:                 final VDomainModelReference dmr = dmrWizard.getDomainModelReference().get();
120:
121:                 EClass rootEClass = dmrWizard.getRootEClass().get();
122:
123:                 /*
124:                  * If the ecore of the selected root EClass is loaded from the workspace, load and register the model, add it
125:                  * to the template's ecore paths, and resolve the root EClass in the registered version of the EPackage. The
126:                  * latter is necessary so that the EClass is properly referenced in the template model.
127:                  */
128:                 final Optional<IFile> selectedEcore = dmrWizard.getSelectedEcore();
129:•                if (selectedEcore.isPresent()) {
130:                         final String ecorePath = selectedEcore.get().getFullPath().toString();
131:                         try {
132:                                 EcoreHelper.registerEcore(ecorePath);
133:                                 addEcorePathToTemplate(ecorePath);
134:                                 final String nsUri = rootEClass.getEPackage().getNsURI();
135:                                 final EPackage ePackage = EPackage.Registry.INSTANCE.getEPackage(nsUri);
136:                                 rootEClass = (EClass) ePackage.getEClassifier(rootEClass.getName());
137:                         } catch (final IOException ex) {
138:                                 getReportService().report(new AbstractReport(ex,
139:                                         String.format("Could not add ecore path \"%s\" to the view template.", ecorePath))); //$NON-NLS-1$
140:                         }
141:                 }
142:
143:                 // Wrap setting the root EClass and the dmr in commands and execute
144:                 setDmrAndRootEClass(eObject, dmr, rootEClass);
145:         }
146:
147:         /**
148:          * Package visible to allow unit testing.
149:          *
150:          * @param eObject The {@link VTDomainModelReferenceSelector} to set the dmr and root EClass in.
151:          * @param dmr The {@link VDomainModelReference} to set
152:          * @param rootEClass The root {@link EClass} to set
153:          */
154:         /* private */void setDmrAndRootEClass(final EObject eObject, final VDomainModelReference dmr,
155:                 final EClass rootEClass) {
156:                 final List<Command> commands = new LinkedList<>();
157:                 final EditingDomain editingDomain = getEditingDomain(eObject);
158:                 final EReference rootEClassFeature = VTDomainmodelreferencePackage.Literals.DOMAIN_MODEL_REFERENCE_SELECTOR__ROOT_ECLASS;
159:•                if (eObject.eClass().getEAllReferences().contains(rootEClassFeature)) {
160:                         commands.add(SetCommand.create(editingDomain, eObject, rootEClassFeature, rootEClass));
161:                 }
162:                 final EReference dmrFeature = VTDomainmodelreferencePackage.Literals.DOMAIN_MODEL_REFERENCE_SELECTOR__DOMAIN_MODEL_REFERENCE;
163:                 commands.add(SetCommand.create(editingDomain, eObject, dmrFeature, dmr));
164:                 final CompoundCommand compoundCommand = new CompoundCommand(commands);
165:                 editingDomain.getCommandStack().execute(compoundCommand);
166:         }
167:
168:         /**
169:          * @param ecorePath
170:          */
171:         private void addEcorePathToTemplate(String ecorePath) {
172:                 EObject domain = getViewModelContext().getDomainModel();
173:•                while (!VTViewTemplate.class.isInstance(domain)) {
174:                         domain = domain.eContainer();
175:                 }
176:•                if (!VTViewTemplate.class.isInstance(domain)) {
177:                         return;
178:                 }
179:                 VTViewTemplate.class.cast(domain).getReferencedEcores().add(ecorePath);
180:         }
181:
182:         @Override
183:         protected Object getText(Object value) {
184:                 final VDomainModelReference modelReference = (VDomainModelReference) value;
185:•                if (modelReference == null || modelReference.getSegments().isEmpty()) {
186:                         return null;
187:                 }
188:                 final VTDomainModelReferenceSelector selector = VTDomainModelReferenceSelector.class
189:                         .cast(getViewModelContext().getDomainModel());
190:                 final EClass rootEClass = selector.getRootEClass();
191:                 final EList<VDomainModelReferenceSegment> segments = modelReference.getSegments();
192:
193:                 String attributeType = null;
194:                 try {
195:                         final IEMFValueProperty valueProperty = databindingEMF.getValueProperty(modelReference, rootEClass);
196:                         attributeType = valueProperty.getStructuralFeature().getEType().getName();
197:                 } catch (final DatabindingFailedException ex) {
198:                         // TODO handle?
199:                 }
200:
201:                 String attributeName = " -> " + adapterFactoryItemDelegator.getText(segments.get(segments.size() - 1)); //$NON-NLS-1$
202:•                if (attributeType != null && !attributeType.isEmpty()) {
203:                         attributeName += " : " + attributeType; //$NON-NLS-1$
204:                 }
205:                 String referencePath = ""; //$NON-NLS-1$
206:
207:•                for (int i = 0; i < segments.size() - 1; i++) {
208:                         referencePath = referencePath + " -> " //$NON-NLS-1$
209:                                 + adapterFactoryItemDelegator.getText(segments.get(i));
210:                 }
211:
212:                 final String linkText = rootEClass.getName() + referencePath + attributeName;
213:•                if (linkText.equals(" -> ")) { //$NON-NLS-1$
214:                         return null;
215:                 }
216:                 return linkText;
217:         }
218:
219:         @Override
220:         public void dispose() {
221:                 super.dispose();
222:•                if (composedAdapterFactory != null) {
223:                         composedAdapterFactory.dispose();
224:                 }
225:         }
226:
227: }