Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8b855e90badaf836b8c0e8ada964667292c6825d (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
/*****************************************************************************
 * Copyright (c) 2011 Atos Origin.
 *
 *    
 * 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:
 *   Atos Origin - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.diagram.common.groups.commands;

import java.util.Collection;
import java.util.List;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.papyrus.diagram.common.groups.core.groupcontainment.GroupContainmentRegistry;
import org.eclipse.papyrus.diagram.common.groups.groupcontainment.AbstractContainerNodeDescriptor;

/**
 * Command to update referencing groups for a child element.
 * (The child element is not necessary already created, it has just to be available through an adapter at runtime execution).
 * 
 * @author arthur daussy
 */
public class SetUpReferencesCommand extends AbstractTransactionalCommand {

	private IAdaptable elementAdapter;

	private List<IGraphicalEditPart> graphicalParent;


	/**
	 * Command constructor
	 * 
	 * @param domain
	 *        editing domain
	 * @param label
	 *        command label
	 * @param adapter
	 *        adapter to recover created element
	 */
	public SetUpReferencesCommand(TransactionalEditingDomain domain, String label, IAdaptable adapter, List<IGraphicalEditPart> _graphicalParents) {
		super(domain, label, null);
		elementAdapter = adapter;
		this.graphicalParent = _graphicalParents;
	}

	/**
	 * 
	 * @see org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#doExecuteWithResult(org.eclipse.core.runtime.IProgressMonitor,
	 *      org.eclipse.core.runtime.IAdaptable)
	 * 
	 * @param arg0
	 * @param arg1
	 * @return
	 * @throws ExecutionException
	 */
	@SuppressWarnings("unchecked")
	@Override
	protected CommandResult doExecuteWithResult(IProgressMonitor arg0, IAdaptable arg1) throws ExecutionException {
		Object createdElement = elementAdapter.getAdapter(EObject.class);
		if(createdElement instanceof EObject) {
			EObject eObjectCreatedElement = (EObject)createdElement;
			for(IGraphicalEditPart parent : graphicalParent) {
				EObject eObjectSourceReference = parent.resolveSemanticElement();
				AbstractContainerNodeDescriptor desc = GroupContainmentRegistry.getContainerDescriptor(parent);
				List<EReference> refs = desc.getReferenceFor(eObjectCreatedElement.eClass());
				for(EReference ref : refs) {
					if(ref != null && ref.isMany()) {
						Collection<EObject> collection = (Collection<EObject>)eObjectSourceReference.eGet(ref);
						if(!collection.contains(eObjectCreatedElement)) {
							collection.add(eObjectCreatedElement);
						}
					} else if(ref != null && !ref.isMany()) {
						eObjectSourceReference.eSet(ref, eObjectCreatedElement);
					}
				}

			}

		}

		return CommandResult.newOKCommandResult();
	}



}

Back to the top