Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 12b21b088c3d6806aa58f0795a9852c7dbe069d5 (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
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 *
 *
 * 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:
 *  Camille Letavernier (camille.letavernier@cea.fr) - Initial API and implementation
 *  Vincent Lorenzo (vincent.lorenzo@cea.fr) - bug 405442
 *****************************************************************************/
package org.eclipse.papyrus.uml.tools.providers;

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

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject;
import org.eclipse.papyrus.infra.services.labelprovider.service.IFilteredLabelProvider;
import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService;
import org.eclipse.papyrus.infra.tools.Activator;
import org.eclipse.papyrus.infra.ui.emf.providers.EMFLabelProvider;
import org.eclipse.papyrus.uml.tools.utils.DataTypeUtil;
import org.eclipse.swt.graphics.Image;

/**
 * A generic Label Provider for EObjects which are instances of UML DataTypes (Defined as EClasses)
 *
 * @author Camille Letavernier
 *
 * @see {@link DataTypeUtil#isDataTypeDefinition(EClass)}
 */
public class GenericDataTypeLabelProvider extends EMFLabelProvider implements IFilteredLabelProvider {

	/**
	 * @since 3.0
	 */
	public static final String SEPARATOR = ","; //$NON-NLS-1$
	
	/**
	 * @since 3.0
	 */
	public static final String DATATYPE_START = "("; //$NON-NLS-1$
	
	/**
	 * @since 3.0
	 */
	public static final String DATATYPE_END = ")"; //$NON-NLS-1$
	
	/**
	 * @since 3.0
	 */
	public static final String COLLECTION_START = "{"; //$NON-NLS-1$
	
	/**
	 * @since 3.0
	 */
	public static final String COLLECTION_END = "}"; //$NON-NLS-1$
	
	/**
	 * @since 3.0
	 */
	public static final String EQUALS = "="; //$NON-NLS-1$

	/**
	 * {@inheritDoc}
	 */
	public boolean accept(Object element) {
		if (element instanceof Collection<?>) {
			for (Object item : ((Collection<?>) element)) {
				if (!accept(item)) {
					return false;
				}
			}
			return !((Collection<?>) element).isEmpty();
		}

		EObject eObject = EMFHelper.getEObject(element);
		if (eObject == null) {
			return false;
		}

		return DataTypeUtil.isDataTypeInstance(eObject);
	}

	@Override
	public String getText(Object element) {
		// now we use the Marte VSL syntax : (propertyName=propertyValue, an otherProperty=value, (propertyFromAReferencedDataType=value) )
		LabelProviderService serv = null;
		if (element instanceof Collection<?> && !((Collection<?>) element).isEmpty()) {
			final Object first = ((Collection<?>) element).iterator().next();
			if (first instanceof EObject) {// always true due to the accept method
				serv = getLabelProviderService((EObject) first);
			}
		} else {
			// always true due to the accept method
			serv = getLabelProviderService((EObject) element);
		}
		if (null == serv) {
			Activator.log.error("LabelProviderService not found for " + element, null); //$NON-NLS-1$
		}

		// Initial implementation. TODO: Improve the label
		final StringBuilder builder = new StringBuilder(DATATYPE_START);
		if (element instanceof Collection<?> && !(((Collection<?>) element).isEmpty())) {
			builder.append(getLabel(serv, element)); // collection management is delegated to this method
		} else {
			final EObject dataTypeInstance = EMFHelper.getEObject(element);
			if (dataTypeInstance != null) {
				final EClass dataTypeDefinition = dataTypeInstance.eClass();
				final Iterator<EStructuralFeature> iter = dataTypeDefinition.getEAllStructuralFeatures().iterator();
				while (iter.hasNext()) {
					final EStructuralFeature feature = iter.next();
					builder.append(feature.getName());
					builder.append(EQUALS);
					final Object value = dataTypeInstance.eGet(feature);
					builder.append(getLabel(serv, value));
					if (iter.hasNext()) {
						builder.append(SEPARATOR);
						builder.append(" ");//$NON-NLS-1$
					}
				}

			}
		}
		builder.append(DATATYPE_END);
		return builder.toString();
	}

	/**
	 * 
	 * @param service
	 *            the label provider service
	 * @param current
	 *            the object for which we want the label
	 * @return
	 * 		the label for the given value
	 * @since 3.0
	 */
	protected String getLabel(final LabelProviderService service, final Object object) {
		final StringBuilder builder = new StringBuilder();
		if (object instanceof Collection<?>) {
			builder.append(COLLECTION_START);
			final Collection<?> coll = (Collection<?>) object;
			final Iterator<?> iter = coll.iterator();
			while (iter.hasNext()) {
				final Object current = iter.next();
				final String value = getLabel(service, current);
				if (value != null) {
					builder.append(value);
				} else {
					builder.append(" "); //$NON-NLS-1$
				}
				if (iter.hasNext()) {
					builder.append(SEPARATOR);
					builder.append(" "); //$NON-NLS-1$
				}
			}
			builder.append(COLLECTION_END);
		} else {
			final String label = service.getLabelProvider(object).getText(object);
			if (label != null) {
				builder.append(label);
			}
		}
		return builder.toString();

	}

	/**
	 * 
	 * @param dataTypeInstance
	 *            a datatype instance
	 * @return
	 * 		the label provider service found or <code>null</code> otherwise
	 * @since 3.0
	 */
	protected LabelProviderService getLabelProviderService(final EObject dataTypeInstance) {
		ServicesRegistry registry = null;
		LabelProviderService service = null;
		try {
			registry = ServiceUtilsForEObject.getInstance().getServiceRegistry(dataTypeInstance);
			service = registry.getService(LabelProviderService.class);
		} catch (ServiceException e) {
			Activator.log.error(e);
		}
		return service;
	}

	@Override
	public Image getImage(Object element) {
		return null;
	}
}

Back to the top