Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 15663948926b57b679496cbc420afd6fc3f306bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
 * <copyright>
 *
 * Copyright (c) 2013 CEA LIST and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *	E.D.Willink (CEA LIST) - initial API and implementation
 *
 * </copyright>
 */
package org.eclipse.ocl.examples.emf.validation.validity.ui.providers;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.ocl.examples.emf.validation.validity.AbstractNode;
import org.eclipse.ocl.examples.emf.validation.validity.ConstrainingNode;
import org.eclipse.ocl.examples.emf.validation.validity.LeafConstrainingNode;
import org.eclipse.ocl.examples.emf.validation.validity.Result;
import org.eclipse.ocl.examples.emf.validation.validity.ResultConstrainingNode;
import org.eclipse.ocl.examples.emf.validation.validity.ResultValidatableNode;
import org.eclipse.ocl.examples.emf.validation.validity.Severity;
import org.eclipse.ocl.examples.emf.validation.validity.ValidatableNode;
import org.eclipse.ocl.examples.emf.validation.validity.ui.messages.ValidationDebugMessages;
import org.eclipse.ocl.examples.emf.validation.validity.ui.view.SeveritiesDecorator;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;

public class NodeLabelProvider extends ColumnLabelProvider
{
	protected class Summary
	{
		private int oks = 0;
		private int infos = 0;
		private int warnings = 0;
		private int errors = 0;
		private int fatals = 0;
		private int defaults = 0;
		
		public void accumulate(@NonNull EObject eObject) {
			Result worstResult = null;
			if (eObject instanceof ResultValidatableNode) {
				worstResult = ((ResultValidatableNode)eObject).getWorstResult();
			}
			else if (eObject instanceof ResultConstrainingNode) {
				worstResult = ((ResultConstrainingNode)eObject).getWorstResult();
			}
			if (worstResult != null) {
				Severity severity = worstResult.getSeverity();
				switch (severity) {
				case OK: oks++; break;
				case INFO: infos++; break;
				case WARNING: warnings++; break;
				case ERROR: errors++; break;
				case FATAL: fatals++; break;
				default: defaults++; break;
				}
			}
		}
		
		public @NonNull String toString() {
			String separator = ", "; //"\n";
			StringBuilder s = new StringBuilder();
			s.append(oks + " ok" + (oks != 1 ? "s" : ""));
			if (infos > 0) {
				s.append(separator + infos + " info" + (oks != 1 ? "s" : ""));
			}
			if (warnings > 0) {
				s.append(separator + warnings + " warning" + (warnings != 1 ? "s" : ""));
			}
			if (errors > 0) {
				s.append(separator + errors + " error" + (errors != 1 ? "s" : ""));
			}
			if (fatals > 0) {
				s.append(separator + fatals + " fatal" + (fatals != 1 ? "s" : ""));
			}
			if (defaults > 0) {
				s.append(separator + defaults + " other" + (defaults != 1 ? "s" : ""));
			}
			@SuppressWarnings("null")@NonNull String string = s.toString();
			return string;
		}
	}
	
	private final @NonNull ILabelProvider labelProvider;
	private final Color validatableColor;
	private final Color constrainingNodeColor;

	public NodeLabelProvider(@NonNull ILabelProvider labelProvider, Color validatableColor, Color constrainingNodeColor) {
		this.labelProvider = labelProvider;
		this.validatableColor = validatableColor;
		this.constrainingNodeColor = constrainingNodeColor;
	}

	@Override
	public void addListener(ILabelProviderListener listener) {
		labelProvider.addListener(listener);
	}

	protected void appendResourceDiagnostic(@NonNull Writer s, @NonNull Diagnostic diagnostic) {
		boolean isFirst = true;
		for (Diagnostic child : diagnostic.getChildren()) {
			try {
				if (isFirst) {
					s.append(child.getMessage());
					isFirst = false;
				} else {
					s.append("\n" + child.getMessage());
				}
			} catch (IOException e) {}
		}
	}

	public Color getBackground(Object element) {
		return null;
	}

	public Font getFont(Object element) {
		if (element instanceof ResultConstrainingNode) {
			return null;
		}
		else if (element instanceof ResultValidatableNode) {
			return JFaceResources.getFontRegistry().getItalic(JFaceResources.DEFAULT_FONT);
		}
		else if (element instanceof ConstrainingNode) {
			return JFaceResources.getFontRegistry().getItalic(JFaceResources.DEFAULT_FONT);
		}
		else {
			return null;
		}
	}

	public Color getForeground(Object element) {
		if (element instanceof ResultConstrainingNode) {
			return validatableColor;
		}
		else if (element instanceof ResultValidatableNode) {
			return constrainingNodeColor;
		}
		else if (element instanceof ConstrainingNode) {
			return constrainingNodeColor;
		}
		else {
			return validatableColor;
		}
	}

	@Override
	public Image getImage(Object element) {
		if (element instanceof ResultValidatableNode) {
			ConstrainingNode constrainingNode = ((ResultValidatableNode)element).getResultConstrainingNode().getParent();
			if (constrainingNode instanceof LeafConstrainingNode) {
				Object image = ((LeafConstrainingNode)constrainingNode).getConstraintLocator().getImage();
				return ExtendedImageRegistry.INSTANCE.getImage(image);
			}
			else {
				return labelProvider.getImage(constrainingNode.getConstrainingObject());
			}
		}
		else if (element instanceof ResultConstrainingNode) {
			ConstrainingNode constrainingNode = (ConstrainingNode) ((ResultConstrainingNode)element).getParent();
			if (constrainingNode instanceof LeafConstrainingNode) {
				Object image = ((LeafConstrainingNode)constrainingNode).getConstraintLocator().getImage();
				return ExtendedImageRegistry.INSTANCE.getImage(image);
			}
			else {
				return labelProvider.getImage(constrainingNode.getConstrainingObject());
			}
		}
		else if (element instanceof ConstrainingNode) {
			return labelProvider.getImage(((ConstrainingNode)element).getConstrainingObject());
		}
		else if (element instanceof ValidatableNode) {
			return labelProvider.getImage(((ValidatableNode)element).getConstrainedObject());
		}
		else {
			return labelProvider.getImage(element);
		}
	}

	protected @NonNull String getResultToolTip(@Nullable Result result) {
		if (result == null) {
			return "No result available";
		}
		if (result.getSeverity() == Severity.OK) {
			return "Successful";
		}
		StringWriter s = new StringWriter();
		Object diagnostic = result.getDiagnostic();
		if (diagnostic == null) {
			s.append("<<null diagnostic message>>");
		}
		else if (diagnostic instanceof Diagnostic) {
			appendResourceDiagnostic(s, (Diagnostic)diagnostic);
		}
		else {
			s.append(String.valueOf(diagnostic));
		}
		Exception exception = result.getException();
		if (exception != null) {
			s.append("\n" + exception.getClass().getName() + ":\n");
			exception.printStackTrace(new PrintWriter(s));	
		}
		@SuppressWarnings("null")@NonNull String string = s.toString();
		return string;
	}

	protected @NonNull String getSummaryToolTip(@NonNull AbstractNode node) {
		Summary summary = new Summary();
		summary.accumulate(node);
		for (TreeIterator<EObject> tit = node.eAllContents(); tit.hasNext(); ) {
			@SuppressWarnings("null")@NonNull EObject eObject = tit.next();
			summary.accumulate(eObject);
		}
		return summary.toString();
	}

	@Override
	public String getText(Object element) {
		return ((AbstractNode)element).getLabel();
	}

	@Override
	public Image getToolTipImage(Object object) {
		Object severityImage = SeveritiesDecorator.getSeverityImage(object);
		return ExtendedImageRegistry.INSTANCE.getImage(severityImage);
	}

	@Override
	public @Nullable String getToolTipText(Object element) {
		if (element instanceof LeafConstrainingNode) {
			LeafConstrainingNode leafConstrainingNode = ((LeafConstrainingNode) element);
			return getLeafConstrainingNodeHoover(leafConstrainingNode, false);
		} else if (element instanceof ResultConstrainingNode) {
			return getResultToolTip(((ResultConstrainingNode) element)
				.getWorstResult());
		} else if (element instanceof ResultValidatableNode) {
			Result result = ((ResultValidatableNode) element).getWorstResult();
			if (result != null) {
				LeafConstrainingNode leafConstrainingNode = result
					.getLeafConstrainingNode();
				return getLeafConstrainingNodeHoover(leafConstrainingNode, true);
			} else {
				ConstrainingNode contrainingNode = ((ResultValidatableNode) element).getResultConstrainingNode().getParent();
				if (contrainingNode instanceof LeafConstrainingNode){
					LeafConstrainingNode leafConstrainingNode = ((LeafConstrainingNode) contrainingNode);
					return getLeafConstrainingNodeHoover(leafConstrainingNode, true);
				}
				return getResultToolTip(result);
			}
		} else if (element instanceof AbstractNode) {
			return getSummaryToolTip((AbstractNode) element);
		} else {
			return "Unknown";
		}
	}

	private String getLeafConstrainingNodeHoover(
			LeafConstrainingNode leafConstrainingNode, boolean withDiagnosisMessage) {
		StringBuilder s = new StringBuilder();
		Resource resource = leafConstrainingNode.getConstraintResource();
		s.append("Location: ");
		if (resource != null) {
			s.append(resource.getURI().toString());
		} else {
			s.append(ValidationDebugMessages.ValidityView_Constraints_LabelProvider_UnexistingResource);
		}
		
		String expression = leafConstrainingNode.getConstraintString();
		s.append("\nExpression: ");
		if (expression != null) {
			s.append(expression);
		} else {
			s.append(ValidationDebugMessages.ValidityView_Constraints_LabelProvider_UnattainableExpression);
		}
		
		if (withDiagnosisMessage) {
			s.append("\nEvaluation Result: ");
			s.append(getResultToolTip(leafConstrainingNode.getWorstResult()));
		}
		return s.toString();
	}

	public int getToolTipTimeDisplayed(Object object) {
		return 15000;
	}

	@Override
	public boolean isLabelProperty(Object element, String property) {
		if (element instanceof ConstrainingNode) {
			return labelProvider.isLabelProperty(((ConstrainingNode)element).getConstrainingObject(), property);
		}
		else if (element instanceof ValidatableNode) {
			return labelProvider.isLabelProperty(((ValidatableNode)element).getConstrainedObject(), property);
		}
		else {
			return labelProvider.isLabelProperty(element, property);
		}
	}

	@Override
	public void removeListener(ILabelProviderListener listener) {
		labelProvider.removeListener(listener);
	}
}

Back to the top