Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e76df3ba15ba1b89cc09c25262bf87129aff0012 (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
/*****************************************************************************
 * 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:
 *  Vincent Lorenzo (CEA LIST) Vincent.Lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.junit.utils;

import junit.framework.Assert;

import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.ParameterizedCommand;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.handlers.IHandlerService;

/**
 * 
 * Useful methods for the handlers and the Eclipse commands
 * 
 */
public class HandlerUtils {

	/**
	 * Warning : you should be sure that the correct Editor/partView... has the focus to test the enablement if the handler!
	 * 
	 * @param commandId
	 *        the command id
	 * @return
	 *         the current handler for this command
	 */
	public static final IHandler getActiveHandlerFor(final String commandId) {
		return HandlerUtils.getCommand(commandId).getHandler();
	}

	/**
	 * 
	 * @param commandId
	 *        the commandId
	 * @return
	 *         the real handler for the command
	 */
	public static final IHandler getRealHandlerFor(final String commandId) {
		//commented because it works only on Eclipse4
		//imports to add : 
		//			- org.eclipse.e4.ui.model.application.MApplication;
		//			- org.eclipse.e4.core.contexts.IEclipseContext
		//			- org.eclipse.e4.core.commands.internal.HandlerServiceImpl
		//			- org.eclipse.ui.internal.handlers.E4HandlerProxy
		//		MApplication appl = (MApplication)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(MApplication.class);
		//		IEclipseContext context = appl.getContext();
		//		E4HandlerProxy handler = HandlerServiceImpl.lookUpHandler(context, commandId);
		//		return handler.getHandler();
		throw new UnsupportedOperationException();
	}

	/**
	 * 
	 * @param commandId
	 *        the command id
	 * @return
	 *         the command object for this command id
	 */
	public static final Command getCommand(final String commandId) {
		final ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(ICommandService.class);
		commandService.refreshElements(commandId, null);
		Assert.assertNotNull("CommandService can't be found", commandService);
		Command cmd = commandService.getCommand(commandId);
		Assert.assertNotNull("Command can't be found", cmd);
		return cmd;
	}

	/**
	 * Execute the command
	 * 
	 * @param cmd
	 *        a command
	 */
	public static final void executeCommand(final Command cmd) throws Exception {
		IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
		Assert.assertNotNull("Impossible to find handler service", handlerService);
		final ParameterizedCommand parameterizedCommand = new ParameterizedCommand(cmd, null);
		Assert.assertEquals("Command is not executable as expected", cmd.isEnabled(), true);
		//execute the command
		handlerService.executeCommand(parameterizedCommand, null);
	}

}

Back to the top