Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dcf9ce499feb1933b9ceff7ed60879ac81e6e806 (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/******************************************************************************
 * Copyright (c) 2007, 2020 Borland Software Corporation, CEA LIST, Artal
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/ 
 * 
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors: 
 *    Dmitry Stadnik (Borland) - initial API and implementation
 *    Aurelien Didier (ARTAL) - aurelien.didier51@gmail.com - Bug 569174
 *****************************************************************************/

«IMPORT 'http://www.eclipse.org/papyrus/gmf/2020/GenModel'»

«DEFINE ValidationDecoratorProvider FOR gmfgen::GenDiagram-»
«EXPAND xpt::Common::copyright FOR editorGen-»
package «providersPackageName»;

«EXPAND xpt::Common::generatedClassComment»
public class «validationDecoratorProviderClassName»
		extends org.eclipse.gmf.runtime.common.core.service.AbstractProvider
		implements org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoratorProvider {

	«EXPAND xpt::Common::generatedMemberComment»
	private static final String KEY = "validationStatus"; «EXPAND xpt::Common::nonNLS»
	«IF editorGen.application = null-»

	«EXPAND xpt::Common::generatedMemberComment»
	private static final String MARKER_TYPE = «editorGen.plugin.getActivatorQualifiedClassName()».ID +
			".«getValidationDiagnosticMarkerType()»"; «EXPAND xpt::Common::nonNLS»

	«EXPAND xpt::Common::generatedMemberComment»
	private static MarkerObserver fileObserver;
	«ENDIF-»

	«EXPAND xpt::Common::generatedMemberComment»
	private static java.util.Map/*<String, List<IDecorator>>*/ allDecorators = new java.util.HashMap();

	«EXPAND xpt::Common::generatedMemberComment»
	public void createDecorators(org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoratorTarget decoratorTarget) {
		org.eclipse.gef.EditPart editPart = (org.eclipse.gef.EditPart) decoratorTarget.getAdapter(org.eclipse.gef.EditPart.class);
		if (editPart instanceof org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart ||
				editPart instanceof org.eclipse.gef.editparts.AbstractConnectionEditPart) {
			Object model = editPart.getModel();
			if ((model instanceof org.eclipse.gmf.runtime.notation.View)) {
				org.eclipse.gmf.runtime.notation.View view = (org.eclipse.gmf.runtime.notation.View) model;
				if (!(view instanceof org.eclipse.gmf.runtime.notation.Edge) && !view.isSetElement()) {
					return;
				}
			}
			org.eclipse.gef.EditDomain ed = editPart.getViewer().getEditDomain();
			if (!(ed instanceof org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditDomain)) {
				return;
			}
			if (((org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditDomain) ed).getEditorPart() instanceof
					«editorGen.editor.getQualifiedClassName()») {
				decoratorTarget.installDecorator(KEY, new StatusDecorator(decoratorTarget));
			}
		}
	}

	«EXPAND xpt::Common::generatedMemberComment»
	public boolean provides(org.eclipse.gmf.runtime.common.core.service.IOperation operation) {
		if (!(operation instanceof org.eclipse.gmf.runtime.diagram.ui.services.decorator.CreateDecoratorsOperation)) {
			return false;
		}
		org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoratorTarget decoratorTarget =
				((org.eclipse.gmf.runtime.diagram.ui.services.decorator.CreateDecoratorsOperation) operation).getDecoratorTarget();
		org.eclipse.gmf.runtime.notation.View view = (org.eclipse.gmf.runtime.notation.View) decoratorTarget.getAdapter(
				org.eclipse.gmf.runtime.notation.View.class);
		return view != null && «EXPAND xpt::editor::VisualIDRegistry::modelID».equals(«EXPAND xpt::editor::VisualIDRegistry::getModelIDMethodCall»(view));
	}

	«EXPAND xpt::Common::generatedMemberComment»
	public static void refreshDecorators(org.eclipse.gmf.runtime.notation.View view) {
		refreshDecorators(org.eclipse.gmf.runtime.diagram.core.util.ViewUtil.getIdStr(view), view.getDiagram());
	}

	«EXPAND xpt::Common::generatedMemberComment»
	private static void refreshDecorators(String viewId, org.eclipse.gmf.runtime.notation.Diagram diagram) {
		final java.util.List decorators = viewId != null ? (java.util.List) allDecorators.get(viewId) : null;
		if (decorators == null || decorators.isEmpty() || diagram == null) {
			return;
		}
		final org.eclipse.gmf.runtime.notation.Diagram fdiagram = diagram;
		org.eclipse.ui.PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {

			public void run() {
				try {
					org.eclipse.emf.transaction.util.TransactionUtil.getEditingDomain(fdiagram).runExclusive(new Runnable() {

						public void run() {
							for (java.util.Iterator it = decorators.iterator(); it.hasNext();) {
								org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecorator decorator =
										(org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecorator) it.next();
								decorator.refresh();
							}
						}
					});
				} catch (Exception e) {
					«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError(
							"Decorator refresh failure", e); «EXPAND xpt::Common::nonNLS»
				}
			}
		});
	}

	«EXPAND xpt::Common::generatedMemberComment»
	public static class StatusDecorator extends org.eclipse.gmf.runtime.diagram.ui.services.decorator.AbstractDecorator {

		«EXPAND xpt::Common::generatedMemberComment»
		private String viewId;

		«EXPAND xpt::Common::generatedMemberComment»
		public StatusDecorator(org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoratorTarget decoratorTarget) {
			super(decoratorTarget);
			try {
				final org.eclipse.gmf.runtime.notation.View view = (org.eclipse.gmf.runtime.notation.View) getDecoratorTarget().getAdapter(
						org.eclipse.gmf.runtime.notation.View.class);
				org.eclipse.emf.transaction.util.TransactionUtil.getEditingDomain(view).runExclusive(new Runnable() {

					public void run() {
						StatusDecorator.this.viewId = view != null ?
								org.eclipse.gmf.runtime.diagram.core.util.ViewUtil.getIdStr(view) : null;
					}
				});
			} catch (Exception e) {
				«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError(
						"ViewID access failure", e); «EXPAND xpt::Common::nonNLS»			
			}
		}

		«EXPAND xpt::Common::generatedMemberComment»
		public void refresh() {
			removeDecoration();
			org.eclipse.gmf.runtime.notation.View view = (org.eclipse.gmf.runtime.notation.View) getDecoratorTarget().getAdapter(
					org.eclipse.gmf.runtime.notation.View.class);
			if (view == null || view.eResource() == null) {
				return;
			}
			org.eclipse.gef.EditPart editPart = (org.eclipse.gef.EditPart) getDecoratorTarget().getAdapter(org.eclipse.gef.EditPart.class);
			if (editPart == null || editPart.getViewer() == null) {
				return;
			}

			// query for all the validation markers of the current resource
			«IF editorGen.application = null-»
			String elementId = org.eclipse.gmf.runtime.diagram.core.util.ViewUtil.getIdStr(view);
			if (elementId == null) {
				return;
			}
			int severity = org.eclipse.core.resources.IMarker.SEVERITY_INFO;
			org.eclipse.core.resources.IMarker foundMarker = null;
			org.eclipse.core.resources.IResource resource = org.eclipse.emf.workspace.util.WorkspaceSynchronizer.getFile(view.eResource());
			if (resource == null || !resource.exists()) {
				return;
			}
			org.eclipse.core.resources.IMarker[] markers = null;
			try {
				markers = resource.findMarkers(MARKER_TYPE, true, org.eclipse.core.resources.IResource.DEPTH_INFINITE);
			} catch (org.eclipse.core.runtime.CoreException e) {
				«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError(
						"Validation markers refresh failure", e); «EXPAND xpt::Common::nonNLS»
			}
			«ELSE-»
			int severity = org.eclipse.core.runtime.IStatus.INFO;
			«EXPAND xpt::editor::ValidationMarker::qualifiedClassName» foundMarker = null;
			«EXPAND xpt::editor::ValidationMarker::qualifiedClassName»[] markers =
					«EXPAND xpt::editor::ValidationMarker::qualifiedClassName».getMarkers(editPart.getViewer(), viewId);
			«ENDIF-»
			if (markers == null || markers.length == 0) {
				return;
			}
			org.eclipse.draw2d.Label toolTip = null;
			for (int i = 0; i < markers.length; i++) {
				«IF editorGen.application = null-»
				org.eclipse.core.resources.IMarker marker = markers[i];
				String attribute = marker.getAttribute(org.eclipse.gmf.runtime.common.ui.resources.IMarker.ELEMENT_ID, ""); «EXPAND xpt::Common::nonNLS»
				if (attribute.equals(elementId)) {
					int nextSeverity = marker.getAttribute(org.eclipse.core.resources.IMarker.SEVERITY,
							org.eclipse.core.resources.IMarker.SEVERITY_INFO);					
				«ELSE-»
				«EXPAND xpt::editor::ValidationMarker::qualifiedClassName» marker = markers[i];
				int nextSeverity = marker.getStatusSeverity();
				«ENDIF-»
					org.eclipse.swt.graphics.Image nextImage = getImage(nextSeverity);	 
					if (foundMarker == null) {				
						foundMarker = marker;
						toolTip = new org.eclipse.draw2d.Label(
							«IF editorGen.application = null-»
								marker.getAttribute(org.eclipse.core.resources.IMarker.MESSAGE, ""), «EXPAND xpt::Common::nonNLS»
							«ELSE-»
								marker.getMessage(),
							«ENDIF-»
								nextImage);						
					} else {
						if (toolTip.getChildren().isEmpty()) {
							org.eclipse.draw2d.Label comositeLabel = new org.eclipse.draw2d.Label();
							org.eclipse.draw2d.FlowLayout fl = new org.eclipse.draw2d.FlowLayout(false);
							fl.setMinorSpacing(0);
							comositeLabel.setLayoutManager(fl);
							comositeLabel.add(toolTip);
							toolTip = comositeLabel;
						}
						toolTip.add(new org.eclipse.draw2d.Label(
							«IF editorGen.application = null-»
								marker.getAttribute(org.eclipse.core.resources.IMarker.MESSAGE, ""),  «EXPAND xpt::Common::nonNLS»
							«ELSE-»
								marker.getMessage(),
							«ENDIF-»
								nextImage));
					}
					severity = (nextSeverity > severity) ? nextSeverity : severity;					
				«IF editorGen.application = null-»
				}
				«ENDIF-»
			}
			if (foundMarker == null) {
				return;
			}

			// add decoration
			if (editPart instanceof org.eclipse.gef.GraphicalEditPart) {
				if (view instanceof org.eclipse.gmf.runtime.notation.Edge) {
					setDecoration(getDecoratorTarget().addConnectionDecoration(getImage(severity), 50, true));										
				} else {
					int margin = -1;
					if (editPart instanceof org.eclipse.gef.GraphicalEditPart) {
						margin = org.eclipse.gmf.runtime.draw2d.ui.mapmode.MapModeUtil.getMapMode(
							((org.eclipse.gef.GraphicalEditPart) editPart).getFigure()).DPtoLP(margin);
					}
					setDecoration(getDecoratorTarget().addShapeDecoration(getImage(severity),
							org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoratorTarget.Direction.NORTH_EAST, margin, true));										
				}
				getDecoration().setToolTip(toolTip);
			}
		}

		«EXPAND xpt::Common::generatedMemberComment»
		private org.eclipse.swt.graphics.Image getImage(int severity) {
			String imageName = org.eclipse.ui.ISharedImages.IMG_OBJS_ERROR_TSK;
			switch (severity) {
			«IF editorGen.application = null-»
			case org.eclipse.core.resources.IMarker.SEVERITY_ERROR:
				imageName = org.eclipse.ui.ISharedImages.IMG_OBJS_ERROR_TSK;
				break;
			case org.eclipse.core.resources.IMarker.SEVERITY_WARNING:
				imageName = org.eclipse.ui.ISharedImages.IMG_OBJS_WARN_TSK;
				break;
			«ELSE-»
			case org.eclipse.core.runtime.IStatus.ERROR:
				imageName = org.eclipse.ui.ISharedImages.IMG_OBJS_ERROR_TSK;
				break;
			case org.eclipse.core.runtime.IStatus.WARNING:
				imageName = org.eclipse.ui.ISharedImages.IMG_OBJS_WARN_TSK;
				break;
			«ENDIF-»
			default:
				imageName = org.eclipse.ui.ISharedImages.IMG_OBJS_INFO_TSK;
			}
			return org.eclipse.ui.PlatformUI.getWorkbench().getSharedImages().getImage(imageName);
		}

		«EXPAND xpt::Common::generatedMemberComment»
		public void activate() {
			if (viewId == null) {
				return;
			}

			// add self to global decorators registry
			java.util.List list = (java.util.List) allDecorators.get(viewId);
			if (list == null) {
				list = new java.util.ArrayList(2);
				list.add(this);
				allDecorators.put(viewId, list);
			} else if (!list.contains(this)) {
				list.add(this);
			}
			«IF editorGen.application = null-»

			// start listening to changes in resources
			org.eclipse.gmf.runtime.notation.View view = (org.eclipse.gmf.runtime.notation.View) getDecoratorTarget().getAdapter(
					org.eclipse.gmf.runtime.notation.View.class);
			if (view == null) {
				return;
			}
			org.eclipse.gmf.runtime.notation.Diagram diagramView = view.getDiagram();
			if (diagramView == null) {
				return;
			}
			if (fileObserver == null) {
				org.eclipse.gmf.runtime.common.ui.resources.FileChangeManager.getInstance().addFileObserver(
						fileObserver = new MarkerObserver(diagramView));
			}
			«ENDIF-»
		}

		«EXPAND xpt::Common::generatedMemberComment»
		public void deactivate() {
			if (viewId == null) {
				return;
			}

			// remove self from global decorators registry
			java.util.List list = (java.util.List) allDecorators.get(viewId);
			if (list != null) {
				list.remove(this);
				if (list.isEmpty()) {
					allDecorators.remove(viewId);
				}
			}
			«IF editorGen.application = null-»

			// stop listening to changes in resources if there are no more decorators
			if (fileObserver != null && allDecorators.isEmpty()) {
				org.eclipse.gmf.runtime.common.ui.resources.FileChangeManager.getInstance().removeFileObserver(fileObserver);
				fileObserver = null;
			}
			«ENDIF-»
			super.deactivate();
		}
	}
	«IF editorGen.application = null-»

	«EXPAND xpt::Common::generatedMemberComment»
	static class MarkerObserver implements org.eclipse.gmf.runtime.common.ui.resources.IFileObserver {

		«EXPAND xpt::Common::generatedMemberComment»
		private org.eclipse.gmf.runtime.notation.Diagram diagram;

		«EXPAND xpt::Common::generatedMemberComment»
		private MarkerObserver(org.eclipse.gmf.runtime.notation.Diagram diagram) {
			this.diagram = diagram;
		}

		«EXPAND xpt::Common::generatedMemberComment»
		public void handleFileRenamed(org.eclipse.core.resources.IFile oldFile, org.eclipse.core.resources.IFile file) {
		}

		«EXPAND xpt::Common::generatedMemberComment»
		public void handleFileMoved(org.eclipse.core.resources.IFile oldFile, org.eclipse.core.resources.IFile file) {
		}

		«EXPAND xpt::Common::generatedMemberComment»
		public void handleFileDeleted(org.eclipse.core.resources.IFile file) {
		}

		«EXPAND xpt::Common::generatedMemberComment»
		public void handleFileChanged(org.eclipse.core.resources.IFile file) {
		}

		«EXPAND xpt::Common::generatedMemberComment»
		public void handleMarkerAdded(org.eclipse.core.resources.IMarker marker) {
			if (marker.getAttribute(org.eclipse.gmf.runtime.common.ui.resources.IMarker.ELEMENT_ID, null) != null) {
				handleMarkerChanged(marker);
			}
		}

		«EXPAND xpt::Common::generatedMemberComment»
		public void handleMarkerDeleted(org.eclipse.core.resources.IMarker marker, java.util.Map attributes) {
			String viewId = (String) attributes.get(org.eclipse.gmf.runtime.common.ui.resources.IMarker.ELEMENT_ID);
			refreshDecorators(viewId, diagram);
		}

		«EXPAND xpt::Common::generatedMemberComment»
		public void handleMarkerChanged(org.eclipse.core.resources.IMarker marker) {
			if (!MARKER_TYPE.equals(getType(marker))) {
				return;
			}
			String viewId = marker.getAttribute(
					org.eclipse.gmf.runtime.common.ui.resources.IMarker.ELEMENT_ID, ""); «EXPAND xpt::Common::nonNLS»
			refreshDecorators(viewId, diagram);
		}

		«EXPAND xpt::Common::generatedMemberComment»
		private String getType(org.eclipse.core.resources.IMarker marker) {
			try {
				return marker.getType();
			} catch (org.eclipse.core.runtime.CoreException e) {
				«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError(
						"Validation marker refresh failure", e); «EXPAND xpt::Common::nonNLS»
				return ""; «EXPAND xpt::Common::nonNLS»
			}
		}
	}
	«ENDIF-»
	«EXPAND additions-»
}
«ENDDEFINE»

«DEFINE additions FOR gmfgen::GenDiagram»«ENDDEFINE»

Back to the top