Skip to content

Package: ViewModelMarkerHelper$Provider

ViewModelMarkerHelper$Provider

nameinstructionbranchcomplexitylinemethod
ViewModelMarkerHelper.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%
createMarkerHelper(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%
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 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: * EclipseSource - initial API and implementation
13: * Christian W. Damus - bugs 544499, 548592
14: ******************************************************************************/
15: package org.eclipse.emfforms.ide.internal.builder;
16:
17: import org.eclipse.core.resources.IFile;
18: import org.eclipse.core.resources.IWorkspaceRunnable;
19: import org.eclipse.core.resources.ResourcesPlugin;
20: import org.eclipse.core.runtime.CoreException;
21: import org.eclipse.core.runtime.IProgressMonitor;
22: import org.eclipse.emf.common.ui.MarkerHelper;
23: import org.eclipse.emf.common.util.Diagnostic;
24: import org.eclipse.emf.ecp.view.spi.model.util.ViewValidator;
25: import org.eclipse.emfforms.bazaar.Bid;
26: import org.eclipse.emfforms.bazaar.Create;
27: import org.eclipse.emfforms.ide.builder.DefaultMarkerHelper;
28: import org.eclipse.emfforms.ide.builder.MarkerHelperProvider;
29: import org.osgi.service.component.annotations.Component;
30:
31: /**
32: * Specific Marker Helper that directly knows the file.
33: */
34: public class ViewModelMarkerHelper extends DefaultMarkerHelper {
35:         private final IFile file;
36:
37:         /** identifier of the marker, similar to plugin.xml value. */
38:         public static final String MARKER_ID = "org.eclipse.emfforms.ide.builder.ViewModelProblem"; //$NON-NLS-1$
39:
40:         @Override
41:         protected String getMarkerID() {
42:                 return MARKER_ID;
43:         }
44:
45:         /**
46:          * Constructor.
47:          *
48:          * @param file file being validated
49:          */
50:         ViewModelMarkerHelper(IFile file) {
51:                 this.file = file;
52:         }
53:
54:         @Override
55:         public void createMarkers(final Diagnostic diagnostic) throws CoreException {
56:                 ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
57:                         @Override
58:                         public void run(IProgressMonitor monitor) throws CoreException {
59:                                 final int severity = diagnostic.getSeverity();
60:                                 if (severity < Diagnostic.WARNING) {
61:                                         // neither info nor ok markers
62:                                         return;
63:                                 }
64:                                 // no child and severity ok, directly create marker.
65:                                 if (diagnostic.getChildren().isEmpty()) {
66:                                         createMarkers(getFile(diagnostic), diagnostic, null);
67:                                 } else {
68:                                         for (final Diagnostic child : diagnostic.getChildren()) {
69:                                                 // retrieve the most relevant child to get the right message: most relevant is a child
70:                                                 // diagnostic with the ECP diagnostic specific source and the right severity
71:                                                 final Diagnostic relevant = extractRelevantDiagnostic(child);
72:                                                 createMarkers(getFile(diagnostic), relevant, diagnostic);
73:                                         }
74:                                 }
75:
76:                         }
77:                 },
78:                         null);
79:         }
80:
81:         private Diagnostic extractRelevantDiagnostic(Diagnostic parentDiagnostic) {
82:                 if (parentDiagnostic.getSeverity() == 0 || parentDiagnostic.getChildren().isEmpty()) {
83:                         return parentDiagnostic;
84:                 }
85:
86:                 // try to get the most relevant => the one with highest severity, and the ECP validation source among the
87:                 // children with highest severity
88:                 Diagnostic result = parentDiagnostic.getChildren().get(0);
89:                 int currentHighest = result.getSeverity();
90:                 for (final Diagnostic child : parentDiagnostic.getChildren()) {
91:                         if (child.getSeverity() > currentHighest) {
92:                                 currentHighest = child.getSeverity();
93:                                 result = child;
94:                         } else if (child.getSeverity() == currentHighest
95:                                 && !ViewValidator.DIAGNOSTIC_SOURCE.equals(result.getSource())
96:                                 && ViewValidator.DIAGNOSTIC_SOURCE.equals(child.getSource())) {
97:                                 currentHighest = child.getSeverity();
98:                                 result = child;
99:                         }
100:                 }
101:                 return result;
102:         }
103:
104:         @Override
105:         protected IFile getFile(Object datum) {
106:                 if (file != null) {
107:                         return file;
108:                 }
109:                 return super.getFile(datum);
110:         }
111:
112:         //
113:         // Nested types
114:         //
115:
116:         /**
117:          * Implementation of the marker helper provider for view model validation.
118:          */
119:         @Component
120:         public static class Provider implements MarkerHelperProvider {
121:
122:                 /** Standard bid (for view model files). */
123:                 private static final Double BID = 10.0;
124:
125:                 /** View model file extension. */
126:                 private static final String VIEW = "view"; //$NON-NLS-1$
127:
128:                 /**
129:                  * Bid on view model files.
130:                  *
131:                  * @param file a file
132:                  * @return a bid, if it is a view model file
133:                  */
134:                 @Bid
135:                 public Double bid(IFile file) {
136:•                        return isViewModelResource(file) ? BID : null;
137:                 }
138:
139:                 /**
140:                  * Create the view model marker helper.
141:                  *
142:                  * @param file a view model file
143:                  * @return its marker helper
144:                  */
145:                 @Create
146:                 public MarkerHelper createMarkerHelper(IFile file) {
147:                         return new ViewModelMarkerHelper(file);
148:                 }
149:
150:                 private static boolean isViewModelResource(IFile resource) {
151:                         return VIEW.equals(resource.getFileExtension());
152:                 }
153:
154:         }
155:
156: }