Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c8a4a03a641922e9379032fcfee0b6671614f6b5 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*****************************************************************************
 * Copyright (c) 2009 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:
 *  Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.pastemanager.command;

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.util.FeatureMapUtil;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.DuplicateElementsRequest;
import org.eclipse.gmf.runtime.notation.Bounds;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.LayoutConstraint;
import org.eclipse.gmf.runtime.notation.Shape;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.emf.utils.BusinessModelResolver;
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;


/**
 * this command is used to wrap a copy command. it allows set a new owner for views.
 */
public class PapyrusDuplicateWrapperCommand extends AbstractTransactionalCommand {

	/** the new container for the shape */
	protected View container = null;

	/** Command that owns this duplicate command */
	protected ICommand duplicateEObjectsCommandOwner = null;

	/** list of object to duplicate */
	protected List<Object> eObjectsToBeDuplicated = null;

	/** Constant used as a key for the parameters map of the duplication request */
	public static final String ADDITIONAL_DUPLICATED_ELEMENTS = "Additional_Duplicated_Elements";

	/**
	 * Constructor.
	 * 
	 * @param editingDomain
	 * @param label
	 * @param eObjectsToBeDuplicated
	 * @param subCommand
	 * @param container
	 */
	public PapyrusDuplicateWrapperCommand(TransactionalEditingDomain editingDomain, String label, List<Object> eObjectsToBeDuplicated, ICommandProxy subCommand, View container) {
		super(editingDomain, label, null);
		this.container = container;
		this.eObjectsToBeDuplicated = eObjectsToBeDuplicated;
		this.duplicateEObjectsCommandOwner = lookForDuplicateCommandOwner(subCommand);
	}

	/**
	 * Verifies that the container of all the original objects can contain
	 * multiple objects.
	 */
	@Override
	@SuppressWarnings("rawtypes")
	public boolean canExecute() {
		for(Iterator iter = eObjectsToBeDuplicated.iterator(); iter.hasNext();) {
			EObject original = (EObject)iter.next();
			//In the case of cut the owner does not exist 
			if(original.eContainer() == null) {
				return true;

			} else {
				EReference reference = original.eContainmentFeature();
				if(reference == null || !FeatureMapUtil.isMany(original.eContainer(), reference)) {
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	@SuppressWarnings("rawtypes")
	protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
		duplicateEObjectsCommandOwner.execute(progressMonitor, info);
		CommandResult result = duplicateEObjectsCommandOwner.getCommandResult();
		//reassociation to the new container
		if(result.getReturnValue() instanceof List) {
			Iterator resultIterator = ((List)result.getReturnValue()).iterator();
			while(resultIterator.hasNext()) {
				Object currentResult = resultIterator.next();

				// the result of a copy is a map
				if(currentResult instanceof Map) {
					Map duplicatedObject = (Map)currentResult;
					Iterator iterator = duplicatedObject.values().iterator();
					// for each view, a container is set if it is null 
					// if this is a shape a new position is set in order to avoid superposition
					while(iterator.hasNext()) {
						Object object = iterator.next();
						if(object instanceof View) {
							View duplicatedView = (View)object;
							if(object instanceof Shape) {
								LayoutConstraint layoutConstraint = ((Shape)object).getLayoutConstraint();
								if(layoutConstraint instanceof Bounds) {
									((Bounds)layoutConstraint).setX(((Bounds)layoutConstraint).getX() + 10);
									((Bounds)layoutConstraint).setY(((Bounds)layoutConstraint).getY() + 10);
								}
							}
							if(duplicatedView.eContainer() == null && container != null) {
								ViewUtil.insertChildView(container, duplicatedView, -1, true);
							}
						}
					}
				}
			}
		} else if(result.getReturnValue() instanceof Map) { // perhaps not a list in case of simple ICommand, result value should be a map
			Map duplicatedObject = (Map)result.getReturnValue();
			Iterator iterator = duplicatedObject.values().iterator();
			// for each view, a container is set if it is null 
			// if this is a shape a new position is set in order to avoid superposition
			while(iterator.hasNext()) {
				Object object = iterator.next();
				if(object instanceof Diagram) {
					Diagram diagramView = (Diagram)object;
					if(container != null && container.eResource() != null) {
						container.eResource().getContents().add(diagramView);
					}

				} else if(object instanceof View) {
					View duplicatedView = (View)object;
					if(object instanceof Shape) {
						LayoutConstraint layoutConstraint = ((Shape)object).getLayoutConstraint();
						if(layoutConstraint instanceof Bounds) {
							((Bounds)layoutConstraint).setX(((Bounds)layoutConstraint).getX() + 10);
							((Bounds)layoutConstraint).setY(((Bounds)layoutConstraint).getY() + 10);
						}
					}
					if(duplicatedView.eContainer() == null && container != null) {
						ViewUtil.insertChildView(container, duplicatedView, -1, true);
					}
				}
			}

			ICommand externalObjectsDuplicateCommand = getExternalObjectsDuplicateCommand((Map<?, ?>)result.getReturnValue());
			if(externalObjectsDuplicateCommand != null && externalObjectsDuplicateCommand.canExecute()) {
				IStatus status = externalObjectsDuplicateCommand.execute(progressMonitor, info);
				if(!status.isOK()) {
					return CommandResult.newErrorCommandResult(status.getException());
				}
			}
		}

		return result;
	}

	/**
	 * this class is used to look for the basic eobject duplicate command
	 * 
	 * @param command
	 *        that contains normally the duplicated command
	 * @return the duplicate command
	 */
	protected ICommand lookForDuplicateCommandOwner(ICommandProxy command) {
		return command.getICommand();
	}

	/**
	 * Returns the list of external objects to duplicate
	 * 
	 * @return the list of external objects to duplicate or an empty list if not elements are found to add.
	 */
	protected ICommand getExternalObjectsDuplicateCommand(Map duplicatedElementsMap) {
		CompositeCommand result = new CompositeCommand("Duplicate External Objects");
		Set<Object> duplicatedExternalElements = new HashSet<Object>();

		for(Object o : duplicatedElementsMap.keySet()) {
			if(o instanceof EObject) {
				EObject object = (EObject)o;
				DuplicateElementsRequest request = new DuplicateElementsRequest(Collections.singletonList(object));
				request.setAllDuplicatedElementsMap(duplicatedElementsMap);
				request.setParameter(PapyrusDuplicateWrapperCommand.ADDITIONAL_DUPLICATED_ELEMENTS, duplicatedExternalElements);
				request.setParameter("Target_Owner", BusinessModelResolver.getInstance().getBusinessModel(container));
				IElementEditService service = ElementEditServiceUtils.getCommandProvider(object);
				ICommand command = service.getEditCommand(request);
				if(command != null) {
					result.add(command);
				}
			}
		}

		return (result.isEmpty()) ? null : result.reduce();
	}
}

Back to the top