Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 86ea40aa0ee17d2f28db643c9b1c146101fa6016 (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
/*****************************************************************************
 * Copyright (c) 2011 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:
 *  Vincent Lorenzo (CEA LIST) Vincent.Lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.merger.provider;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyReferenceRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.MoveRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
import org.eclipse.papyrus.uml.compare.merger.utils.MoveWithIndexCommand;
import org.eclipse.papyrus.uml.compare.merger.utils.MoveWithIndexRequest;

//TODO : try to merge with the PapyrusTableCommandFactory?
public class PapyrusMergeCommandProvider {

	public static PapyrusMergeCommandProvider INSTANCE = new PapyrusMergeCommandProvider();

	private PapyrusMergeCommandProvider() {

	}

	public Command getDestroyCommand(final TransactionalEditingDomain domain, final EObject element) {
		final IEditCommandRequest request = new DestroyElementRequest(domain, element, false);
		return getCommand(element, request);
	}

	//TODO elementToEdit and targetContainer are the same
	public Command getMoveCommand(final TransactionalEditingDomain domain, final EObject elementToEdit, final EObject targetContainer, final EReference targetFeature, final EObject elementToMove) {
		final IEditCommandRequest request = new MoveRequest(domain, targetContainer, targetFeature, elementToMove);
		return getCommand(elementToEdit, request);
	}

	private Command getCommand(final EObject elementToEdit, final IEditCommandRequest request) {
		final IElementEditService provider = ElementEditServiceUtils.getCommandProvider(elementToEdit);
		if(request instanceof MoveWithIndexRequest) {//TODO remove this test when the move with index will be in the service edit
			return new GMFtoEMFCommandWrapper(new MoveWithIndexCommand((MoveRequest)request));
		}
		if(provider != null) {
			return new GMFtoEMFCommandWrapper(provider.getEditCommand(request));
		}
		return null;
	}

	public Command getDestroyReferenceCommand(final TransactionalEditingDomain editingDomain, final EObject container, final EReference containingFeature, final EObject referencedObject, final boolean confirmationRequired) {
		final IEditCommandRequest request = new DestroyReferenceRequest(editingDomain, container, containingFeature, referencedObject, confirmationRequired);
		return getCommand(container, request);
	}

	public Command getSetCommand(final TransactionalEditingDomain domain, final EObject element, final EStructuralFeature feature, final Object value) {
		final IEditCommandRequest request = new SetRequest(domain, element, feature, value);
		return getCommand(element, request);
	}


	/**
	 * Returns the command to set the id of the source object to the new object. This command do something, only if the source and the target aren't
	 * owned by the same resource
	 * 
	 * @param domain
	 * @param sourceElement
	 * @param targetElement
	 * @return
	 *         the command to set the id of the source object to the new object
	 */
	public Command getSetXMIIDCommand(final TransactionalEditingDomain domain, final EObject sourceElement, final EObject targetElement) {
		//TODO change for an EMFCommand
		return new GMFtoEMFCommandWrapper(new AbstractTransactionalCommand(domain, "Set XMI Command", null) {

			@Override
			protected CommandResult doExecuteWithResult(final IProgressMonitor monitor, final IAdaptable info) throws ExecutionException {
				final Resource sourceResource = sourceElement.eResource();
				final Resource targetResource = targetElement.eResource();
				if(sourceElement != null && targetElement != null && sourceResource instanceof XMIResource && targetResource instanceof XMIResource) {
					//TODO : this test is commented because the result of this command is worse with the test than without...
					//					if(sourceResource != targetResource) {
						final String xmi_id = EMFHelper.getXMIID(sourceElement);
						((XMIResource)targetElement.eResource()).setID(sourceElement, xmi_id);
					//					}
				}
				return CommandResult.newOKCommandResult();
			}
		});

	}

	public Command getAddToResourceCommand(final TransactionalEditingDomain domain, final Resource res, final EObject eobjectToAdd) {
		//TODO change for an EMFCommand
		return new GMFtoEMFCommandWrapper(new AbstractTransactionalCommand(domain, "Add EObject to Resource Command", null) {

			@Override
			protected CommandResult doExecuteWithResult(final IProgressMonitor monitor, final IAdaptable info) throws ExecutionException {
				res.getContents().add(eobjectToAdd);
				return CommandResult.newOKCommandResult();
			}
		});
	}

	//TODO elementToEdit and targetContainer are the same
	public Command getMoveWithIndexCommand(final TransactionalEditingDomain domain, final EObject elementToEdit, final EObject targetContainer, final EReference targetFeature, final EObject elementToMove, final int index, final boolean reorder) {
		final IEditCommandRequest request = new MoveWithIndexRequest(domain, targetContainer, targetFeature, elementToMove, index, reorder);
		return getCommand(elementToEdit, request);
	}

}

Back to the top