Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 22937356b997679132c249dbd630c16f37cc202f (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
/*****************************************************************************
 * Copyright (c) 2013, 2017 CEA LIST and others.
 *
 * 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:
 *   CEA LIST - Initial API and implementation
 *   Christian W. Damus (CEA) - bug 431618
 *
 *****************************************************************************/
package org.eclipse.papyrus.cdo.internal.ui.markers;

import java.util.Collection;
import java.util.Iterator;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.transaction.RunnableWithResult;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.gmf.runtime.common.core.command.AbstractCommand;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.papyrus.cdo.internal.ui.Activator;
import org.eclipse.papyrus.cdo.validation.problems.EProblem;
import org.eclipse.papyrus.cdo.validation.problems.edit.ProblemEditUtil;
import org.eclipse.papyrus.cdo.validation.problems.util.ProblemsManager;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.services.markerlistener.IPapyrusMarker;
import org.eclipse.papyrus.infra.services.markerlistener.providers.AbstractMarkerProvider;
import org.eclipse.papyrus.infra.services.markerlistener.providers.IMarkerProvider2;
import org.eclipse.papyrus.infra.services.markerlistener.util.MarkerListenerUtils;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;

/**
 * This is the CDOMarkerProvider type. Enjoy.
 */
public class CDOMarkerProvider extends AbstractMarkerProvider implements IMarkerProvider2 {

	private final ProblemEditUtil defaultUtil = new ProblemEditUtil(new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE));

	public CDOMarkerProvider() {
		super();
	}

	@Override
	public boolean canProvideMarkersFor(Resource resource) {
		return resource instanceof CDOResource;
	}

	@Override
	public Collection<? extends IPapyrusMarker> getMarkers(final Resource resource, final String type, final boolean includeSubtypes) throws CoreException {
		// run in a read-only transaction because the problems manager accesses
		// a cross-reference adapter
		return run(resource, CoreException.class, new RunnableWithResult.Impl<Collection<? extends IPapyrusMarker>>() {

			@Override
			public void run() {
				setResult(Lists.newArrayList(Iterators.transform( //
						getProblems(resource, type, includeSubtypes), //
						CDOPapyrusMarker.wrap(getProblemEditUtil(resource)))));
			}
		});
	}

	protected Iterator<? extends EProblem> getProblems(final Resource resource, final String type, boolean includeSubtypes) {
		final Predicate<EProblem> filter;

		if (type == null) {
			filter = Predicates.alwaysTrue();
		} else if (includeSubtypes) {
			filter = new Predicate<EProblem>() {

				@Override
				public boolean apply(EProblem input) {
					return MarkerListenerUtils.isMarkerTypeSubtypeOf(input.getType(), type);
				}
			};
		} else {
			filter = new Predicate<EProblem>() {

				@Override
				public boolean apply(EProblem input) {
					return type.equals(input.getType());
				}
			};
		}

		return Iterators.filter(getProblemsManager(resource).getAllProblems(resource), filter);
	}

	@Override
	public void createMarkers(final Resource resource, final Diagnostic diagnostic, final IProgressMonitor monitor) throws CoreException {
		// run in a read-only transaction because the problems manager accesses
		// a cross-reference adapter. Note that a read/write transaction is not
		// needed because we aren't modifying the contents of the resource set
		// (the problems model is not stored in a resource)
		run(resource, CoreException.class, new Runnable() {

			@Override
			public void run() {
				try {
					basicCreateMarkers(resource, diagnostic, monitor);
				} catch (CoreException e) {
					throw new WrappedException(e);
				}
			}
		});
	}

	final void basicCreateMarkers(Resource resource, Diagnostic diagnostic, IProgressMonitor monitor) throws CoreException {
		super.createMarkers(resource, diagnostic, monitor);
	}

	@Override
	protected void doCreateMarker(Resource resource, Diagnostic diagnostic) throws CoreException {
		ProblemsManager mgr = getProblemsManager(resource);
		EProblem problem = mgr.createProblem(diagnostic);
		if (problem != null) {
			mgr.addProblem(problem);
		}
	}

	@Override
	protected void batchCreated(Resource resource) {
		super.batchCreated(resource);

		ResourceSet rset = resource.getResourceSet();
		if (rset instanceof ModelSet) {
			// yield the resource set to any other threads that might
			// be waiting to read it
			((ModelSet) rset).getTransactionalEditingDomain().yield();
		}
	}

	@Override
	public void deleteMarkers(final EObject object, final IProgressMonitor monitor, final String type, final boolean includeSubtypes) throws CoreException {
		// run in a read-only transaction because the problems manager accesses
		// a cross-reference adapter. Note that a read/write transaction is not
		// needed because we aren't modifying the contents of the resource set
		// (the problems model is not stored in a resource)
		run(object.eResource(), CoreException.class, new Runnable() {

			@Override
			public void run() {
				try {
					basicDeleteMarkers(object, monitor, type, includeSubtypes);
				} catch (CoreException e) {
					throw new WrappedException(e);
				}
			}
		});
	}

	protected final void basicDeleteMarkers(EObject object, IProgressMonitor monitor, String type, boolean includeSubtypes) throws CoreException {
		super.deleteMarkers(object, monitor, type, includeSubtypes);
	}

	@Override
	public void deleteMarkers(final Resource resource, IProgressMonitor monitor) {
		try {
			this.deleteMarkers(resource, monitor, null, true);
		} catch (CoreException e) {
			Activator.log.error(e);
		}
	}

	@Override
	public void deleteMarkers(final Resource resource, IProgressMonitor monitor, final String markerType, final boolean includeSubtypes) throws CoreException {
		SubMonitor sub = SubMonitor.convert(monitor, IProgressMonitor.UNKNOWN);

		// run in a read-only transaction because the problems manager accesses
		// a cross-reference adapter. Note that a read/write transaction is not
		// needed because we aren't modifying the contents of the resource set
		// (the problems model is not stored in a resource)
		run(resource, new Runnable() {

			@Override
			public void run() {
				ProblemsManager mgr = getProblemsManager(resource);
				if (markerType == null) {
					// efficiently remove all markers for the resource
					mgr.purgeProblems(resource);
				} else {
					// tediously remove the matching markers
					for (EProblem next : Lists.newArrayList(getProblems(resource, markerType, includeSubtypes))) {
						ProblemsManager.delete(next);
					}
				}
			}
		});

		sub.done();
	}

	@Override
	public boolean hasMarkers(Resource context, EObject object) {
		ProblemsManager manager = getProblemsManager(context);

		return (manager != null) && manager.getAllProblems(object).hasNext();
	}

	@Override
	public ICommand getMarkerDeletionCommand(Resource context, EObject object) {
		return new MarkerDeletionCommand(context, object);
	}

	private ProblemsManager getProblemsManager(Resource resource) {
		return ProblemsManager.getProblemsManager(resource.getResourceSet());
	}

	private ProblemEditUtil getProblemEditUtil(Resource resource) {
		ProblemEditUtil result = defaultUtil;

		ResourceSet rset = resource.getResourceSet();
		if (rset instanceof ModelSet) {
			AdapterFactory factory = ((AdapterFactoryEditingDomain) ((ModelSet) rset).getTransactionalEditingDomain()).getAdapterFactory();
			result = new ProblemEditUtil(factory);
		}

		return result;
	}

	static <X extends Throwable> void run(Resource context, Runnable runnable) {
		run(context, RuntimeException.class, runnable);
	}

	static <X extends Throwable> void run(Resource context, Class<X> exceptionType, Runnable runnable) throws X {
		ResourceSet rset = context.getResourceSet();
		if (rset instanceof ModelSet) {
			try {
				((ModelSet) rset).getTransactionalEditingDomain().runExclusive(runnable);
			} catch (WrappedException e) {
				throw exceptionType.cast(e.exception());
			} catch (InterruptedException e) {
				Activator.log.error("CDO problem markers runnable interrupted.", e); //$NON-NLS-1$
			}
		} else {
			runnable.run();
		}
	}

	static <T, X extends Throwable> T run(Resource context, Class<X> exceptionType, RunnableWithResult<T> runnable) throws X {
		T result;

		ResourceSet rset = context.getResourceSet();
		if (rset instanceof ModelSet) {
			try {
				result = TransactionUtil.runExclusive(((ModelSet) rset).getTransactionalEditingDomain(), runnable);
			} catch (WrappedException e) {
				throw exceptionType.cast(e.exception());
			} catch (InterruptedException e) {
				Activator.log.error("CDO problem markers runnable interrupted.", e); //$NON-NLS-1$
				result = null;
			}
		} else {
			runnable.run();
			result = runnable.getResult();
		}

		return result;
	}

	//
	// Nested types
	//

	private class MarkerDeletionCommand extends AbstractCommand {

		private Resource context;

		private EObject object;

		private Collection<? extends EProblem> problemsForUndo;

		public MarkerDeletionCommand(Resource context, EObject object) {
			super("Delete markers");

			this.context = context;
			this.object = object;
		}

		@Override
		public void dispose() {
			context = null;
			object = null;
			problemsForUndo = null;

			super.dispose();
		}

		@Override
		protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
			ProblemsManager manager = getProblemsManager(context);
			Collection<EProblem> problems = Lists.newArrayList(manager.getAllProblems(object));
			if (problems.isEmpty()) {
				// Nothing to do
				return CommandResult.newOKCommandResult();
			}

			for (EProblem problem : problems) {
				ProblemsManager.delete(problem);
			}

			// Initialize undo information
			problemsForUndo = problems;

			return CommandResult.newOKCommandResult();
		}

		@Override
		protected CommandResult doUndoWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
			if (problemsForUndo == null) {
				// Nothing to do
				return CommandResult.newOKCommandResult();
			}

			// Detach undo information
			final Collection<? extends EProblem> problems = problemsForUndo;
			problemsForUndo = null;

			Resource resource = object.eResource();
			if (resource != null) { // Should have been reattached by now
				context = resource;
				ProblemsManager manager = getProblemsManager(resource);
				for (EProblem problem : problems) {
					problem.setElement(object);
					manager.addProblem(problem);
				}
			}

			return CommandResult.newOKCommandResult();
		}

		@Override
		protected CommandResult doRedoWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
			return doExecuteWithResult(progressMonitor, info);
		}

	}

}

Back to the top