Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2b58fc63f94f69e19b1a0f4c919785a138b78a8b (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
/*****************************************************************************
 * Copyright (c) 2016 CEA LIST and others.
 * 
 * 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.uml.propertylifecycle.processors;

import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.AbstractEditCommandRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientRelationshipRequest;
import org.eclipse.papyrus.propertylifecycle.commands.LifecycleSetCommand;
import org.eclipse.papyrus.propertylifecycle.utils.CommandValueProcessor;
import org.eclipse.papyrus.uml.propertylifecycle.Activator;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Property;

/**
 *
 */
public class AssociationProcessor implements CommandValueProcessor {

	Association association;

	@Override
	public ICommand setValueFromRequest(String featureLabel, boolean isImmutable, AbstractEditCommandRequest request) {
		return setName(request, isImmutable);
	}

	public AssociationProcessor() {

	}

	public ICommand setName(AbstractEditCommandRequest request, boolean isImmutable) {
		if (isImmutable) {
			Activator.log.trace(Activator.PLCSTRATEGY_TRACE, "isJambon: " + isImmutable);
		} else {
			Activator.log.trace(Activator.PLCSTRATEGY_TRACE, "isJambon: " + isImmutable);
		}
		ICommand gmfCommand = null;

		if (request instanceof ConfigureRequest) {
			ConfigureRequest confRequest = (ConfigureRequest) request;

			association = (Association) confRequest.getElementToConfigure();
			Classifier sourceType = getSourceOwnerType(confRequest);
			Classifier targetType = getTargetOwnerType(confRequest);

			String initializedName = "A_" + sourceType.getName() + "_" + targetType.getName();//$NON-NLS-1$ //$NON-NLS-2$
			EStructuralFeature feature = association.eClass().getEStructuralFeature("name");

			ICommand setCommand = new LifecycleSetCommand("AssociationConfigure", association, feature, initializedName);
			gmfCommand = CompositeCommand.compose(gmfCommand, setCommand);
		}

		if (request instanceof ReorientRelationshipRequest) {
			ReorientRelationshipRequest reorientRequest = (ReorientRelationshipRequest) request;

			association = (Association) reorientRequest.getRelationship();
			EStructuralFeature feature = association.eClass().getEStructuralFeature("name");
			if (association == null || feature == null) {
				return gmfCommand;
			}

			Property sourceProperty = association.getMemberEnds().get(1);
			Property targetProperty = association.getMemberEnds().get(0);
			String initializedName = null;

			if (reorientRequest.getDirection() == ReorientRelationshipRequest.REORIENT_SOURCE) {
				sourceProperty.setName(getReorientOwnerType(reorientRequest).getName().toLowerCase());
				initializedName = "A_" + getReorientOwnerType(reorientRequest).getName() + "_" + targetProperty.getType().getName();
			}
			if (reorientRequest.getDirection() == ReorientRelationshipRequest.REORIENT_TARGET) {
				targetProperty.setName(getReorientOwnerType(reorientRequest).getName().toLowerCase());
				initializedName = "A_" + sourceProperty.getType().getName() + "_" + getReorientOwnerType(reorientRequest).getName();
			}

			// The strategies will be browsed by this setRequest and a command will be constructed in the PropertyProcessor if any match
			ICommand setCommand = new LifecycleSetCommand("AssociationReorient", association, feature, initializedName);
			gmfCommand = CompositeCommand.compose(gmfCommand, setCommand);
		}

		return gmfCommand;
	}


	/**
	 * This method provides the new owner type
	 * 
	 * @param request
	 *            The {@link ConfigureRequest}
	 * @return
	 * 		The Classifier owning the new end or null if none exists
	 */
	protected Classifier getReorientOwnerType(ReorientRelationshipRequest request) {
		Object paramObject = request.getNewRelationshipEnd();
		if (paramObject instanceof Classifier) {
			return (Classifier) paramObject;
		}

		return null;
	}

	/**
	 * This method provides the source type
	 * 
	 * @param request
	 *            The {@link ConfigureRequest}
	 * @return
	 * 		The Classifier owning the new end or null if none exists
	 */
	protected Classifier getSourceOwnerType(ConfigureRequest request) {
		Object paramObject = request.getParameter(CreateRelationshipRequest.SOURCE);
		if (paramObject instanceof Classifier) {
			return (Classifier) paramObject;
		}

		return null;
	}

	/**
	 * This method provides the target type
	 * 
	 * @param request
	 *            The {@link ConfigureRequest}
	 * @return
	 * 		The Classifier owning the new end or null if none exists
	 */
	protected Classifier getTargetOwnerType(ConfigureRequest request) {
		Object paramObject = request.getParameter(CreateRelationshipRequest.TARGET);
		if (paramObject instanceof Classifier) {
			return (Classifier) paramObject;
		}

		return null;
	}

}

Back to the top