Skip to main content
summaryrefslogtreecommitdiffstats
blob: a26658afe12fb63bf99afcadf23fa248f766bd0b (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
/*******************************************************************************
 * Copyright (c) 2014 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.rcp.ui.internal.contentmergeviewer.accessor.impl;

import static org.eclipse.emf.compare.merge.AbstractMerger.isInTerminalState;

import com.google.common.collect.ImmutableList;

import java.util.List;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.compare.FeatureMapChange;
import org.eclipse.emf.compare.internal.merge.IMergeData;
import org.eclipse.emf.compare.internal.merge.MergeMode;
import org.eclipse.emf.compare.internal.utils.ComparisonUtil;
import org.eclipse.emf.compare.rcp.ui.internal.mergeviewer.item.impl.MergeViewerItem;
import org.eclipse.emf.compare.rcp.ui.mergeviewer.IMergeViewer.MergeViewerSide;
import org.eclipse.emf.compare.rcp.ui.mergeviewer.item.IMergeViewerItem;
import org.eclipse.emf.compare.utils.IEqualityHelper;
import org.eclipse.emf.compare.utils.ReferenceUtil;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.FeatureMap;

/**
 * A specific {@link AbstractStructuralFeatureAccessor} for FeatureMapChanges of kind DifferenceKind.CHANGE
 * (represent a value that changed his key).
 * 
 * @author <a href="mailto:axel.richard@obeo.fr">Axel Richard</a>
 * @since 4.0
 */
public class FeatureMapKeyChangeAccessorImpl extends AbstractStructuralFeatureAccessor {

	/**
	 * Default constructor.
	 * 
	 * @param adapterFactory
	 *            the adapater factory used to create the accessor.
	 * @param diff
	 *            the diff associated with this accessor.
	 * @param side
	 *            the side of the accessor.
	 */
	public FeatureMapKeyChangeAccessorImpl(AdapterFactory adapterFactory, FeatureMapChange diff,
			MergeViewerSide side) {
		super(adapterFactory, diff, side);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.rcp.ui.contentmergeviewer.accessor.ICompareAccessor#getItems()
	 */
	public ImmutableList<? extends IMergeViewerItem> getItems() {
		final EStructuralFeature leftKey = getKey(MergeViewerSide.LEFT);
		final EStructuralFeature rightKey = getKey(MergeViewerSide.RIGHT);
		final EStructuralFeature ancestorKey = getKey(MergeViewerSide.ANCESTOR);
		final MergeViewerItem item = new MergeViewerItem(getComparison(), getInitialDiff(), leftKey, rightKey,
				ancestorKey, getSide(), getRootAdapterFactory());

		return ImmutableList.of(item);
	}

	/**
	 * Returns the {@link FeatureMapChange} diff associated with this accessor.
	 * 
	 * @return the {@link FeatureMapChange} diff associated with this accessor.
	 */
	public FeatureMapChange getFeatureMapChange() {
		return (FeatureMapChange)getInitialDiff();
	}

	/**
	 * Returns the key of the FeatureMap.Entry for the given side.
	 * 
	 * @param side
	 *            the given side.
	 * @return the key of the FeatureMap.Entry for the given side, or null if not found.
	 */
	private EStructuralFeature getKey(MergeViewerSide side) {
		EStructuralFeature key = null;
		final FeatureMapChange diff = (FeatureMapChange)getInitialDiff();
		final FeatureMap.Entry entry = (FeatureMap.Entry)diff.getValue();
		if ((side != MergeViewerSide.ANCESTOR && side.convertToDifferenceSource().equals(diff.getSource()))
				&& !isInTerminalState(diff)) {
			key = entry.getEStructuralFeature();
		} else if (isInTerminalState(diff) && !isMergedTargetIsSource(diff)) {
			key = entry.getEStructuralFeature();
		} else {
			// The entry of the diff source side.
			final Object value = entry.getValue();
			// The feature map object of the opposite side of the diff source.
			final EObject eObject = getEObject(side);
			// The entries of the opposite side of the diff source.
			final List<Object> list = ReferenceUtil.getAsList(eObject, getStructuralFeature());

			final IEqualityHelper equalityHelper = getComparison().getEqualityHelper();

			for (Object object : list) {
				// We've found the same value on the opposite side, return the key associated
				if (object instanceof FeatureMap.Entry
						&& equalityHelper.matchingValues(value, ((FeatureMap.Entry)object).getValue())) {
					key = ((FeatureMap.Entry)object).getEStructuralFeature();
					break;
				}
			}
		}
		return key;
	}

	/**
	 * Returns true if the given diff is merged and the target side of the merge is the same as the diff
	 * source.
	 * 
	 * @param diff
	 *            the given {@link FeatureMapChange}.
	 * @return true if the given diff is merged and the target side of the merge is the same as the diff
	 *         source, false otherwise.
	 */
	private boolean isMergedTargetIsSource(FeatureMapChange diff) {
		IMergeData mergeData = (IMergeData)EcoreUtil.getExistingAdapter(ComparisonUtil.getComparison(diff),
				IMergeData.class);
		if (mergeData != null) {
			MergeMode mode = MergeMode.getMergeMode(diff, mergeData.isLeftEditable(),
					mergeData.isRightEditable());
			return diff.getSource() == mode.getMergeTarget(mergeData.isLeftEditable(),
					mergeData.isRightEditable());
		}
		return false;
	}
}

Back to the top