Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f8a17f5850a04821d1afd2f6265903421601444e (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
/*******************************************************************************
 * 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.dnd;

import java.util.List;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.infra.core.utils.EditorUtils;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.TransferData;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.navigator.CommonDropAdapter;
import org.eclipse.ui.views.navigator.LocalSelectionTransfer;
import org.eclipse.uml2.common.edit.command.ChangeCommand;
import org.eclipse.uml2.uml.Namespace;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageableElement;

/**
 * This class handle Drop events in ModelExplorer view.
 * 
 * @author <a href="mailto:jerome.benois@obeo.fr">Jerome Benois</a>
 */
public class CommonDropAdapterAssistant extends org.eclipse.ui.navigator.CommonDropAdapterAssistant {

	public CommonDropAdapterAssistant() {
	}

	@Override
	public IStatus handleDrop(CommonDropAdapter dropAdapter, DropTargetEvent dropTargetEvent, Object dropTarget) {
		Package targetElement = (Package)dropTarget;
		if(LocalSelectionTransfer.getInstance().isSupportedType(dropAdapter.getCurrentTransfer())) {
			switch(dropAdapter.getCurrentOperation()) {
			case DND.DROP_MOVE:
				handleDropMove(targetElement);
				break;
			// case DND.DROP_COPY:
			// break;
			}

		}
		return null;
	}

	@Override
	public IStatus validateDrop(Object target, int operation, TransferData transferType) {
		if(target instanceof Package) {
			return Status.OK_STATUS;
		}
		return Status.CANCEL_STATUS;
	}

	private void handleDropMove(final Package target) {
		ISelection s = LocalSelectionTransfer.getInstance().getSelection();
		if(s instanceof IStructuredSelection) {
			List<?> selectedElements = ((IStructuredSelection)s).toList();
			for(Object o : selectedElements) {
				if(o instanceof PackageableElement) {
					PackageableElement element = (PackageableElement)o;
					moveElementTo(element, target);
				}
			}
		}
	}

	private void moveElementTo(final PackageableElement element, final Package target) {
		TransactionalEditingDomain editingDomain = getEditingDomain();
		if(editingDomain != null && (element.getNamespace() instanceof Package)) {
			ChangeCommand changeCommand = new ChangeCommand(editingDomain, new Runnable() {

				public void run() {
					Namespace oldOwner = element.getNamespace();
					if(oldOwner instanceof Package) {
						// Remove from the old package
						Package pkg = (Package)oldOwner;
						pkg.getPackagedElements().remove(element);

						// Add to the new package
						target.getPackagedElements().add(element);
					}
				}
			}, "Move " + element.getName() + " to " + target.getName());

			if(changeCommand.canExecute()) {
				editingDomain.getCommandStack().execute(changeCommand);
			}
		}
	}

	private TransactionalEditingDomain getEditingDomain() {
		return EditorUtils.getTransactionalEditingDomain();
	}

	private IMultiDiagramEditor getMultiDiagramEditor() {
		IEditorPart editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		if(editorPart instanceof IMultiDiagramEditor) {
			return (IMultiDiagramEditor)editorPart;
		}
		return null;
	}
}

Back to the top