Skip to content

Package: ValidationService

ValidationService

nameinstructionbranchcomplexitylinemethod
getSubstitutionLabelProvider(EMFFormsViewContext, AdapterFactory)
M: 30 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
validate(Iterable)
M: 7 C: 15
68%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 3 C: 5
63%
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: * Christian W. Damus - bugs 548761, 552127
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.view.spi.validation;
16:
17: import java.util.Collection;
18: import java.util.stream.Collectors;
19: import java.util.stream.StreamSupport;
20:
21: import org.eclipse.emf.common.notify.AdapterFactory;
22: import org.eclipse.emf.ecore.EObject;
23: import org.eclipse.emf.ecore.EValidator.SubstitutionLabelProvider;
24: import org.eclipse.emf.ecp.view.internal.validation.ECPSubstitutionLabelProvider;
25: import org.eclipse.emf.ecp.view.internal.validation.ViewSubstitutionLabelProviderFactory;
26: import org.eclipse.emf.ecp.view.spi.context.GlobalViewModelService;
27: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
28: import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
29: import org.eclipse.emfforms.spi.core.services.view.EMFFormsViewContext;
30:
31: /**
32: * @author Eugen
33: * @since 1.5
34: *
35: */
36: public interface ValidationService extends GlobalViewModelService {
37:         /**
38:          * Adds a validation provider to the list of known validation providers. The domain model will be revalidated after
39:          * the provider has been added.
40:          *
41:          * @param validationProvider the {@link ValidationProvider} to add
42:          */
43:         void addValidationProvider(ValidationProvider validationProvider);
44:
45:         /**
46:          * Adds a validation provider to the list of known validation providers.
47:          *
48:          * @param validationProvider the {@link ValidationProvider} to add
49:          * @param revalidate whether to revalidate the domain model after the provider has been added
50:          * @since 1.6
51:          */
52:         void addValidationProvider(ValidationProvider validationProvider, boolean revalidate);
53:
54:         /**
55:          * Removes a validation provider from the list of known validation providers.
56:          *
57:          * @param validationProvider the {@link ValidationProvider} to remove
58:          */
59:         void removeValidationProvider(ValidationProvider validationProvider);
60:
61:         /**
62:          * Removes a validation provider from the list of known validation providers. The domain model will be revalidated
63:          * after the provider has been removed
64:          *
65:          * @param validationProvider the {@link ValidationProvider} to remove
66:          * @param revalidate whether to revalidate the domain model after the provider has been removed
67:          * @since 1.6
68:          */
69:         void removeValidationProvider(ValidationProvider validationProvider, boolean revalidate);
70:
71:         /**
72:          * Registers a listener that will receive {@link org.eclipse.emf.common.util.Diagnostic Diagnostic}s with severity
73:          * higher than {@link org.eclipse.emf.common.util.Diagnostic Diagnostic#OK}. After
74:          * registration the listener's {@link ViewValidationListener#onNewValidation(java.util.Set)} will be called with
75:          * current
76:          * results.
77:          *
78:          * @param listener the listener to register
79:          */
80:         void registerValidationListener(ViewValidationListener listener);
81:
82:         /**
83:          * Deregisters the given listener.
84:          *
85:          * @param listener the listener to deregister
86:          */
87:         void deregisterValidationListener(ViewValidationListener listener);
88:
89:         /**
90:          * Validate the {@code objects} provided by an iterable.
91:          *
92:          * @param objects the iterable from which to obtain objects to validate
93:          *
94:          * @since 1.23
95:          */
96:         default void validate(Iterable<? extends EObject> objects) {
97:•                if (objects instanceof Collection<?>) {
98:                         // This should be safe because a correct implementation must never modify the collection
99:                         @SuppressWarnings("unchecked")
100:                         final Collection<EObject> collection = (Collection<EObject>) objects;
101:                         validate(collection);
102:                 } else {
103:                         // Create a new collection
104:                         final Collection<EObject> collection = StreamSupport.stream(objects.spliterator(), false)
105:                                 .collect(Collectors.toList());
106:                         validate(collection);
107:                 }
108:         }
109:
110:         /**
111:          * Validates all given eObjects.
112:          *
113:          * @param eObjects the eObjects to validate
114:          *
115:          * @see {@link #validate(Iterable)} for an alternative that is more convenient when validating
116:          * content trees covered by EMF {@code TreeIterator}s
117:          */
118:         void validate(Collection<EObject> eObjects);
119:
120:         /**
121:          * Obtain a substitution label provider suitable for rendering model elements as they
122:          * are presented in diagnostics produced by the {@link ValidationService}.
123:          *
124:          * @param context the view model context
125:          * @param adapterFactory an adapter factory to use to get item providers for model elements
126:          *
127:          * @return the substitution label provider
128:          *
129:          * @since 1.22
130:          */
131:         static SubstitutionLabelProvider getSubstitutionLabelProvider(EMFFormsViewContext context,
132:                 AdapterFactory adapterFactory) {
133:
134:                 SubstitutionLabelProvider result = null;
135:
136:                 // FIXME: This should not have to be a ComposedAdapterFactory
137:•                if (adapterFactory instanceof ComposedAdapterFactory
138:•                        && context instanceof ViewModelContext
139:•                        && ((ViewModelContext) context).hasService(ViewSubstitutionLabelProviderFactory.class)) {
140:
141:                         result = context.getService(ViewSubstitutionLabelProviderFactory.class)
142:                                 .createSubstitutionLabelProvider((ComposedAdapterFactory) adapterFactory);
143:                 }
144:
145:•                if (result == null) {
146:                         result = new ECPSubstitutionLabelProvider(adapterFactory);
147:                 }
148:
149:                 return result;
150:         }
151:
152: }