Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cdf46c2d5be85adbcef6cf64e5cdb05b4f52db26 (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
/*****************************************************************************
 * 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.emf.utils;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServiceNotFoundException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.core.utils.AbstractServiceUtils;
import org.eclipse.ui.ISources;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartSite;

/**
 * ServicesUtils based on the Handler's ExecutionEvent
 * 
 * It first tests the current selection, then the IWorkbenchPart on which the handler is executed.
 * The IWorkbenchPart is expected to be adaptable to a ServiceRegistry.
 * 
 * @author Camille Letavernier
 * 
 * @see ServiceUtilsForSelection
 */
public class ServiceUtilsForHandlers extends AbstractServiceUtils<ExecutionEvent> {

	private ServiceUtilsForHandlers() {
		//Singleton
	}

	@Override
	public ServicesRegistry getServiceRegistry(ExecutionEvent from) throws ServiceException {

		Object context = from.getApplicationContext();

		if(context instanceof IEvaluationContext) {
			IEvaluationContext evaluationContext = (IEvaluationContext)context;

			//Search for the IWorkbenchPartSite from which the ExecutionEvent is sent (May be different that the Active one)
			Object workbenchPartSite = evaluationContext.getVariable("org.eclipse.ui.IWorkbenchPartSite");
			if(workbenchPartSite instanceof IWorkbenchPartSite) {
				IWorkbenchPartSite site = (IWorkbenchPartSite)workbenchPartSite;
				Object registry = site.getAdapter(ServicesRegistry.class);
				if(registry != null && registry instanceof ServicesRegistry) {
					return (ServicesRegistry)registry;
				}

				//Search for the IWorkbenchPart from which the ExecutionEvent is sent (May be different that the Active one)
				IWorkbenchPart workbenchPart = site.getPart();
				registry = workbenchPart.getAdapter(ServicesRegistry.class);
				if(registry != null && registry instanceof ServicesRegistry) {
					return (ServicesRegistry)registry;
				}
			}

			Object selection = evaluationContext.getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME);

			ServicesRegistry registry;

			//Try to resolve the ServicesRegistry from the current selection
			if(selection instanceof ISelection && !((ISelection)selection).isEmpty()) {
				try {
					registry = ServiceUtilsForSelection.getInstance().getServiceRegistry((ISelection)selection);
					if(registry != null) {
						return registry;
					}
				} catch (ServiceException ex) {
					//Ignore and try another ServiceUtils
				}
			}

			//We couldn't retrieve the ServiceRegistry from the current selection.

			//Try to adapt the active part to the ServicesRegistry
			IWorkbenchPart part = (IWorkbenchPart)evaluationContext.getVariable(ISources.ACTIVE_PART_NAME);
			registry = (ServicesRegistry)(part).getAdapter(ServicesRegistry.class);
			if(registry != null) {
				return registry;
			}
		}

		throw new ServiceNotFoundException("The ServiceRegistry cannot be resolved");
	}

	public static ServiceUtilsForHandlers getInstance() {
		return instance;
	}

	private static final ServiceUtilsForHandlers instance = new ServiceUtilsForHandlers();
}

Back to the top