Skip to content

Package: ViewModelValidationDelegate$Provider

ViewModelValidationDelegate$Provider

nameinstructionbranchcomplexitylinemethod
ViewModelValidationDelegate.Provider()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
bid(IFile)
M: 0 C: 7
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createValidationDelegate()
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%
isViewModelResource(IFile)
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%
static {...}
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) 2019 Christian W. Damus 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: * Christian W. Damus - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.ide.internal.builder;
15:
16: import java.io.IOException;
17: import java.util.LinkedHashSet;
18: import java.util.Set;
19:
20: import org.eclipse.core.resources.IFile;
21: import org.eclipse.emf.common.notify.Notifier;
22: import org.eclipse.emf.common.notify.impl.AdapterImpl;
23: import org.eclipse.emf.common.util.URI;
24: import org.eclipse.emf.ecore.EObject;
25: import org.eclipse.emf.ecore.resource.Resource;
26: import org.eclipse.emf.ecore.resource.ResourceSet;
27: import org.eclipse.emf.ecore.util.EcoreUtil;
28: import org.eclipse.emf.ecp.ide.spi.util.EcoreHelper;
29: import org.eclipse.emf.ecp.ide.spi.util.ViewModelHelper;
30: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
31: import org.eclipse.emf.ecp.view.spi.model.VView;
32: import org.eclipse.emf.ecp.view.spi.model.VViewModelProperties;
33: import org.eclipse.emf.ecp.view.spi.model.util.ViewModelPropertiesHelper;
34: import org.eclipse.emfforms.bazaar.Bid;
35: import org.eclipse.emfforms.bazaar.Create;
36: import org.eclipse.emfforms.common.spi.validation.ValidationService;
37: import org.eclipse.emfforms.ide.builder.ValidationDelegate;
38: import org.eclipse.emfforms.ide.builder.ValidationDelegateProvider;
39: import org.eclipse.emfforms.ide.builder.ValidationServiceDelegate;
40: import org.osgi.service.component.annotations.Component;
41:
42: /**
43: * The validation delegate for view models.
44: */
45: public class ViewModelValidationDelegate extends ValidationServiceDelegate {
46:
47:         /**
48:          * Initializes me.
49:          */
50:         public ViewModelValidationDelegate() {
51:                 super();
52:         }
53:
54:         @Override
55:         protected ResourceSet loadModel(IFile file) throws IOException {
56:                 ResourceSet result = null;
57:
58:                 final LinkedHashSet<String> ecores = new LinkedHashSet<String>();
59:
60:                 // load file thanks to ECP helpers to avoid missing Properties
61:                 final VView view = ViewModelHelper.loadView(file, ecores);
62:
63:                 final ViewAdapter adapter = new ViewAdapter(view, ecores);
64:
65:                 if (view == null) {
66:                         adapter.dispose();
67:                 } else {
68:                         view.eAdapters().add(adapter);
69:
70:                         // the ViewModelHelper forces file: scheme URI, which is bad
71:                         // for marker navigation
72:                         final Resource resource = view.eResource();
73:                         resource.setURI(URI.createPlatformResourceURI(file.getFullPath().toString(), true));
74:
75:                         result = resource.getResourceSet();
76:                         result.eAdapters().add(adapter);
77:                 }
78:
79:                 return result;
80:         }
81:
82:         @Override
83:         protected EObject getModel(ResourceSet resourceSet) {
84:                 return ViewAdapter.getView(resourceSet);
85:         }
86:
87:         @Override
88:         protected void configure(ValidationService validationService, ResourceSet resourceSet, EObject model) {
89:                 super.configure(validationService, resourceSet, model);
90:
91:                 final VView view = (VView) model;
92:
93:                 final VViewModelProperties properties = ViewModelPropertiesHelper.getInhertitedPropertiesOrEmpty(view);
94:                 view.setLoadingProperties(properties);
95:
96:                 validationService.addObjectFilter(this::skipValidation);
97:         }
98:
99:         private boolean skipValidation(EObject eObject) {
100:                 return VDomainModelReference.class.isInstance(eObject);
101:         }
102:
103:         //
104:         // Nested types
105:         //
106:
107:         /**
108:          * Implementation of the validation delegate provider for view model validation.
109:          */
110:         @Component
111:         public static class Provider implements ValidationDelegateProvider {
112:
113:                 /** Standard bid (for view model files). */
114:                 private static final Double BID = 10.0;
115:
116:                 /** View model file extension. */
117:                 private static final String VIEW = "view"; //$NON-NLS-1$
118:
119:                 /**
120:                  * Bid on view model files.
121:                  *
122:                  * @param file a file
123:                  * @return a bid, if it is a view model file
124:                  */
125:                 @Bid
126:                 public Double bid(IFile file) {
127:•                        return isViewModelResource(file) ? BID : null;
128:                 }
129:
130:                 /**
131:                  * Create the view model validation delegate.
132:                  *
133:                  * @return the view model validation delegate
134:                  */
135:                 @Create
136:                 public ValidationDelegate createValidationDelegate() {
137:                         return new ViewModelValidationDelegate();
138:                 }
139:
140:                 private static boolean isViewModelResource(IFile resource) {
141:                         return VIEW.equals(resource.getFileExtension());
142:                 }
143:
144:         }
145:
146:         /**
147:          * An adapter that just attaches the list of registered Ecore paths
148:          * to the model to find after validation for deregistration.
149:          */
150:         private static final class ViewAdapter extends AdapterImpl {
151:                 private final Set<String> ecores;
152:                 private final VView view;
153:
154:                 ViewAdapter(VView view, Set<String> ecores) {
155:                         super();
156:
157:                         this.view = view;
158:                         this.ecores = ecores;
159:                 }
160:
161:                 @Override
162:                 public boolean isAdapterForType(Object type) {
163:                         return type == VView.class || type == ViewAdapter.class;
164:                 }
165:
166:                 @Override
167:                 public void unsetTarget(Notifier oldTarget) {
168:                         super.unsetTarget(oldTarget);
169:
170:                         dispose();
171:                 }
172:
173:                 VView getView() {
174:                         return view;
175:                 }
176:
177:                 void dispose() {
178:                         for (final String registeredEcore : ecores) {
179:                                 EcoreHelper.unregisterEcore(registeredEcore);
180:                         }
181:                         ecores.clear();
182:                 }
183:
184:                 static VView getView(ResourceSet resourceSet) {
185:                         final ViewAdapter adapter = (ViewAdapter) EcoreUtil.getExistingAdapter(resourceSet, ViewAdapter.class);
186:                         return adapter == null ? null : adapter.getView();
187:                 }
188:
189:         }
190:
191: }