Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8da4ac12ec778a19d1144b2a0ff3d23944b8667a (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) 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 : 372745: [ActivityDiagram] Major refactoring group framework
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.activity.commands;

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

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.workspace.util.WorkspaceSynchronizer;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.diagram.core.internal.l10n.DiagramCoreMessages;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;

public class ChangeParentCommand extends AbstractTransactionalCommand {

	private IAdaptable parent;

	private IAdaptable child;

	private int index;

	/**
	 * Creates a new <code>AddCommand</code>
	 *
	 * @param editingDomain
	 *            the editing domain through which model changes are made
	 * @param parent
	 *            The parent view adapter
	 * @param child
	 *            The child view adapter
	 */
	public ChangeParentCommand(TransactionalEditingDomain editingDomain, IAdaptable parent, IAdaptable child) {
		this(editingDomain, parent, child, ViewUtil.APPEND);
	}

	/**
	 * Creates a new <code>AddCommand</code>
	 *
	 * @param editingDomain
	 *            the editing domain through which model changes are made
	 * @param parent
	 *            The parent view adapter
	 * @param child
	 *            The child view adapter
	 * @param index
	 *            the child insertion index
	 */
	public ChangeParentCommand(TransactionalEditingDomain editingDomain, IAdaptable parent, IAdaptable child, int index) {
		super(editingDomain, DiagramCoreMessages.AddCommand_Label, null);
		assert null != parent : "Null parent in AddCommand";//$NON-NLS-1$
		assert null != child : "Null child in AddCommand";//$NON-NLS-1$
		this.parent = parent;
		this.child = child;
		this.index = index;
	}

	@Override
	public List getAffectedFiles() {
		View view = (View) parent.getAdapter(View.class);
		if (view != null) {
			List result = new ArrayList();
			IFile file = WorkspaceSynchronizer.getFile(view.eResource());
			if (file != null) {
				result.add(file);
			}
			return result;
		}
		return super.getAffectedFiles();
	}

	/**
	 * executes the command; which will get the child and the containaer from
	 * the <code>IAdaptable<code> and then insert the child at the given index
	 * in the containers child list.
	 */
	@Override
	protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
		/**
		 * Change graphical parent
		 */
		View childView = child.getAdapter(View.class);
		View parentView = parent.getAdapter(View.class);
		EObject oldParent = childView.eContainer();
		if (oldParent instanceof View) {
			((View) oldParent).removeChild(childView);
		}
		if (index == ViewUtil.APPEND) {
			parentView.insertChild(childView);
		} else {
			parentView.insertChildAt(childView, index);
		}
		/**
		 * Change coordinate
		 */
		ViewUtil.setStructuralFeatureValue(childView, NotationPackage.eINSTANCE.getLocation_X(), Integer.valueOf(110));
		ViewUtil.setStructuralFeatureValue(childView, NotationPackage.eINSTANCE.getLocation_Y(), Integer.valueOf(110));
		return CommandResult.newOKCommandResult();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.gmf.runtime.common.core.command.ICommand#getLabel()
	 */
	@Override
	public String getLabel() {
		return DiagramCoreMessages.AddCommand_Label;
	}

	protected Point getAbsoluteLocation(View v) {
		Point result = getLocation(v);
		EObject container = v.eContainer();
		while (container instanceof View) {
			View parentView = (View) container;
			Point parentCoordinate = getLocation(parentView);
			if (parentCoordinate != null) {
				result.translate(parentCoordinate);
			}
			container = parentView.eContainer();
		}
		return result;
	}

	protected Point getLocation(View v) {
		Integer x = (Integer) ViewUtil.getStructuralFeatureValue(v, NotationPackage.eINSTANCE.getLocation_X());
		Integer y = (Integer) ViewUtil.getStructuralFeatureValue(v, NotationPackage.eINSTANCE.getLocation_Y());
		return new Point(x, y);
	}
}

Back to the top