Skip to main content
summaryrefslogtreecommitdiffstats
blob: c73219067723d7987bbd270ba755d680d511bd23 (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
/*******************************************************************************
 * Copyright (c) 2009 Obeo.
 * 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:
 *     Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.papyrus.navigator.actions;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.jface.action.Action;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IPageMngr;
import org.eclipse.papyrus.infra.core.utils.EditorUtils;
import org.eclipse.papyrus.navigator.internal.Activator;

/**
 * Action used to duplicate the given diagram
 * 
 * @author <a href="mailto:jerome.benois@obeo.fr">Jerome Benois</a>
 */
public class DuplicateDiagramAction extends Action {

	Diagram diagram;

	IPageMngr pageMngr;

	public DuplicateDiagramAction(IPageMngr pageMngr, Diagram diagram) {
		this.diagram = diagram;
		this.pageMngr = pageMngr;

		setImageDescriptor(Activator.getImageDescriptor("icons/etool16/duplicate.png"));
		setText("Duplicate");
		setEnabled(true);
	}

	/**
	 * Duplicate the given diagram
	 * 
	 * @see org.eclipse.jface.action.Action#run()
	 */
	@Override
	public void run() {
		TransactionalEditingDomain editingDomain = EditorUtils.getTransactionalEditingDomain();
		if(editingDomain != null) {

			// Create a compound command containing removing of the sash and removing from GMF
			// resource.
			CompoundCommand command = new CompoundCommand();

			// Clone the current diagram
			final Diagram newDiagram = (Diagram)EcoreUtil.copy(diagram);
			// Give a new name
			newDiagram.setName("Copy of " + diagram.getName());

			Command addGmfDiagramCmd = new AddCommand(editingDomain, diagram.eResource().getContents(), newDiagram);
			// EMFCommandOperation operation = new EMFCommandOperation(editingDomain,
			// addGmfDiagramCmd);

			Command sashOpenComd = new RecordingCommand(editingDomain) {

				@Override
				protected void doExecute() {
					pageMngr.openPage(newDiagram);
				}
			};

			// TODO : synchronize with Cedric
			// command.append(operation.getCommand());
			command.append(addGmfDiagramCmd);
			command.append(sashOpenComd);
			// Execute changes through a Command so that Undo/Redo is supported
			editingDomain.getCommandStack().execute(command);

		}

	}

}

Back to the top