Skip to content

Package: SWTValidationUiServiceImpl

SWTValidationUiServiceImpl

nameinstructionbranchcomplexitylinemethod
SWTValidationUiServiceImpl()
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
SWTValidationUiServiceImpl(SWTValidationHelper)
M: 0 C: 24
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
getValidationBackgroundColor(Diagnostic, VElement, ViewModelContext)
M: 0 C: 30
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
getValidationBackgroundColor(VElement, ViewModelContext)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getValidationForegroundColor(Diagnostic, VElement, ViewModelContext)
M: 0 C: 30
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
getValidationForegroundColor(VElement, ViewModelContext)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getValidationIcon(Diagnostic, VElement, ViewModelContext)
M: 0 C: 30
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getValidationIcon(VElement, ViewModelContext)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
highestSeverityDiagnostic(VElement)
M: 0 C: 36
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
severity(Diagnostic)
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%

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: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.swt.core.ui;
15:
16: import java.util.HashMap;
17: import java.util.Map;
18:
19: import org.eclipse.emf.common.util.Diagnostic;
20: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
21: import org.eclipse.emf.ecp.view.spi.model.VDiagnostic;
22: import org.eclipse.emf.ecp.view.spi.model.VElement;
23: import org.eclipse.emfforms.spi.swt.core.ui.SWTValidationHelper;
24: import org.eclipse.emfforms.spi.swt.core.ui.SWTValidationUiService;
25: import org.eclipse.swt.graphics.Color;
26: import org.eclipse.swt.graphics.Image;
27: import org.osgi.service.component.annotations.Component;
28:
29: /**
30: * Default implementation of the {@link SWTValidationUiService} which delegates to the {@link SWTValidationHelper} to
31: * get the validation icons and colors.
32: *
33: * @author Lucas Koehler
34: *
35: */
36: @Component
37: public class SWTValidationUiServiceImpl implements SWTValidationUiService {
38:
39:         private final Map<Integer, Color> severityBackgroundColorMap = new HashMap<Integer, Color>();
40:         private final Map<Integer, Color> severityForegroundColorMap = new HashMap<Integer, Color>();
41:         private final Map<Integer, Image> severityIconMap = new HashMap<Integer, Image>();
42:         private SWTValidationHelper validationHelper = SWTValidationHelper.INSTANCE;
43:
44:         /** Default constructor. */
45:         public SWTValidationUiServiceImpl() {
46:                 // Nothing to do here
47:         }
48:
49:         /**
50:          * Test constructor that allows specifying a custom {@link SWTValidationHelper}.
51:          *
52:          * @param validationHelper The custom {@link SWTValidationHelper}
53:          */
54:         SWTValidationUiServiceImpl(SWTValidationHelper validationHelper) {
55:                 this.validationHelper = validationHelper;
56:         }
57:
58:         @Override
59:         public Image getValidationIcon(Diagnostic diagnostic, VElement vElement, ViewModelContext viewModelContext) {
60:                 final int severity = severity(diagnostic);
61:•                if (!severityIconMap.containsKey(severity)) {
62:                         final Image validationIcon = validationHelper.getValidationIcon(severity, vElement, viewModelContext);
63:                         severityIconMap.put(severity, validationIcon);
64:                 }
65:                 return severityIconMap.get(severity);
66:         }
67:
68:         @Override
69:         public Image getValidationIcon(VElement vElement, ViewModelContext viewModelContext) {
70:                 return getValidationIcon(highestSeverityDiagnostic(vElement), vElement, viewModelContext);
71:         }
72:
73:         @Override
74:         public Color getValidationForegroundColor(Diagnostic diagnostic, VElement vElement,
75:                 ViewModelContext viewModelContext) {
76:                 final int severity = severity(diagnostic);
77:•                if (!severityForegroundColorMap.containsKey(severity)) {
78:                         final Color validationForegroundColor = validationHelper.getValidationForegroundColor(severity, vElement,
79:                                 viewModelContext);
80:                         severityForegroundColorMap.put(severity, validationForegroundColor);
81:                 }
82:                 return severityForegroundColorMap.get(severity);
83:         }
84:
85:         @Override
86:         public Color getValidationForegroundColor(VElement vElement, ViewModelContext viewModelContext) {
87:                 return getValidationForegroundColor(highestSeverityDiagnostic(vElement), vElement, viewModelContext);
88:         }
89:
90:         @Override
91:         public Color getValidationBackgroundColor(Diagnostic diagnostic, VElement vElement,
92:                 ViewModelContext viewModelContext) {
93:                 final int severity = severity(diagnostic);
94:•                if (!severityBackgroundColorMap.containsKey(severity)) {
95:                         final Color validationBackgroundColor = validationHelper.getValidationBackgroundColor(severity, vElement,
96:                                 viewModelContext);
97:                         severityBackgroundColorMap.put(severity, validationBackgroundColor);
98:                 }
99:                 return severityBackgroundColorMap.get(severity);
100:         }
101:
102:         @Override
103:         public Color getValidationBackgroundColor(VElement vElement, ViewModelContext viewModelContext) {
104:                 return getValidationBackgroundColor(highestSeverityDiagnostic(vElement), vElement, viewModelContext);
105:         }
106:
107:         private static Diagnostic highestSeverityDiagnostic(VElement element) {
108:                 Diagnostic mostSevere = Diagnostic.OK_INSTANCE;
109:                 final VDiagnostic vDiagnostic = element.getDiagnostic();
110:•                if (vDiagnostic != null && vDiagnostic.getDiagnostics().size() > 0) {
111:•                        for (final Object o : vDiagnostic.getDiagnostics()) {
112:                                 final Diagnostic diagnostic = (Diagnostic) o;
113:•                                mostSevere = mostSevere.getSeverity() >= diagnostic.getSeverity() ? mostSevere : diagnostic;
114:                         }
115:                 }
116:                 return mostSevere;
117:         }
118:
119:         /** Wrap getting a Diagnostic's severity to make the call null-safe. */
120:         private static int severity(Diagnostic diagnostic) {
121:                 // If there is no diagnostic, we assume everything is ok.
122:•                return diagnostic != null ? diagnostic.getSeverity() : Diagnostic.OK;
123:         }
124: }