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

import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.EMap;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EValidator;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.papyrus.cdo.internal.ui.Activator;
import org.eclipse.papyrus.cdo.validation.problems.EProblem;
import org.eclipse.papyrus.cdo.validation.problems.ESeverity;
import org.eclipse.papyrus.cdo.validation.problems.edit.ProblemEditUtil;
import org.eclipse.papyrus.cdo.validation.problems.util.ProblemsManager;
import org.eclipse.papyrus.infra.services.markerlistener.IPapyrusMarker;
import org.eclipse.papyrus.infra.services.markerlistener.util.MarkerListenerUtils;

import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.collect.Maps;

/**
 * This is the CDOPapyrusMarker type. Enjoy.
 */
public class CDOPapyrusMarker implements IPapyrusMarker {

	private final ProblemEditUtil util;

	private final Resource resource;

	private final EProblem problem;

	CDOPapyrusMarker(EProblem problem, ProblemEditUtil util) {
		super();

		this.util = util;
		this.resource = problem.getElement().eResource();
		this.problem = problem;
	}

	public static Function<EProblem, CDOPapyrusMarker> wrap(final ProblemEditUtil util) {

		return new Function<EProblem, CDOPapyrusMarker>() {

			@Override
			public CDOPapyrusMarker apply(EProblem input) {
				return new CDOPapyrusMarker(input, util);
			}
		};
	}

	@Override
	public Resource getResource() {
		return resource;
	}

	@Override
	public EObject getEObject() {
		return problem.getElement();
	}

	@Override
	public boolean exists() {
		EObject element = getEObject();
		return (element != null) && (element.eResource() != null);
	}

	@Override
	public String getType() {
		String result = problem.getType();
		return (result != null) ? result : EValidator.MARKER;
	}

	@Override
	public String getTypeLabel() throws CoreException {
		String result = MarkerListenerUtils.getMarkerTypeLabel(getType());
		return ((result != null) && (result.length() > 0)) ? result : util.getProblemType(problem);
	}

	@Override
	public void delete() {
		ProblemsManager.delete(problem);
	}

	@Override
	public Object getAttribute(String name) throws CoreException {
		Object result = null;

		if (name.equals(EValidator.URI_ATTRIBUTE)) {
			result = EcoreUtil.getURI(getEObject()).toString();
		} else if (name.equals(SEVERITY)) {
			result = getMarkerSeverity();
		} else if (name.equals(MESSAGE)) {
			result = problem.getMessage();
		} else if (problem.getAttributes().containsKey(name)) {
			result = coerce(problem.getAttributes().get(name));
		} else {
			throw new CoreException(error("No such marker attribute: " + name)); //$NON-NLS-1$
		}

		return result;
	}

	protected Object coerce(String value) {
		Object result;

		if (value == null) {
			result = value;
		} else if (isBoolean(value)) {
			result = Boolean.parseBoolean(value);
		} else if (isInteger(value)) {
			result = Integer.parseInt(value);
		} else {
			result = value;
		}

		return result;
	}

	static boolean isBoolean(String s) {
		return (s != null) && (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("false")); //$NON-NLS-1$ //$NON-NLS-2$
	}

	static boolean isInteger(String s) {
		boolean result = (s != null) && (s.length() > 0);

		for (int i = 0; result && (i < s.length()); i++) {
			result = Character.isDigit(s.charAt(i));
		}

		return result;
	}

	protected final IStatus error(String message) {
		return new Status(IStatus.ERROR, Activator.PLUGIN_ID, message);
	}

	protected int getMarkerSeverity() {
		int result;

		switch (problem.getSeverity()) {
		case OK:
		case INFO:
			result = SEVERITY_INFO;
			break;
		case WARNING:
			result = SEVERITY_WARNING;
			break;
		default:
			result = SEVERITY_ERROR;
			break;
		}

		return result;
	}

	@Override
	public String getAttribute(String name, String defaultValue) {
		String result = null;

		if (name.equals(EValidator.URI_ATTRIBUTE)) {
			result = EcoreUtil.getURI(getEObject()).toString();
		} else if (name.equals(MESSAGE)) {
			result = problem.getMessage();
		} else if (problem.getAttributes().containsKey(name)) {
			result = coerce(problem.getAttributes().get(name), defaultValue);
		} else {
			result = defaultValue;
		}

		return (result != null) ? result : defaultValue;
	}

	protected String coerce(String value, String defaultValue) {
		String result;

		if (value == null) {
			result = defaultValue;
		} else if (isBoolean(value) || isInteger(value)) {
			throw new IllegalArgumentException("Not a string value: " + value); //$NON-NLS-1$
		} else {
			result = value;
		}

		return result;
	}

	@Override
	public boolean getAttribute(String name, boolean defaultValue) {
		boolean result;

		if (problem.getAttributes().containsKey(name)) {
			result = coerce(problem.getAttributes().get(name), defaultValue);
		} else {
			result = defaultValue;
		}

		return result;
	}

	protected boolean coerce(String value, boolean defaultValue) {
		boolean result;

		if (value == null) {
			result = defaultValue;
		} else if (!isBoolean(value)) {
			throw new IllegalArgumentException("Not a boolean value: " + value); //$NON-NLS-1$
		} else {
			result = Boolean.parseBoolean(value);
		}

		return result;
	}

	@Override
	public int getAttribute(String name, int defaultValue) {
		int result;

		if (name.equals(SEVERITY)) {
			result = getMarkerSeverity();
		} else if (problem.getAttributes().containsKey(name)) {
			result = coerce(problem.getAttributes().get(name), defaultValue);
		} else {
			result = defaultValue;
		}

		return result;
	}

	protected int coerce(String value, int defaultValue) {
		int result;

		if (value == null) {
			result = defaultValue;
		} else if (!isInteger(value)) {
			throw new IllegalArgumentException("Not an integer value: " + value); //$NON-NLS-1$
		} else {
			result = Integer.parseInt(value);
		}

		return result;
	}

	@Override
	public Map<String, ?> getAttributes() throws CoreException {
		Map<String, Object> result = coerce(problem.getAttributes());

		result.put(EValidator.URI_ATTRIBUTE, EcoreUtil.getURI(getEObject()).toString());
		result.put(SEVERITY, getMarkerSeverity());
		result.put(MESSAGE, problem.getMessage());

		return result;
	}

	protected Map<String, Object> coerce(EMap<String, String> attributes) {
		Map<String, Object> result = Maps.newHashMap();

		for (Map.Entry<String, String> next : attributes) {
			result.put(next.getKey(), coerce(next.getValue()));
		}

		return result;
	}

	@Override
	public void setAttribute(String name, Object value) throws CoreException {
		if (name.equals(EValidator.URI_ATTRIBUTE)) {
			throw new CoreException(error("Cannot set URI of a CDOPapyrusMarker.")); //$NON-NLS-1$
		} else if (name.equals(SEVERITY)) {
			setMarkerSeverity(((Number) value).intValue());
		} else if (name.equals(MESSAGE)) {
			problem.setMessage((String) value);
		} else if (value == null) {
			problem.getAttributes().removeKey(name);
		} else {
			problem.getAttributes().put(name, value.toString());
		}
	}

	protected void setMarkerSeverity(int severity) throws CoreException {
		switch (severity) {
		case SEVERITY_INFO:
			problem.setSeverity(ESeverity.INFO);
			break;
		case SEVERITY_WARNING:
			problem.setSeverity(ESeverity.WARNING);
			break;
		case SEVERITY_ERROR:
			problem.setSeverity(ESeverity.ERROR);
			break;
		default:
			throw new CoreException(error("Invalid marker severity: " //$NON-NLS-1$
					+ severity));
		}
	}

	@Override
	public void setAttribute(String name, String value) throws CoreException {
		if (name.equals(EValidator.URI_ATTRIBUTE)) {
			throw new CoreException(error("Cannot set URI of a CDOPapyrusMarker.")); //$NON-NLS-1$
		} else if (name.equals(MESSAGE)) {
			problem.setMessage(value);
		} else if (name.equals(SEVERITY)) {
			throw new CoreException(error("Severity of a CDOPapyrusMarker is not a string.")); //$NON-NLS-1$
		} else if (value == null) {
			problem.getAttributes().removeKey(name);
		} else {
			problem.getAttributes().put(name, value);
		}
	}

	@Override
	public void setAttribute(String name, boolean value) throws CoreException {
		if (name.equals(EValidator.URI_ATTRIBUTE)) {
			throw new CoreException(error("URI of a CDOPapyrusMarker is not a boolean.")); //$NON-NLS-1$
		} else if (name.equals(SEVERITY)) {
			throw new CoreException(error("Severity of a CDOPapyrusMarker is not a boolean.")); //$NON-NLS-1$
		} else if (name.equals(MESSAGE)) {
			throw new CoreException(error("Message of a CDOPapyrusMarker is not a boolean.")); //$NON-NLS-1$
		} else {
			problem.getAttributes().put(name, Boolean.toString(value));
		}
	}

	@Override
	public void setAttribute(String name, int value) throws CoreException {
		if (name.equals(SEVERITY)) {
			setMarkerSeverity(value);
		} else if (name.equals(EValidator.URI_ATTRIBUTE)) {
			throw new CoreException(error("URI of a CDOPapyrusMarker is not an integer.")); //$NON-NLS-1$
		} else if (name.equals(MESSAGE)) {
			throw new CoreException(error("Message of a CDOPapyrusMarker is not an integer.")); //$NON-NLS-1$
		} else {
			problem.getAttributes().put(name, Integer.toString(value));
		}
	}

	@Override
	public void setAttributes(Map<String, ?> attributes) throws CoreException {
		for (Map.Entry<String, ?> next : attributes.entrySet()) {
			String name = next.getKey();
			Object value = next.getValue();

			if (name.equals(EValidator.URI_ATTRIBUTE)) {
				throw new CoreException(error("Cannot set URI of a CDOPapyrusMarker.")); //$NON-NLS-1$
			} else if (name.equals(SEVERITY)) {
				setMarkerSeverity(((Number) value).intValue());
			} else if (name.equals(MESSAGE)) {
				problem.setMessage((String) value);
			} else if (value != null) {
				problem.getAttributes().put(name, value.toString());
			}
		}
	}

	@Override
	public boolean equals(Object obj) {
		return (obj instanceof CDOPapyrusMarker) && Objects.equal(((CDOPapyrusMarker) obj).problem, problem);
	}

	@Override
	public int hashCode() {
		return (problem == null) ? 0 : problem.hashCode();
	}

	@Override
	public String toString() {
		return String.format("CDOPapyrusMarker:%s:%s", getResource(), hashCode()); //$NON-NLS-1$
	}

	@Override
	public boolean isSubtypeOf(String type) throws CoreException {
		return (type == null) || MarkerListenerUtils.isMarkerTypeSubtypeOf(getType(), type);
	}
}

Back to the top