Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cdc02e5420cd90127cb1bca5de7a5878bdd90e9e (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
151
152
153
154
155
156
157
158
159
160
161
162
163
/*****************************************************************************
 * 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.views.modelexplorer.handler;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.Platform;
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.jface.viewers.TreeSelection;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IPageManager;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.utils.ServiceUtilsForActionHandlers;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * This provides facilities to get the TransactionEditingDomain and the PageManager from the Model Explorer
 *
 *
 *
 */
public abstract class AbstractModelExplorerHandler extends AbstractHandler {

	/**
	 * Returns the
	 *
	 * @return
	 *         the current editing domain
	 */
	protected TransactionalEditingDomain getEditingDomain() {
		TransactionalEditingDomain editingDomain = null;
		try {
			editingDomain = org.eclipse.papyrus.infra.core.utils.ServiceUtilsForActionHandlers.getInstance().getTransactionalEditingDomain();
		} catch (ServiceException e) {
			// we are closing the editor, so the model explorer has nothing to display
			// e.printStackTrace();
		}
		return editingDomain;
	}

	/**
	 * Returns the page manager
	 *
	 * @return
	 *         the page manager
	 */
	protected IPageManager getPageManager() {
		IPageManager pageManager = null;
		try {
			pageManager = ServiceUtilsForActionHandlers.getInstance().getIPageManager();
		} catch (ServiceException e) {
			// we are closing the editor, so the model explorer has nothing to display
			// e.printStackTrace();
		}
		return pageManager;
	}

	/**
	 * Adapt the specified object to the requested type, if possible.
	 * Return null if the object can't be adapted.
	 *
	 * @param object
	 * @param expectedClassType
	 * @return The adapted object, or null.
	 */
	@SuppressWarnings("unchecked")
	private <T> T adapt(Object object, Class<T> expectedClassType) {


		EObject eobject = EMFHelper.getEObject(object);

		if (eobject != null && expectedClassType.isInstance(eobject)) {
			return (T) eobject;
		}



		// Try global mechanism
		{
			T ele = Platform.getAdapterManager().getAdapter(object, expectedClassType);
			if (ele != null) {
				return ele;
			}
			// Try as EObject if the expectedClasType is sub-type of EObject.
			if (EObject.class.isAssignableFrom(expectedClassType)) {
				// to EObject
				eobject = Platform.getAdapterManager().getAdapter(object, EObject.class);

				if (eobject != null && expectedClassType.isInstance(eobject)) {

					return (T) eobject;
				}
			}
		}
		// Can't be adapted
		return null;

	}

	/**
	 * Filter the list, and only retain objects that can be adapted to the specified type
	 *
	 * @param objects
	 * @param class1
	 * @return
	 */
	private <T> List<T> getAllElementAdaptedToType(List<Object> list, Class<T> expectedClassType) {

		List<T> res = new ArrayList<T>();

		for (Object cur : list) {

			T adapted = adapt(cur, expectedClassType);
			if (adapted != null) {
				res.add(adapted);
			}
		}
		return res;
	}

	/**
	 * Get all selected element of the specified type.
	 *
	 * @param expectedType
	 * @return
	 * @throws ExecutionException
	 */
	@SuppressWarnings("unchecked")
	protected <T> List<T> getCurrentSelectionAdaptedToType(ExecutionEvent event, Class<T> expectedType) throws ExecutionException {

		// Get selection from the workbench
		ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);

		// Get the selected objects according to the type of the selected
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
			return getAllElementAdaptedToType(structuredSelection.toList(), expectedType);
		} else if (selection instanceof TreeSelection) {
			TreeSelection treeSelection = (TreeSelection) selection;
			return getAllElementAdaptedToType(treeSelection.toList(), expectedType);

		}
		return null;
	}
}

Back to the top