Skip to main content
summaryrefslogtreecommitdiffstats
blob: 785272678353267246bacd7f58650b2802b06419 (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
/*******************************************************************************
 * 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
 *     Philip Langer - fixed bugs 461504, 488618
 *******************************************************************************/
package org.eclipse.emf.compare.provider.spec;

import static com.google.common.base.Strings.isNullOrEmpty;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.compare.AttributeChange;
import org.eclipse.emf.compare.DifferenceKind;
import org.eclipse.emf.compare.DifferenceSource;
import org.eclipse.emf.compare.internal.EMFCompareEditMessages;
import org.eclipse.emf.compare.provider.AttributeChangeItemProvider;
import org.eclipse.emf.compare.provider.IItemDescriptionProvider;
import org.eclipse.emf.compare.provider.IItemStyledLabelProvider;
import org.eclipse.emf.compare.provider.ISemanticObjectLabelProvider;
import org.eclipse.emf.compare.provider.SafeAdapterFactoryItemDelegator;
import org.eclipse.emf.compare.provider.utils.ComposedStyledString;
import org.eclipse.emf.compare.provider.utils.IStyledString;
import org.eclipse.emf.compare.provider.utils.IStyledString.Style;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.FeatureMap;
import org.eclipse.emf.ecore.util.FeatureMapUtil;
import org.eclipse.emf.edit.provider.AdapterFactoryItemDelegator;
import org.eclipse.emf.edit.provider.IItemFontProvider;

/**
 * Specialized {@link AttributeChangeItemProvider} returning nice output for {@link #getText(Object)} and
 * {@link #getImage(Object)}.
 * 
 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
 */
public class AttributeChangeItemProviderSpec extends AttributeChangeItemProvider implements IItemStyledLabelProvider, IItemDescriptionProvider, ISemanticObjectLabelProvider {

	/** The elide length. */
	private static final int ELIDE_LENGTH = 50;

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

	/** The item delegator for attribute change values. */
	private final AdapterFactoryItemDelegator itemDelegator;

	/**
	 * Constructs an AttributeChangeItemProviderSpec with the given factory.
	 * 
	 * @param adapterFactory
	 *            the factory given to the super constructor.
	 */
	public AttributeChangeItemProviderSpec(AdapterFactory adapterFactory) {
		super(adapterFactory);
		itemDelegator = new SafeAdapterFactoryItemDelegator(getRootAdapterFactory());
		overlayProvider = new OverlayImageProvider(getResourceLocator());
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.provider.MatchItemProvider#getImage(java.lang.Object)
	 */
	@Override
	public Object getImage(Object object) {
		AttributeChange attributeChange = (AttributeChange)object;
		Object attributeChangeValueImage = itemDelegator.getImage(attributeChange.getValue());

		if (attributeChangeValueImage == null) {
			attributeChangeValueImage = super.getImage(object);
		}

		Object diffImage = overlayProvider.getComposedImage(attributeChange, attributeChangeValueImage);
		Object ret = overlayImage(object, diffImage);

		return ret;
	}

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

	/**
	 * Returns the name of the attribute linked to the given {@link AttributeChange}.
	 * 
	 * @param attChange
	 *            the given {@link AttributeChange}.
	 * @return the name of the attribute linked to the given {@link AttributeChange}.
	 */
	protected String getAttributeText(final AttributeChange attChange) {
		return attChange.getAttribute().getName();
	}

	/**
	 * Converts to text the given {@link AttributeChange}.
	 * 
	 * @param attChange
	 *            the given {@link AttributeChange}.
	 * @return a nice text from the the given {@link AttributeChange}.
	 */
	protected String getValueText(final AttributeChange attChange) {
		String value;
		Object attValue = attChange.getValue();
		if (FeatureMapUtil.isFeatureMap(attChange.getAttribute())) {
			FeatureMap.Entry entry = (FeatureMap.Entry)attValue;
			EStructuralFeature entryFeature = entry.getEStructuralFeature();
			if (entryFeature instanceof EAttribute) {
				value = EcoreUtil.convertToString(((EAttribute)entryFeature).getEAttributeType(), attValue);
			} else {
				value = itemDelegator.getText(entry.getValue());
			}
		} else {
			value = EcoreUtil.convertToString(attChange.getAttribute().getEAttributeType(), attValue);
		}

		if (isNullOrEmpty(value)) {
			if (attValue instanceof EObject && ((EObject)attValue).eIsProxy()) {
				value = "proxy : " + ((InternalEObject)attValue).eProxyURI().toString(); //$NON-NLS-1$
			} else {
				value = "<null>"; //$NON-NLS-1$
			}
		} else {
			value = org.eclipse.emf.compare.provider.spec.Strings.removeLineBreaks(value);
			value = org.eclipse.emf.compare.provider.spec.Strings.elide(value, ELIDE_LENGTH, "..."); //$NON-NLS-1$
		}
		return value;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.edit.provider.ItemProviderAdapter#getForeground(java.lang.Object)
	 */
	@Override
	public Object getForeground(Object object) {
		AttributeChange attributeChange = (AttributeChange)object;
		switch (attributeChange.getState()) {
			case MERGED:
			case DISCARDED:
				return URI.createURI("color://rgb/156/156/156"); //$NON-NLS-1$
			default:
				return super.getForeground(object);
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.provider.IItemStyledLabelProvider#getStyledText(java.lang.Object)
	 */
	@Override
	public IStyledString.IComposedStyledString getStyledText(Object object) {
		final AttributeChange attChange = (AttributeChange)object;

		final String valueText = getValueText(attChange);

		final String attributeText = getAttributeText(attChange);

		ComposedStyledString ret = new ComposedStyledString();
		final Object attChangeValue = attChange.getValue();
		if (attChangeValue instanceof EObject && ((EObject)attChangeValue).eIsProxy()) {
			Style italic = Style.builder().setFont(IItemFontProvider.ITALIC_FONT).build();
			ret.append(valueText, italic);
		} else {
			ret.append(valueText);
		}
		ret.append(" [" + attributeText, Style.DECORATIONS_STYLER); //$NON-NLS-1$

		switch (attChange.getKind()) {
			case ADD:
				ret.append(" add", Style.DECORATIONS_STYLER); //$NON-NLS-1$
				break;
			case DELETE:
				ret.append(" delete", Style.DECORATIONS_STYLER); //$NON-NLS-1$
				break;
			case CHANGE:
				ret.append(
						" " + ReferenceChangeItemProviderSpec.changeText(attChange, attChange.getAttribute()), //$NON-NLS-1$
						Style.DECORATIONS_STYLER);
				break;
			case MOVE:
				ret.append(" move", Style.DECORATIONS_STYLER); //$NON-NLS-1$
				break;
			default:
				throw new IllegalStateException("Unsupported " + DifferenceKind.class.getSimpleName() //$NON-NLS-1$
						+ " value: " + attChange.getKind()); //$NON-NLS-1$
		}
		ret.append("]", Style.DECORATIONS_STYLER); //$NON-NLS-1$

		return ret;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.provider.ISemanticObjectLabelProvider#getSemanticObjectLabel(java.lang.Object)
	 * @since 4.2
	 */
	public String getSemanticObjectLabel(Object object) {
		final AttributeChange attChange = (AttributeChange)object;
		return getValueText(attChange);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.provider.IItemDescriptionProvider#getDescription(java.lang.Object)
	 */
	public String getDescription(Object object) {
		final AttributeChange attChange = (AttributeChange)object;

		final String valueText = getValueText(attChange);
		final String attributeText = getAttributeText(attChange);

		String hasBeenAndSide = EMFCompareEditMessages.getString("change.local"); //$NON-NLS-1$
		if (attChange.getSource() == DifferenceSource.RIGHT) {
			hasBeenAndSide = EMFCompareEditMessages.getString("change.remote"); //$NON-NLS-1$
		}

		String ret = ""; //$NON-NLS-1$

		switch (attChange.getKind()) {
			case ADD:
				ret = EMFCompareEditMessages.getString("AttributeChangeItemProviderSpec.valueAdded", //$NON-NLS-1$
						valueText, hasBeenAndSide, attributeText);
				break;
			case DELETE:
				ret = EMFCompareEditMessages.getString("AttributeChangeItemProviderSpec.valueRemoved", //$NON-NLS-1$
						valueText, hasBeenAndSide, attributeText);
				break;
			case CHANGE:
				String changeText = ReferenceChangeItemProviderSpec.changeText(attChange,
						attChange.getAttribute());
				ret = EMFCompareEditMessages.getString("AttributeChangeItemProviderSpec.valueChanged", //$NON-NLS-1$
						attributeText, valueText, hasBeenAndSide, changeText);
				break;
			case MOVE:
				ret = EMFCompareEditMessages.getString("AttributeChangeItemProviderSpec.valueMoved", //$NON-NLS-1$
						valueText, hasBeenAndSide, attributeText);
				break;
			default:
				throw new IllegalStateException("Unsupported " + DifferenceKind.class.getSimpleName() //$NON-NLS-1$
						+ " value: " + attChange.getKind()); //$NON-NLS-1$
		}

		return ret;
	}
}

Back to the top