Skip to main content
summaryrefslogtreecommitdiffstats
blob: b204c8e0ed80d8bc3a58802d9a19f38c4ce00335 (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
/*******************************************************************************
 * Copyright (c) 2013, 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.rcp.ui.tests.structuremergeviewer.groups.provider;

import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.find;

import java.io.IOException;
import java.util.Iterator;

import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.compare.AttributeChange;
import org.eclipse.emf.compare.Comparison;
import org.eclipse.emf.compare.EMFCompare;
import org.eclipse.emf.compare.EMFCompare.Builder;
import org.eclipse.emf.compare.Match;
import org.eclipse.emf.compare.ReferenceChange;
import org.eclipse.emf.compare.rcp.internal.extension.impl.EMFCompareBuilderConfigurator;
import org.eclipse.emf.compare.rcp.ui.internal.structuremergeviewer.filters.StructureMergeViewerFilter;
import org.eclipse.emf.compare.rcp.ui.internal.structuremergeviewer.groups.provider.TreeItemProviderAdapterFactorySpec;
import org.eclipse.emf.compare.scope.DefaultComparisonScope;
import org.eclipse.emf.compare.scope.IComparisonScope;
import org.eclipse.emf.compare.tests.edit.data.ResourceScopeProvider;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
import org.eclipse.emf.edit.tree.TreeNode;
import org.junit.Before;

import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Predicate;
import com.google.common.eventbus.EventBus;

/**
 * @author <a href="mailto:axel.richard@obeo.fr">Axel Richard</a>
 */
@SuppressWarnings("restriction")
public class AbstractTestTreeNodeItemProviderAdapter {

	protected TreeItemProviderAdapterFactorySpec treeItemProviderAdapterFactory;

	protected EventBus eventBus;

	@Before
	public void before() throws IOException {
		eventBus = new EventBus();
		treeItemProviderAdapterFactory = new TreeItemProviderAdapterFactorySpec(
				new StructureMergeViewerFilter(eventBus));
	}

	/**
	 * @return the comparison
	 * @throws IOException
	 */
	public static Comparison getComparison(ResourceScopeProvider scopeProvider) throws IOException {
		final IComparisonScope scope = new DefaultComparisonScope(scopeProvider.getLeft(),
				scopeProvider.getRight(), scopeProvider.getOrigin());
		final Builder builder = EMFCompare.builder();
		EMFCompareBuilderConfigurator.createDefault().configure(builder);
		return builder.build().compare(scope);
	}

	/**
	 * Function that retrieve the data of the given TreeNode.
	 */
	public static final Function<Object, EObject> TREE_NODE_DATA = new Function<Object, EObject>() {
		public EObject apply(Object node) {
			if (node instanceof TreeNode) {
				return ((TreeNode)node).getData();
			} else if (node instanceof EObject) {
				return (EObject)node;
			}
			return null;
		}
	};

	/**
	 * Function that returns all contents of the given EObject.
	 */
	public static final Function<Object, Iterator<EObject>> E_ALL_CONTENTS = new Function<Object, Iterator<EObject>>() {
		public Iterator<EObject> apply(Object object) {
			if (object instanceof EObject) {
				return ((EObject)object).eAllContents();
			}
			return null;
		}
	};

	public static TreeNode getTreeNode(TreeNode parent, EObject eObject) {
		for (TreeNode child : parent.getChildren()) {
			EObject data = child.getData();
			if (eObject.equals(data)) {
				return child;
			}
		}
		return null;
	}

	public static Predicate<Object> matchTreeNode = new Predicate<Object>() {
		public boolean apply(Object object) {
			if (object instanceof TreeNode) {
				EObject data = ((TreeNode)object).getData();
				if (data instanceof Match) {
					return true;
				}
			}
			return false;
		}
	};

	protected Match getMatchWithFeatureValue(Iterable<?> c, final String featureName, final Object value) {
		Iterable<Match> matches = filter(c, Match.class);
		Predicate<Match> predicate = hasFeatureValue(featureName, value);
		return find(matches, predicate);
	}

	protected ReferenceChange getReferenceChangeWithFeatureValue(Iterable<?> c, final String featureName,
			final Object value) {
		Iterable<ReferenceChange> matches = filter(c, ReferenceChange.class);
		Predicate<ReferenceChange> predicate = new Predicate<ReferenceChange>() {
			public boolean apply(ReferenceChange referenceChange) {
				EObject referenceChangeValue = referenceChange.getValue();
				if (referenceChangeValue != null) {
					return Objects.equal(eGet(referenceChangeValue, featureName), value);
				}
				return false;
			}
		};
		return find(matches, predicate);
	}

	protected AttributeChange getAttributeChangeWithFeatureValue(Iterable<?> c, final String featureName,
			final Object value) {
		Iterable<AttributeChange> matches = filter(c, AttributeChange.class);
		Predicate<AttributeChange> predicate = new Predicate<AttributeChange>() {
			public boolean apply(AttributeChange attributeChange) {
				Object attributeChangeValue = attributeChange.getValue();
				if (attributeChangeValue instanceof EObject) {
					return Objects.equal(eGet((EObject)attributeChangeValue, featureName), value);
				} else if (attributeChangeValue instanceof String) {
					return featureName.equals(attributeChange.getAttribute().getName())
							&& value.equals(attributeChangeValue);
				} else {
					// attributeChangeValue can be an instance of Integer or something else that has not been
					// managed yet.
					throw new IllegalStateException("Developers need to implement missing cases.");
				}
			}
		};
		return find(matches, predicate);
	}

	protected Predicate<Match> hasFeatureValue(final String featureName, final Object value) {
		Predicate<Match> predicate = new Predicate<Match>() {
			public boolean apply(Match match) {
				final boolean ret;
				final EObject left = match.getLeft();
				final EObject right = match.getRight();
				final EObject origin = match.getOrigin();
				if (left != null) {
					ret = Objects.equal(value, eGet(left, featureName));
				} else if (right != null) {
					ret = Objects.equal(value, eGet(right, featureName));
				} else if (origin != null) {
					ret = Objects.equal(value, eGet(origin, featureName));
				} else {
					ret = false;
				}
				return ret;
			}
		};
		return predicate;
	}

	protected Object eGet(EObject eObject, String featureName) {
		EStructuralFeature eStructuralFeature = eObject.eClass().getEStructuralFeature(featureName);
		return eObject.eGet(eStructuralFeature);
	}

	protected ITreeItemContentProvider adaptAsITreeItemContentProvider(Notifier notifier) {
		ITreeItemContentProvider contentProvider = (ITreeItemContentProvider)treeItemProviderAdapterFactory
				.adapt(notifier, ITreeItemContentProvider.class);
		return contentProvider;
	}
}

Back to the top