Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 71cd6866ae4df061be35238ef43d26e866114a58 (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
/*****************************************************************************
 * Copyright (c) 2014 CEA LIST and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   CEA LIST - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.uml.tools.databinding;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
import org.eclipse.papyrus.uml.tools.Activator;
import org.eclipse.uml2.uml.Artifact;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.DataType;
import org.eclipse.uml2.uml.Interface;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Signal;
import org.eclipse.uml2.uml.StructuredClassifier;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.UMLPackage;

/**
 *
 * This class provides methods to set the owner for UML Association
 *
 */
public class OwnedAttributeHelper {

	/**
	 *
	 * @param type
	 *            a UML type
	 * @return
	 * 		the owned attribute feature for the given type, or <code>null</code> when not found
	 * @since 4.1
	 */
	public static final EStructuralFeature getOwnedAttributeFeatureForType(final Type type) {
		if (null == type) {
			Activator.log.warn("The type is null!"); //$NON-NLS-1$
		}

		if (type instanceof StructuredClassifier) {
			return UMLPackage.eINSTANCE.getStructuredClassifier_OwnedAttribute();
		}
		if (type instanceof Interface) {
			return UMLPackage.eINSTANCE.getInterface_OwnedAttribute();
		}
		if (type instanceof DataType) {
			return UMLPackage.eINSTANCE.getDataType_OwnedAttribute();
		}
		if (type instanceof Artifact) {
			return UMLPackage.eINSTANCE.getArtifact_OwnedAttribute();
		}
		if (type instanceof Signal) {
			return UMLPackage.eINSTANCE.getSignal_OwnedAttribute();
		}


		// Unknown type : we try to find the feature reflexively
		final String eClassName = type.eClass().getName();
		Activator.log.warn(String.format("Unknown type : %s", eClassName)); //$NON-NLS-1$
		EStructuralFeature feature = type.eClass().getEStructuralFeature("ownedAttribute"); //$NON-NLS-1$
		if (feature == null) {
			Activator.log.warn(String.format("Cannot find a valid feature for type %s.", eClassName)); //$NON-NLS-1$
		}
		return feature;
	}

	/**
	 *
	 * @param type
	 *            a UML type
	 * @return
	 * 		the owned attribute feature for the given type, or <code>null</code> when not found
	 *
	 * @deprecated since 4.1
	 *             use org.eclipse.papyrus.uml.tools.databinding.OwnedAttributeHelper.getOwnedAttributeFeatureForType(Type) API instead
	 *             This method will be removed in Papyrus 5.0, see bug 540875
	 */
	@Deprecated
	public static EStructuralFeature getFeatureForType(Type type) {
		return getOwnedAttributeFeatureForType(type);
	}

	/**
	 *
	 * @param association
	 *            an association
	 * @param memberEnd
	 *            the member end of the association
	 * @return
	 * 		the command to use to set the owner of the association
	 */
	public static ICommand getSetTypeOwnerForAssociationAttributeCommand(Association association, Property memberEnd) {
		ICommand command = null;
		Type ownerType;
		List<Type> ownerList = association.getEndTypes();

		if (ownerList.get(0).equals(memberEnd.getType()) && ownerList.size() > 1) {
			ownerType = ownerList.get(1);
		} else {
			ownerType = ownerList.get(0);
		}

		EStructuralFeature ownedAttributeFeature = getOwnedAttributeFeatureForType(ownerType);
		if (ownedAttributeFeature != null) {

			List<Property> attributeList = new ArrayList<>();
			attributeList.addAll((EList<Property>) ownerType.eGet(ownedAttributeFeature));
			attributeList.add(memberEnd);

			IElementEditService provider = ElementEditServiceUtils.getCommandProvider(ownerType);
			if (provider != null) {
				SetRequest request = new SetRequest(ownerType, ownedAttributeFeature, memberEnd);

				command = provider.getEditCommand(request);
			}
		}
		return command;
	}
}

Back to the top