Skip to main content
summaryrefslogtreecommitdiffstats
blob: a403a9b8df8b378f08ea7ac779e04fe4fd57c048 (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
/*******************************************************************************
 * Copyright (c) 2013 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 com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;

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

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.compare.DifferenceKind;
import org.eclipse.emf.compare.DifferenceSource;
import org.eclipse.emf.compare.Match;
import org.eclipse.emf.compare.ResourceAttachmentChange;
import org.eclipse.emf.compare.provider.AdapterFactoryUtil;
import org.eclipse.emf.compare.provider.IItemDescriptionProvider;
import org.eclipse.emf.compare.provider.IItemStyledLabelProvider;
import org.eclipse.emf.compare.provider.ResourceAttachmentChangeItemProvider;
import org.eclipse.emf.compare.provider.utils.ComposedStyledString;
import org.eclipse.emf.compare.provider.utils.IStyledString.IComposedStyledString;
import org.eclipse.emf.compare.provider.utils.IStyledString.Style;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;

/**
 * Specialized {@link ResourceAttachmentChangeItemProvider} returning nice output for {@link #getText(Object)}
 * and {@link #getImage(Object)}.
 * 
 * @author <a href="mailto:axel.richard@obeo.fr">Axel Richard</a>
 * @since 3.0
 */
public class ResourceAttachmentChangeItemProviderSpec extends ResourceAttachmentChangeItemProvider implements IItemStyledLabelProvider, IItemDescriptionProvider {

	/** The image provider used with this item provider. */
	private final OverlayImageProvider overlayProvider;

	/**
	 * Constructor calling super {@link #ResourceAttachmentChangeItemProvider(AdapterFactory)}.
	 * 
	 * @param adapterFactory
	 *            The adapter factory.
	 */
	public ResourceAttachmentChangeItemProviderSpec(AdapterFactory adapterFactory) {
		super(adapterFactory);
		overlayProvider = new OverlayImageProvider(getResourceLocator());
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.provider.ResourceAttachmentChangeItemProvider#getChildren(java.lang.Object)
	 */
	@Override
	public Collection<?> getChildren(Object object) {
		Collection<?> superChildren = super.getChildren(object);
		List<? super Object> ret = newArrayList(superChildren);

		ResourceAttachmentChange resourceAttachmentChange = (ResourceAttachmentChange)object;
		final EObject value;
		switch (resourceAttachmentChange.getSource()) {
			case LEFT:
				value = resourceAttachmentChange.getMatch().getLeft();
				break;
			case RIGHT:
				value = resourceAttachmentChange.getMatch().getRight();
				break;
			default:
				value = null;
				new IllegalStateException();
				break;
		}

		Match matchOfValue = resourceAttachmentChange.getMatch().getComparison().getMatch(value);
		if (matchOfValue != null) {
			Collection<?> children = getChildren(matchOfValue);
			children.remove(resourceAttachmentChange);
			ret.addAll(children);
		}

		return ImmutableList.copyOf(ret);
	}

	/**
	 * Returns the children of the given {@link Match}.
	 * 
	 * @param matchOfValue
	 *            the given {@link Match}.
	 * @return the children of the given {@link Match}.
	 */
	private Collection<?> getChildren(Match matchOfValue) {
		final Collection<?> children;
		ITreeItemContentProvider matchItemContentProvider = (ITreeItemContentProvider)adapterFactory.adapt(
				matchOfValue, ITreeItemContentProvider.class);
		if (matchItemContentProvider != null) {
			Collection<?> itemProviderChildren = matchItemContentProvider.getChildren(matchOfValue);
			if (itemProviderChildren instanceof ImmutableCollection<?>) {
				children = newArrayList(itemProviderChildren);
			} else {
				children = itemProviderChildren;
			}

			Iterator<?> childrenIterator = children.iterator();
			while (childrenIterator.hasNext()) {
				Object child = childrenIterator.next();
				if (child instanceof Match) {
					if (!matchItemContentProvider.hasChildren(child)) {
						childrenIterator.remove();
					}
				}

			}
		} else {
			children = ImmutableList.of();
		}
		return children;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.provider.MatchItemProvider#getImage(java.lang.Object)
	 */
	@Override
	public Object getImage(Object object) {
		final Match match = ((ResourceAttachmentChange)object).getMatch();
		Object ret = AdapterFactoryUtil.getImage(getRootAdapterFactory(), match.getLeft());
		if (ret == null) {
			ret = AdapterFactoryUtil.getImage(getRootAdapterFactory(), match.getRight());
		}
		if (ret == null) {
			ret = AdapterFactoryUtil.getImage(getRootAdapterFactory(), match.getOrigin());
		}
		if (ret == null) {
			ret = super.getImage(object);
		}

		Object matchImage = overlayProvider.getComposedImage((ResourceAttachmentChange)object, ret);
		ret = overlayImage(object, matchImage);

		return ret;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.provider.ResourceAttachmentChangeItemProvider#getText(java.lang.Object)
	 */
	@Override
	public String getText(Object object) {
		return getStyledText(object).getString();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.provider.IItemStyledLabelProvider#getStyledText(java.lang.Object)
	 */
	public IComposedStyledString getStyledText(Object object) {
		final Match match = ((ResourceAttachmentChange)object).getMatch();
		String value = AdapterFactoryUtil.getText(getRootAdapterFactory(), match.getLeft());
		if (value == null) {
			value = AdapterFactoryUtil.getText(getRootAdapterFactory(), match.getRight());
		}
		if (value == null) {
			value = AdapterFactoryUtil.getText(getRootAdapterFactory(), match.getOrigin());
		}
		if (value == null) {
			value = super.getText(object);
		}

		ComposedStyledString ret = new ComposedStyledString(value);

		DifferenceKind labelValue = ((ResourceAttachmentChange)object).getKind();
		String label = labelValue == null ? "" : labelValue.toString().toLowerCase(); //$NON-NLS-1$

		return ret.append(" [resource contents " + label + "]", Style.DECORATIONS_STYLER); //$NON-NLS-1$ //$NON-NLS-2$

	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.provider.IItemDescriptionProvider#getDescription(java.lang.Object)
	 */
	public String getDescription(Object object) {
		final ResourceAttachmentChange rac = (ResourceAttachmentChange)object;
		final Match match = rac.getMatch();
		String ret = AdapterFactoryUtil.getText(getRootAdapterFactory(), match.getLeft());
		if (ret == null) {
			ret = AdapterFactoryUtil.getText(getRootAdapterFactory(), match.getRight());
		}
		if (ret == null) {
			ret = AdapterFactoryUtil.getText(getRootAdapterFactory(), match.getOrigin());
		}
		if (ret == null) {
			ret = super.getText(object);
		}

		String remotely = "";
		if (rac.getSource() == DifferenceSource.RIGHT) {
			remotely = "remotely ";
		}

		DifferenceKind labelValue = rac.getKind();
		final String hasBeen = " has been ";

		switch (labelValue) {
			case ADD:
				ret += hasBeen + remotely + "added to resource contents";
				break;
			case DELETE:
				ret += hasBeen + remotely + "deleted from resource contents";
				break;
			case MOVE:
				ret += hasBeen + remotely + "moved in resource contents";
				break;
			default:
				throw new IllegalStateException("Unsupported " + DifferenceKind.class.getSimpleName()
						+ " value: " + rac.getKind());
		}
		return ret;
	}
}

Back to the top