Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 957e653a1ad432f5056f3054e94fa18530add47d (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) 2012 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:
 *  CEA LIST - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.component.custom.handler;

import java.util.ArrayList;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.emf.edit.command.DeleteCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.infra.emf.gmf.command.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.gmfdiag.common.utils.ServiceUtilsForEditPart;
import org.eclipse.papyrus.uml.diagram.common.commands.SemanticAdapter;
import org.eclipse.papyrus.uml.diagram.component.custom.command.DeferredSetViewCommand;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

/**
 * This class is an abstract class in charge to transform a editpart into another editpart
 *
 */
public abstract class ChangeShapeHandler extends AbstractHandler {

	protected TransactionalEditingDomain transactionalEditingDomain = null;

	protected org.eclipse.uml2.uml.Element selectedElement = null;
	protected String newType;

	/**
	 *
	 * Constructor.
	 *
	 */
	public ChangeShapeHandler() {
		super();
	}

	protected abstract AbstractTransactionalCommand getChangeShapeCommand(final GraphicalEditPart editPart);

	/**
	 *
	 * @return null or the selected editPart
	 */
	protected GraphicalEditPart getSelectedGraphicalEditpart() {
		IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (activeWorkbenchWindow != null) {
			ISelectionService selectionService = activeWorkbenchWindow.getSelectionService();
			ISelection selection = selectionService.getSelection();
			if (selection instanceof IStructuredSelection) {
				Object selectedobject = ((IStructuredSelection) selection).getFirstElement();
				if (selectedobject instanceof GraphicalEditPart) {
					return (GraphicalEditPart) selectedobject;
				}
			}
		}
		return null;
	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		final GraphicalEditPart editPart = getSelectedGraphicalEditpart();
		ServiceUtilsForEditPart util = ServiceUtilsForEditPart.getInstance();
		try {
			transactionalEditingDomain = util.getTransactionalEditingDomain(editPart);
		} catch (Exception e) {
			System.err.println("impossible to get the Transactional Editing Domain " + e);
		}
		try {
			editPart.getEditingDomain().runExclusive(new Runnable() {

				@Override
				public void run() {
					Display.getCurrent().asyncExec(new Runnable() {

						@Override
						public void run() {
							// get Links
							View view = (View) (editPart.getModel());
							TransactionalEditingDomain domain = editPart.getEditingDomain();
							@SuppressWarnings("unchecked")
							ArrayList<Edge> targetEdge = new ArrayList<Edge>(view.getTargetEdges());
							@SuppressWarnings("unchecked")
							ArrayList<Edge> sourceEdge = new ArrayList<Edge>(view.getSourceEdges());

							// create new Shape
							AbstractTransactionalCommand createCommand = getChangeShapeCommand(editPart);
							org.eclipse.emf.common.command.Command deleteCommand = DeleteCommand.create(editPart.getEditingDomain(), view);
							org.eclipse.emf.common.command.CompoundCommand compoundCommand = new org.eclipse.emf.common.command.CompoundCommand("change Shape");
							compoundCommand.append(new GMFtoEMFCommandWrapper(createCommand));

							// remove old Shape
							compoundCommand.append(deleteCommand);

							// Add links
							DeferredSetViewCommand deferredSetCommand = new DeferredSetViewCommand(domain, (SemanticAdapter) createCommand.getCommandResult().getReturnValue(), NotationPackage.eINSTANCE.getView_SourceEdges(), sourceEdge);
							compoundCommand.append(new GMFtoEMFCommandWrapper(deferredSetCommand));
							deferredSetCommand = new DeferredSetViewCommand(domain, (SemanticAdapter) createCommand.getCommandResult().getReturnValue(), NotationPackage.eINSTANCE.getView_TargetEdges(), targetEdge);
							compoundCommand.append(new GMFtoEMFCommandWrapper(deferredSetCommand));

							editPart.getEditingDomain().getCommandStack().execute(compoundCommand);
						}
					});
				}
			});
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return null;
	}
}

Back to the top