Skip to content

Package: PreSetValidationStrategy

PreSetValidationStrategy

nameinstructionbranchcomplexitylinemethod
PreSetValidationStrategy(VElement, EStructuralFeature, UpdateValueStrategy)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
convert(Object)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getStructuralFeature()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getUpdatePolicy()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getVElement()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setAfterConvertValidator(IValidator)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setAfterGetValidator(IValidator)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setBeforeSetValidator(IValidator)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setConverter(IConverter)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
validateAfterConvert(Object)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
validateAfterGet(Object)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
validateBeforeSet(Object)
M: 18 C: 60
77%
M: 5 C: 7
58%
M: 4 C: 3
43%
M: 3 C: 18
86%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2017 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: * Edgar Mueller - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.edit.spi.swt.util;
15:
16: import org.eclipse.core.databinding.UpdateValueStrategy;
17: import org.eclipse.core.databinding.conversion.IConverter;
18: import org.eclipse.core.databinding.validation.IValidator;
19: import org.eclipse.core.runtime.IStatus;
20: import org.eclipse.core.runtime.Status;
21: import org.eclipse.emf.common.util.BasicDiagnostic;
22: import org.eclipse.emf.common.util.Diagnostic;
23: import org.eclipse.emf.databinding.EMFUpdateValueStrategy;
24: import org.eclipse.emf.ecore.EStructuralFeature;
25: import org.eclipse.emf.ecp.view.spi.model.VDiagnostic;
26: import org.eclipse.emf.ecp.view.spi.model.VElement;
27: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
28: import org.eclipse.emf.edit.command.SetCommand;
29: import org.eclipse.emfforms.spi.common.validation.PreSetValidationService;
30: import org.osgi.framework.BundleContext;
31: import org.osgi.framework.FrameworkUtil;
32: import org.osgi.framework.ServiceReference;
33:
34: /**
35: * A common {@link EMFUpdateValueStrategy} that implements {@link #validateBeforeSet(Object)}.
36: *
37: * @since 1.13
38: */
39: public class PreSetValidationStrategy extends EMFUpdateValueStrategy {
40:
41:         private final EStructuralFeature eStructuralFeature;
42:         private final VElement vElement;
43:         private final UpdateValueStrategy strategy;
44:
45:         /**
46:          * Constructor.
47:          *
48:          * @param vElement the {@link VElement}
49:          * @param eStructuralFeature an {@link EStructuralFeature} that defines any validation constraints
50:          * @param delegate the strategy to delegate to
51:          */
52:         public PreSetValidationStrategy(VElement vElement, EStructuralFeature eStructuralFeature,
53:                 UpdateValueStrategy delegate) {
54:                 this.vElement = vElement;
55:                 this.eStructuralFeature = eStructuralFeature;
56:                 strategy = delegate;
57:         }
58:
59:         @Override
60:         public IStatus validateBeforeSet(Object value) {
61:                 final BundleContext bundleContext = FrameworkUtil
62:                         .getBundle(getClass())
63:                         .getBundleContext();
64:                 final ServiceReference<PreSetValidationService> serviceReference = bundleContext
65:                         .getServiceReference(PreSetValidationService.class);
66:
67:•                if (serviceReference == null) {
68:                         return strategy.validateBeforeSet(value);
69:                 }
70:
71:•                if (eStructuralFeature.isUnsettable() && SetCommand.UNSET_VALUE == value) {
72:                         /* we are dealing with an unsettable feature and the unset value */
73:                         /* we need to validate the default value instead of the unset value */
74:                         value = eStructuralFeature.getDefaultValue();
75:                 }
76:
77:                 try {
78:
79:                         final PreSetValidationService service = bundleContext.getService(serviceReference);
80:
81:•                        if (service == null) {
82:                                 return strategy.validateBeforeSet(value);
83:                         }
84:
85:                         final Diagnostic result = service.validate(eStructuralFeature, value);
86:
87:•                        if (result.getSeverity() == Diagnostic.OK) {
88:                                 return Status.OK_STATUS;
89:                         }
90:
91:                         // TODO: existing diagnostics?
92:                         final VDiagnostic vDiagnostic = VViewFactory.eINSTANCE.createDiagnostic();
93:                         vDiagnostic.getDiagnostics().add(result);
94:•                        if (vElement != null) {
95:                                 vElement.setDiagnostic(vDiagnostic);
96:                         }
97:                         return BasicDiagnostic.toIStatus(result);
98:                 } finally {
99:                         bundleContext.ungetService(serviceReference);
100:                 }
101:         }
102:
103:         @Override
104:         public Object convert(Object value) {
105:                 return strategy.convert(value);
106:         }
107:
108:         @Override
109:         public int getUpdatePolicy() {
110:                 return strategy.getUpdatePolicy();
111:         }
112:
113:         @Override
114:         public UpdateValueStrategy setAfterConvertValidator(IValidator validator) {
115:                 super.setAfterConvertValidator(validator);
116:                 return strategy.setAfterConvertValidator(validator);
117:         }
118:
119:         @Override
120:         public UpdateValueStrategy setBeforeSetValidator(IValidator validator) {
121:                 super.setBeforeSetValidator(validator);
122:                 return strategy.setBeforeSetValidator(validator);
123:         }
124:
125:         @Override
126:         public UpdateValueStrategy setAfterGetValidator(IValidator validator) {
127:                 super.setAfterGetValidator(validator);
128:                 return strategy.setAfterGetValidator(validator);
129:         }
130:
131:         @Override
132:         public UpdateValueStrategy setConverter(IConverter converter) {
133:                 super.setConverter(converter);
134:                 return strategy.setConverter(converter);
135:         }
136:
137:         @Override
138:         public IStatus validateAfterConvert(Object value) {
139:                 return strategy.validateAfterConvert(value);
140:         }
141:
142:         @Override
143:         public IStatus validateAfterGet(Object value) {
144:                 return strategy.validateAfterGet(value);
145:         }
146:
147:         /**
148:          * Returns the {@link EStructuralFeature} that defines any validation constraints.
149:          *
150:          * @return the structural feature
151:          */
152:         public EStructuralFeature getStructuralFeature() {
153:                 return eStructuralFeature;
154:         }
155:
156:         /**
157:          * Returns the associated {@link VElement}.
158:          *
159:          * @return the {@link VElement}
160:          */
161:         public VElement getVElement() {
162:                 return vElement;
163:         }
164: }