Skip to main content
summaryrefslogtreecommitdiffstats
blob: 390ef113f4df778ba50b238065bce8fb5f178996 (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
/*****************************************************************************
 * Copyright (c) 2011 Atos.
 *
 *    
 * 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:
 *   Arthur Daussy (Atos) - Initial API and implementation
 *   Arthur Daussy - 371712 : [Activitydiagram] Papyrus should provide a way to manually resynchronize pins and parameters on Call Actions
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.activity.commands;

import org.eclipse.emf.common.util.EMap;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EModelElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.papyrus.infra.emf.commands.CreateEAnnotationCommand;
import org.eclipse.papyrus.uml.diagram.activity.helper.IPinToParameterLinkCommand;
import org.eclipse.papyrus.uml.diagram.activity.helper.datastructure.ILinkPinToTarget;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Pin;

/**
 * Command to create or update the EAnnoation which store the link from a {@link Pin} to a {@link Parameter}
 * 
 * @author arthur daussy
 * 
 */
public class CreatePinToParameterLinkEAnnotation extends CreateEAnnotationCommand implements IPinToParameterLinkCommand {

	/**
	 * {@link Parameter}
	 */
	private Element target;

	/**
	 * {@link EAnnotation}
	 */
	protected EAnnotation eAnnotation;

	/**
	 * Pin which will old the {@link EAnnotation}
	 */
	protected EModelElement modelOwner;

	public CreatePinToParameterLinkEAnnotation(TransactionalEditingDomain domain, Pin object, Element target) {
		super(domain, object, PIN_TO_PARAMETER_LINK);
		this.target = target;
		this.modelOwner = object;
	}

	public CreatePinToParameterLinkEAnnotation(TransactionalEditingDomain domain, ILinkPinToTarget link) {
		super(domain, link.getPin(), PIN_TO_PARAMETER_LINK);
		this.target = link.getTarget();
		this.modelOwner = link.getPin();
	}

	@Override
	protected EAnnotation createEAnnotation() {
		eAnnotation = super.createEAnnotation();
		return eAnnotation;
	}

	@Override
	protected void doExecute() {
		//try to get an old EAnnotation
		eAnnotation = modelOwner.getEAnnotation(PIN_TO_PARAMETER_LINK);
		if(eAnnotation == null) {
			super.doExecute();
		}
		EMap<String, String> details = eAnnotation.getDetails();
		if(details != null) {
			details.put(PIN_TO_PARAMETER_LINK, getXmiID(target));
		}
	}

	/**
	 * Get the XMI id to identify the parameter
	 * 
	 * @param p
	 *        {@link Parameter} to identify
	 * @return XMI id og the {@link Parameter}
	 */
	protected String getXmiID(EObject p) {
		Resource ressource = p.eResource();
		if(ressource instanceof XMIResource) {
			return ((XMIResource)ressource).getID(p);
		}
		return NO_PARAMETER_ID;
	}

	@Override
	public boolean canExecute() {
		return super.canExecute() && modelOwner != null && target != null;
	}
}

Back to the top