Skip to content

Package: EMFFormsEditSupportImpl

EMFFormsEditSupportImpl

nameinstructionbranchcomplexitylinemethod
EMFFormsEditSupportImpl()
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%
canSetProperty(VDomainModelReference, EObject)
M: 16 C: 40
71%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 4 C: 13
76%
M: 0 C: 1
100%
getEnumLiteral(EStructuralFeature, Object)
M: 0 C: 35
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
getEnumName(Object)
M: 13 C: 10
43%
M: 3 C: 1
25%
M: 2 C: 1
33%
M: 3 C: 2
40%
M: 0 C: 1
100%
getImage(VDomainModelReference, EObject, Object)
M: 17 C: 44
72%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 4 C: 14
78%
M: 0 C: 1
100%
getText(VDomainModelReference, EObject, Object)
M: 17 C: 70
80%
M: 3 C: 5
63%
M: 3 C: 2
40%
M: 4 C: 20
83%
M: 0 C: 1
100%
isMultiLine(VDomainModelReference, EObject)
M: 10 C: 46
82%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 2 C: 15
88%
M: 0 C: 1
100%
reportMissingPropertyDescriptor(EObject, EStructuralFeature)
M: 0 C: 22
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
setEMFFormsDatabinding(EMFFormsDatabinding)
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%
setEMFFormsLocalizationService(EMFFormsLocalizationService)
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%
setEMFSpecificService(EMFSpecificService)
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%
setReportService(ReportService)
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%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 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: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.core.services.editsupport;
15:
16: import org.eclipse.core.databinding.observable.IObserving;
17: import org.eclipse.core.databinding.observable.value.IObservableValue;
18: import org.eclipse.emf.common.util.Enumerator;
19: import org.eclipse.emf.ecore.EAttribute;
20: import org.eclipse.emf.ecore.EClassifier;
21: import org.eclipse.emf.ecore.EObject;
22: import org.eclipse.emf.ecore.EStructuralFeature;
23: import org.eclipse.emf.ecore.EcorePackage;
24: import org.eclipse.emf.ecp.common.spi.asserts.Assert;
25: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
26: import org.eclipse.emf.edit.provider.IItemLabelProvider;
27: import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
28: import org.eclipse.emfforms.spi.common.BundleResolver;
29: import org.eclipse.emfforms.spi.common.BundleResolverFactory;
30: import org.eclipse.emfforms.spi.common.BundleResolver.NoBundleFoundException;
31: import org.eclipse.emfforms.spi.common.report.AbstractReport;
32: import org.eclipse.emfforms.spi.common.report.ReportService;
33: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
34: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedReport;
35: import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
36: import org.eclipse.emfforms.spi.core.services.editsupport.EMFFormsEditSupport;
37: import org.eclipse.emfforms.spi.core.services.emfspecificservice.EMFSpecificService;
38: import org.eclipse.emfforms.spi.localization.EMFFormsLocalizationService;
39: import org.osgi.framework.Bundle;
40:
41: /**
42: * EMF implementation of {@link EMFFormsEditSupport}.
43: *
44: * @author Lucas Koehler
45: *
46: */
47: public class EMFFormsEditSupportImpl implements EMFFormsEditSupport {
48:         private EMFFormsDatabinding emfFormsDatabinding;
49:         private EMFSpecificService emfSpecificService;
50:         private ReportService reportService;
51:         private final BundleResolver bundleResolver = BundleResolverFactory.createBundleResolver();
52:         private EMFFormsLocalizationService emfFormsLocalizationService;
53:
54:         /**
55:          * Sets the {@link EMFFormsLocalizationService} service.
56:          *
57:          * @param emfFormsLocalizationService The localization service.
58:          */
59:         protected void setEMFFormsLocalizationService(EMFFormsLocalizationService emfFormsLocalizationService) {
60:                 this.emfFormsLocalizationService = emfFormsLocalizationService;
61:         }
62:
63:         /**
64:          * Sets the {@link EMFFormsDatabinding} service.
65:          *
66:          * @param emfFormsDatabinding The databinding service.
67:          */
68:         protected void setEMFFormsDatabinding(EMFFormsDatabinding emfFormsDatabinding) {
69:                 this.emfFormsDatabinding = emfFormsDatabinding;
70:         }
71:
72:         /**
73:          * Sets the {@link EMFSpecificService}.
74:          *
75:          * @param emfSpecificService The {@link EMFSpecificService}
76:          */
77:         protected void setEMFSpecificService(EMFSpecificService emfSpecificService) {
78:                 this.emfSpecificService = emfSpecificService;
79:         }
80:
81:         /**
82:          * Sets the {@link ReportService}.
83:          *
84:          * @param reportService The {@link ReportService}
85:          */
86:         protected void setReportService(ReportService reportService) {
87:                 this.reportService = reportService;
88:         }
89:
90:         /**
91:          * {@inheritDoc}
92:          *
93:          * @see org.eclipse.emfforms.spi.core.services.editsupport.EMFFormsEditSupport#isMultiLine(org.eclipse.emf.ecp.view.spi.model.VDomainModelReference,
94:          * org.eclipse.emf.ecore.EObject)
95:          */
96:         @Override
97:         public boolean isMultiLine(VDomainModelReference domainModelReference, EObject rootObject) {
98:                 Assert.create(domainModelReference).notNull();
99:                 Assert.create(rootObject).notNull();
100:
101:                 IObservableValue observableValue;
102:                 try {
103:                         observableValue = emfFormsDatabinding.getObservableValue(domainModelReference,
104:                                 rootObject);
105:                 } catch (final DatabindingFailedException ex) {
106:                         reportService.report(new DatabindingFailedReport(ex));
107:                         return false;
108:                 }
109:                 final IObserving observing = (IObserving) observableValue;
110:                 final EObject value = (EObject) observing.getObserved();
111:                 final EStructuralFeature structuralFeature = (EStructuralFeature) observableValue.getValueType();
112:                 observableValue.dispose();
113:
114:                 final IItemPropertyDescriptor itemPropertyDescriptor = emfSpecificService.getIItemPropertyDescriptor(value,
115:                         structuralFeature);
116:•                if (itemPropertyDescriptor == null) {
117:                         reportMissingPropertyDescriptor(value, structuralFeature);
118:                         return false;
119:                 }
120:
121:                 return itemPropertyDescriptor.isMultiLine(value);
122:         }
123:
124:         /**
125:          * {@inheritDoc}
126:          *
127:          * @see org.eclipse.emfforms.spi.core.services.editsupport.EMFFormsEditSupport#canSetProperty(org.eclipse.emf.ecp.view.spi.model.VDomainModelReference,
128:          * org.eclipse.emf.ecore.EObject)
129:          */
130:         @Override
131:         public boolean canSetProperty(VDomainModelReference domainModelReference, EObject rootObject) {
132:                 Assert.create(domainModelReference).notNull();
133:                 Assert.create(rootObject).notNull();
134:
135:                 IObservableValue observableValue;
136:                 try {
137:                         observableValue = emfFormsDatabinding.getObservableValue(domainModelReference,
138:                                 rootObject);
139:                 } catch (final DatabindingFailedException ex) {
140:                         reportService.report(new DatabindingFailedReport(ex));
141:                         return false;
142:                 }
143:                 final IObserving observing = (IObserving) observableValue;
144:                 final EObject value = (EObject) observing.getObserved();
145:                 final EStructuralFeature structuralFeature = (EStructuralFeature) observableValue.getValueType();
146:                 observableValue.dispose();
147:
148:                 final IItemPropertyDescriptor itemPropertyDescriptor = emfSpecificService.getIItemPropertyDescriptor(value,
149:                         structuralFeature);
150:•                if (itemPropertyDescriptor == null) {
151:                         reportMissingPropertyDescriptor(value, structuralFeature);
152:                         return false;
153:                 }
154:                 return itemPropertyDescriptor.canSetProperty(value);
155:         }
156:
157:         /**
158:          * {@inheritDoc}
159:          *
160:          * @see org.eclipse.emfforms.spi.core.services.editsupport.EMFFormsEditSupport#getText(org.eclipse.emf.ecp.view.spi.model.VDomainModelReference,
161:          * org.eclipse.emf.ecore.EObject, java.lang.Object)
162:          */
163:         @Override
164:         public String getText(VDomainModelReference domainModelReference, EObject rootObject, Object element) {
165:                 Assert.create(domainModelReference).notNull();
166:                 Assert.create(rootObject).notNull();
167:                 Assert.create(element).notNull();
168:
169:                 IObservableValue observableValue;
170:                 try {
171:                         observableValue = emfFormsDatabinding.getObservableValue(domainModelReference,
172:                                 rootObject);
173:                 } catch (final DatabindingFailedException ex) {
174:                         reportService.report(new DatabindingFailedReport(ex));
175:                         return ex.getMessage();
176:                 }
177:                 final IObserving observing = (IObserving) observableValue;
178:                 final EObject value = (EObject) observing.getObserved();
179:                 final EStructuralFeature structuralFeature = (EStructuralFeature) observableValue.getValueType();
180:                 observableValue.dispose();
181:
182:•                if (EAttribute.class.isInstance(structuralFeature)
183:•                        && EcorePackage.eINSTANCE.getEEnum().isInstance(EAttribute.class.cast(structuralFeature).getEType())) {
184:                         final String result = getEnumLiteral(structuralFeature, element);
185:•                        if (result != null) {
186:                                 return result;
187:                         }
188:                 }
189:
190:                 final IItemPropertyDescriptor itemPropertyDescriptor = emfSpecificService.getIItemPropertyDescriptor(value,
191:                         structuralFeature);
192:•                if (itemPropertyDescriptor == null) {
193:                         reportMissingPropertyDescriptor(value, structuralFeature);
194:                         return null;
195:                 }
196:                 final IItemLabelProvider labelProvider = itemPropertyDescriptor.getLabelProvider(rootObject);
197:
198:                 return labelProvider.getText(element);
199:         }
200:
201:         private static final String LITERAL_NAME = "_UI_%1$s_%2$s_literal"; //$NON-NLS-1$
202:
203:         private String getEnumLiteral(EStructuralFeature feature, Object element) {
204:                 final EClassifier featureType = feature.getEType();
205:                 final String enumName = getEnumName(element);
206:                 Bundle bundle;
207:                 try {
208:                         bundle = bundleResolver.getEditBundle(featureType);
209:                         final String key = String.format(LITERAL_NAME, featureType.getName(), enumName);
210:                         return emfFormsLocalizationService.getString(bundle, key);
211:                 } catch (final NoBundleFoundException ex) {
212:                         // do nothing - see bug 467498
213:                 }
214:                 return null;
215:         }
216:
217:         private String getEnumName(Object element) {
218:•                if (Enumerator.class.isInstance(element)) {
219:                         return Enumerator.class.cast(element).getName();
220:                 }
221:•                if (Enum.class.isInstance(element)) {
222:                         return Enum.class.cast(element).name();
223:                 }
224:                 return element.toString();
225:         }
226:
227:         /**
228:          * {@inheritDoc}
229:          *
230:          * @see org.eclipse.emfforms.spi.core.services.editsupport.EMFFormsEditSupport#getImage(org.eclipse.emf.ecp.view.spi.model.VDomainModelReference,
231:          * org.eclipse.emf.ecore.EObject, java.lang.Object)
232:          */
233:         @Override
234:         public Object getImage(VDomainModelReference domainModelReference, EObject rootObject, Object element) {
235:                 Assert.create(domainModelReference).notNull();
236:                 Assert.create(rootObject).notNull();
237:                 // Assert.create(element).notNull();
238:
239:                 IObservableValue observableValue;
240:                 try {
241:                         observableValue = emfFormsDatabinding.getObservableValue(domainModelReference,
242:                                 rootObject);
243:                 } catch (final DatabindingFailedException ex) {
244:                         reportService.report(new DatabindingFailedReport(ex));
245:                         return ex.getMessage();
246:                 }
247:                 final IObserving observing = (IObserving) observableValue;
248:                 final EObject value = (EObject) observing.getObserved();
249:                 final EStructuralFeature structuralFeature = (EStructuralFeature) observableValue.getValueType();
250:                 observableValue.dispose();
251:
252:                 final IItemPropertyDescriptor itemPropertyDescriptor = emfSpecificService.getIItemPropertyDescriptor(value,
253:                         structuralFeature);
254:•                if (itemPropertyDescriptor == null) {
255:                         reportMissingPropertyDescriptor(value, structuralFeature);
256:                         return null;
257:                 }
258:                 final IItemLabelProvider labelProvider = itemPropertyDescriptor.getLabelProvider(rootObject);
259:
260:                 return labelProvider.getImage(element);
261:         }
262:
263:         private void reportMissingPropertyDescriptor(final EObject value, final EStructuralFeature structuralFeature) {
264:                 reportService
265:                         .report(new AbstractReport(
266:                                 String
267:                                         .format(
268:                                                 "No IItemPropertyDescriptor for feature %2$s in EClass %1$s found.", value.eClass().getName(), //$NON-NLS-1$
269:                                                 structuralFeature.getName())));
270:         }
271: }