Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9d5ac7d9b6d7eef14849c905abc2933a915bc319 (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
/*****************************************************************************
 * Copyright (c) 2014, 2015 CEA LIST, Christian W. Damus, and others.
 *
 *
 * 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
 *  Christian W. Damus - bug 468079
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.developper.mde.handler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.editor.PapyrusMultiDiagramEditor;
import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForHandlers;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Package;

/**
 * Abstract handler in order to connect to papyrus
 *
 */
public abstract class IDMAbstractHandler extends AbstractHandler {

	protected TransactionalEditingDomain transactionalEditingDomain = null;
	protected PapyrusMultiDiagramEditor papyrusEditor;
	private static Map<Element, Element> elt2DocElt = new HashMap<Element, Element>();
	private static Map<String, String> href2DocHREF = new HashMap<String, String>();
	private static Map<Package, Package> Toc2DocElt = new HashMap<Package, Package>();

	/**
	 * get the root package
	 *
	 * @param elem
	 * @return the root package
	 */
	public Package getToPackage(Element elem) {
		Package tmp = elem.getNearestPackage();
		while (tmp.getOwner() != null && (tmp.getOwner() instanceof Package)) {
			tmp = (Package) tmp.getOwner();
		}
		return tmp;
	}

	// @Override
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		try {
			papyrusEditor = (PapyrusMultiDiagramEditor) ServiceUtilsForHandlers.getInstance().getService(IMultiDiagramEditor.class, event);
			transactionalEditingDomain = ServiceUtilsForHandlers.getInstance().getService(org.eclipse.emf.transaction.TransactionalEditingDomain.class, event);
		} catch (Exception e) {
			System.err.println("impossible to get the Transactional Editing Domain " + e); //$NON-NLS-1$
		}
		return null;
	}

	/**
	 * getSelected element in the diagram or in hte model explorer
	 *
	 * @return Element or null
	 */
	protected Element getSelection() {
		ISelectionService selectionService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
		ISelection selection = selectionService.getSelection();

		if (selection instanceof IStructuredSelection) {
			Object selectedobject = ((IStructuredSelection) selection).getFirstElement();

			EObject selectedElement = EMFHelper.getEObject(selectedobject);
			if (selectedElement instanceof Element) {
				return (Element) selectedElement;
			}
		}
		return null;
	}

	/**
	 * getSelected element in the diagram or in hte model explorer
	 *
	 * @return Element or null
	 */
	protected ArrayList<Element> getSelectionSet() {
		ArrayList<Element> selectedSet = new ArrayList<Element>();
		ISelectionService selectionService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
		ISelection selection = selectionService.getSelection();

		// look for papyrus

		if (selection instanceof IStructuredSelection) {
			@SuppressWarnings("rawtypes")
			Iterator selectedobjectIteractor = ((IStructuredSelection) selection).iterator();
			while (selectedobjectIteractor.hasNext()) {
				Object currentSelection = selectedobjectIteractor.next();
				EObject selectedElement = EMFHelper.getEObject(currentSelection);
				if (selectedElement instanceof Element) {
					selectedSet.add((Element) selectedElement);
				}
			}


		}
		return selectedSet;
	}

	public static void clear() {
		elt2DocElt.clear();
		href2DocHREF.clear();
		Toc2DocElt.clear();
	}

	public static Element getDocElement(Element modelElement) {
		return elt2DocElt.get(modelElement);
	}

	public static String getDocHREF(String href) {
		return href2DocHREF.get(href);
	}

	public static void putDocElement(Element modelElement, Element docElement) {
		elt2DocElt.put(modelElement, docElement);
		href2DocHREF.put(modelElement.eResource().getURIFragment(modelElement), docElement.eResource().getURIFragment(docElement));
	}

	public static Package getDocPackageForTOC(Package tocPackage) {
		return Toc2DocElt.get(tocPackage);
	}

	public static void putTOCPackage(Package tocPackage, Package docPackage) {
		Toc2DocElt.put(tocPackage, docPackage);
	}
}

Back to the top