Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a3544ab1b5b7d35bab9d6871fa56689444f279e5 (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
/*****************************************************************************
 * Copyright (c) 2011 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:
 *		
 *		CEA LIST - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.sysml.diagram.blockdefinition.dnd.helper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.papyrus.gmf.diagram.common.edit.policy.ILinkMappingHelper;
import org.eclipse.papyrus.uml.diagram.common.dnd.helper.LinkMappingHelper.CommonSourceUMLSwitch;
import org.eclipse.papyrus.uml.diagram.common.dnd.helper.LinkMappingHelper.CommonTargetUMLSwitch;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Type;

/**
 * The Class LinkMappingHelper is specialization of the link mapping helper for the Class diagram
 */
public class CustomLinkMappingHelper implements ILinkMappingHelper {

	/**
	 * {@inheritDoc}
	 */
	public Collection<?> getSource(Element link) {
		CommonSourceUMLSwitch umlSwitch = new CommonSourceUMLSwitch() {

			public java.util.Collection<?> caseAssociation(org.eclipse.uml2.uml.Association object) {

				java.util.Collection<?> ends = Collections.emptySet();
				
				// Binary associations only in this diagram.
				// Other expectation:
				// - property ends are typed by Classifiers
				// - property ends are type is not null
				
				if(object.getMemberEnds().size() == 2) {
					
					Property semanticTarget = object.getMemberEnds().get(1);
					
					// The proposed graphical target is a representation of the type of
					// the semantic source.
					Type sourceType = semanticTarget.getType();
					if ((sourceType != null) && (sourceType instanceof Classifier)) {
						ends = Arrays.asList(new EObject[]{sourceType});
					}
					
				} else {
					// TODO: log warning here - can only drop binary associations in this diagram...
				}
				
				return ends;
			};

			//			public java.util.Collection<?> caseAssociationClass(org.eclipse.uml2.uml.AssociationClass object) {
			//				return object.getEndTypes();
			//			};

			//			public java.util.Collection<?> caseGeneralizationSet(org.eclipse.uml2.uml.GeneralizationSet object) {
			//				return object.getGeneralizations();
			//			};

			public java.util.Collection<?> caseInterfaceRealization(org.eclipse.uml2.uml.InterfaceRealization object) {
				ArrayList<EObject> result = new ArrayList<EObject>();
				result.add(object.getImplementingClassifier());
				return result;
			};

		};
		return umlSwitch.doSwitch(link);
	}

	/**
	 * {@inheritDoc}
	 */
	public Collection<?> getTarget(Element link) {
		CommonTargetUMLSwitch umlSwitch = new CommonTargetUMLSwitch() {

			public java.util.Collection<?> caseAssociation(org.eclipse.uml2.uml.Association object) {
				
				java.util.Collection<?> ends = Collections.emptySet();
				
				// Binary associations only in this diagram.
				// Other expectation:
				// - property ends are typed by Classifiers
				// - property ends are type is not null
				
				if(object.getMemberEnds().size() == 2) {
					
					Property semanticSource = object.getMemberEnds().get(0);
					
					// The proposed graphical target is a representation of the type of
					// the semantic source.
					Type sourceType = semanticSource.getType();
					if ((sourceType != null) && (sourceType instanceof Classifier)) {
						ends = Arrays.asList(new EObject[]{sourceType});
					}
										
				} else {
					// TODO: log warning here - can only drop binary associations in this diagram...
				}
				
				return ends;
			};

			//			public java.util.Collection<?> caseAssociationClass(org.eclipse.uml2.uml.AssociationClass object) {
			//				return object.getEndTypes();
			//			};

			//			public java.util.Collection<?> caseGeneralizationSet(org.eclipse.uml2.uml.GeneralizationSet object) {
			//				return object.getGeneralizations();
			//			};

			public java.util.Collection<?> caseInterfaceRealization(org.eclipse.uml2.uml.InterfaceRealization object) {
				ArrayList<EObject> result = new ArrayList<EObject>();
				result.add(object.getContract());
				return result;
			};

		};
		return umlSwitch.doSwitch(link);
	}
}

Back to the top