Skip to main content
summaryrefslogtreecommitdiffstats
blob: 11eb887a102085d95e4bd856927e4ab2525d6605 (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
/*******************************************************************************
 * Copyright (c) 2012, 2016 Obeo.
 * 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:
 *     Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.compare.provider.spec;

import static com.google.common.collect.Lists.newArrayList;
import static org.eclipse.emf.compare.ConflictKind.REAL;
import static org.eclipse.emf.compare.utils.EMFComparePredicates.canBeConsideredAsPseudoConflicting;
import static org.eclipse.emf.compare.utils.EMFComparePredicates.hasDirectOrIndirectConflict;

import java.util.Collection;
import java.util.List;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.util.ResourceLocator;
import org.eclipse.emf.compare.Comparison;
import org.eclipse.emf.compare.Diff;
import org.eclipse.emf.compare.DifferenceKind;
import org.eclipse.emf.compare.DifferenceSource;
import org.eclipse.emf.compare.DifferenceState;
import org.eclipse.emf.compare.Match;
import org.eclipse.emf.compare.internal.merge.IMergeData;
import org.eclipse.emf.compare.internal.utils.ComparisonUtil;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.provider.ComposedImage;

/**
 * Utility class to externalize the retrieval of difference overlay.
 * 
 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
 */
public class OverlayImageProvider {

	/** The base name of the change (and move) kind of diff overlay. */
	private static final String CHG_OV = "chg_ov"; //$NON-NLS-1$

	/** The base name of the deletion kind of diff overlay. */
	private static final String DEL_OV = "del_ov"; //$NON-NLS-1$

	/** The base name of the add kind of diff overlay. */
	private static final String ADD_OV = "add_ov"; //$NON-NLS-1$

	/** The base name of the merge to right diff overlay. */
	private static final String MERGED_TO_RIGHT_OV = "merged_right_ov"; //$NON-NLS-1$

	/** The base name of the merge to left diff overlay. */
	private static final String MERGED_TO_LEFT_OV = "merged_left_ov"; //$NON-NLS-1$

	/** The base name of the rejected diff overlay. */
	private static final String REJECTED_OV = "removed_ov"; //$NON-NLS-1$

	/** The base name of the accepted diff overlay. */
	private static final String ACCEPTED_OV = "merged_ov"; //$NON-NLS-1$

	/** The resource locator to use to retrieve the images. */
	private final ResourceLocator fResourceLocator;

	/**
	 * Creates a new instance with the given resource locator.
	 * 
	 * @param resourceLocator
	 *            the resource locator to use to retrieve images
	 */
	public OverlayImageProvider(ResourceLocator resourceLocator) {
		this.fResourceLocator = resourceLocator;
	}

	/**
	 * Returns a composed image with the image of the given diff the appropriate overlay.
	 * 
	 * @param diff
	 *            the diff for which we have to find an overlay.
	 * @param imageToCompose
	 *            the image of the diff to use as base.
	 * @return a composed image with the image of the given diff the appropriate overlay.
	 */
	public Object getComposedImage(Diff diff, Object imageToCompose) {
		String overlay = getImageOverlay(diff);
		return getComposedImage(imageToCompose, overlay);
	}

	/**
	 * Returns a composed image with the image of the given match the appropriate overlay.
	 * 
	 * @param match
	 *            the match for which we have to find an overlay.
	 * @param imageToCompose
	 *            the image of the match to use as base.
	 * @return a composed image with the image of the given match the appropriate overlay.
	 */
	public Object getComposedImage(Match match, Object imageToCompose) {
		String overlay = getImageOverlay();
		return getComposedImage(imageToCompose, overlay);
	}

	/**
	 * Returns a composed image with the image of the given object and the appropriate overlay.
	 * 
	 * @param object
	 *            the object for which we have to find an overlay.
	 * @param imageToCompose
	 *            the image of the match to use as base.
	 * @return a composed image with the image of the given object and the appropriate overlay.
	 * @since 4.0
	 */
	public Object getComposedImage(Object object, Object imageToCompose) {
		String overlay = "full/ovr16/match_ov"; //$NON-NLS-1$
		return getComposedImage(imageToCompose, overlay);
	}

	/**
	 * Returns a composed image with the image of the given match the overlay at the given path.
	 * 
	 * @param imageToCompose
	 *            the image to use as base.
	 * @param overlayPath
	 *            the path to the overlay.
	 * @return a composed image with the image of the given match the overlay at the given path.
	 */
	private Object getComposedImage(Object imageToCompose, String overlayPath) {
		Collection<Object> images = newArrayList();
		images.add(imageToCompose);
		if (overlayPath != null) {
			Object image = fResourceLocator.getImage(overlayPath);
			images.add(image);
		}
		return new ComposedImageExtension(images);
	}

	/**
	 * Returns the path to the image overlay for the given {@code diff}.
	 * 
	 * @param diff
	 *            the diff we have to find an image for.
	 * @return the path to the image overlay for the given {@code diff}.
	 */
	// Nothing here has to be externalized
	@SuppressWarnings("nls")
	private String getImageOverlay(Diff diff) {
		final Comparison comparison = ComparisonUtil.getComparison(diff);
		String path = "full/ovr16/";

		if (diff.getState() == DifferenceState.MERGED) {
			path += getMergedOverlay(diff);
		} else if (diff.getState() == DifferenceState.DISCARDED) {
			path += REJECTED_OV;
		} else if (comparison.isThreeWay()) {
			path += getThreeWayOverlay(diff);
		} else {
			path += getTwoWayDiffOverlay(diff);
		}
		return path;
	}

	/**
	 * Returns the overlay path for the given unmerged diff.
	 * 
	 * @param diff
	 *            the diff we have to find an image for.
	 * @return the overlay path for the given unmerged diff.
	 */
	// Nothing here has to be externalized
	@SuppressWarnings("nls")
	private String getThreeWayOverlay(final Diff diff) {
		final DifferenceKind diffKind = diff.getKind();
		final DifferenceSource source = diff.getSource();

		StringBuilder path = new StringBuilder();
		if (hasDirectOrIndirectConflict(REAL).apply(diff)) {
			// The diff or one of its refining diffs are in a pseudo conflict
			path.append("conf");
			if (source == DifferenceSource.RIGHT) {
				path.append("r_");
			}
		} else if (canBeConsideredAsPseudoConflicting().apply(diff)) {
			// If the diff is not a refined diff and are in a pseudo conflict
			// Or if the diff is a refined diff that are not in a direct pseudo conflict, but all its refining
			// diffs are in pseudo conflicts
			path.append("pconf");
			if (source == DifferenceSource.RIGHT) {
				path.append("r_");
			}
		} else {
			switch (source) {
				case LEFT:
					path.append("r_out");
					break;
				case RIGHT:
					path.append("r_in");
					break;
				default:
					// Cannot happen ... for now
					break;
			}
		}

		switch (diffKind) {
			case ADD:
				path.append(ADD_OV);
				break;
			case DELETE:
				path.append(DEL_OV);
				break;
			case CHANGE:
				// fallthrough
			case MOVE:
				path.append(CHG_OV);
				break;
			default:
				// Cannot happen ... for now
				break;
		}
		return path.toString();
	}

	/**
	 * Return the merged overlay path for the given diff.
	 * 
	 * @param diff
	 *            the diff we have to find an image for.
	 * @return the merged overlay for the given diff.
	 */
	private String getMergedOverlay(Diff diff) {
		final String path;
		Adapter adapter = EcoreUtil.getExistingAdapter(diff, IMergeData.class);
		if (adapter != null) {
			IMergeData mergeData = (IMergeData)adapter;
			switch (mergeData.getMergeMode()) {
				case LEFT_TO_RIGHT:
					path = MERGED_TO_RIGHT_OV;
					break;
				case RIGHT_TO_LEFT:
					path = MERGED_TO_LEFT_OV;
					break;
				case ACCEPT:
					path = ACCEPTED_OV;
					break;
				case REJECT:
					path = REJECTED_OV;
					break;
				default:
					throw new IllegalStateException();
			}
		} else {
			// fall-back to standard overlay.
			final Match match = diff.getMatch();
			final Comparison comparison = match.getComparison();
			if (comparison.isThreeWay()) {
				path = getThreeWayOverlay(diff);
			} else {
				path = getTwoWayDiffOverlay(diff);
			}
		}
		return path;
	}

	/**
	 * Returns the overlay path for the given unmerged diff.
	 * 
	 * @param diff
	 *            the diff we have to find an image for.
	 * @return the overlay path for the given unmerged diff.
	 */
	private String getTwoWayDiffOverlay(Diff diff) {
		final String path;
		final DifferenceKind diffKind = diff.getKind();
		switch (diffKind) {
			case ADD:
				path = ADD_OV;
				break;
			case DELETE:
				path = DEL_OV;
				break;
			case CHANGE:
				// fallthrough
			case MOVE:
				path = CHG_OV;
				break;
			default:
				throw new IllegalStateException();
		}
		return path;
	}

	/**
	 * Returns the overlay path for the given match.
	 * 
	 * @return the overlay path for the given match.
	 */
	// Nothing here has to be externalized
	@SuppressWarnings("nls")
	private String getImageOverlay() {
		return "full/ovr16/match_ov";
	}

	/**
	 * Extended {@link ComposedImage} that positionned the overlay properly for EMF Compare.
	 * 
	 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
	 */
	private static final class ComposedImageExtension extends ComposedImage {

		/** The offset of the overlays. */
		private static final int X_OFFSET = 10;

		/**
		 * Creates a new instance with the given image list. The second image will be draw at the offset
		 * (X_OFFSET, 2).
		 * 
		 * @param images
		 *            the images to composed.
		 */
		ComposedImageExtension(Collection<?> images) {
			super(images);
		}

		/**
		 * {@inheritDoc}
		 * 
		 * @see org.eclipse.emf.edit.provider.ComposedImage#getDrawPoints(org.eclipse.emf.edit.provider.ComposedImage.Size)
		 */
		@Override
		public List<Point> getDrawPoints(Size size) {
			List<ComposedImage.Point> result = super.getDrawPoints(size);
			if (result.size() > 1) {
				result.get(1).x = X_OFFSET;
				result.get(1).y = 2;
			}
			return result;
		}

		/**
		 * {@inheritDoc}
		 * 
		 * @see org.eclipse.emf.edit.provider.ComposedImage#getSize(java.util.Collection)
		 */
		@Override
		public Size getSize(Collection<? extends Size> sizes) {
			this.imageSizes = newArrayList(sizes);
			List<Point> drawPoints = getDrawPoints(null);

			Size result = new Size();
			for (int i = 0; i < sizes.size(); i++) {
				Size size = this.imageSizes.get(i);
				Point point = drawPoints.get(i);

				result.width = Math.max(result.width, size.width + Math.abs(point.x));
				result.height = Math.max(result.height, size.height + Math.abs(point.y));
			}
			return result;
		}
	}
}

Back to the top