Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9f082c7a5b2ff4297579c8e42c6368429a571ce6 (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
package org.eclipse.ui.tests.util;
import java.lang.reflect.Method;
import junit.framework.TestCase;
import org.eclipse.jface.action.*;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.internal.WorkbenchWindow;


/**
 * <code>ActionUtil</code> contains methods to run actions
 * in the workbench.
 */
public class ActionUtil {


	/**
	 * Runs an action contribution.
	 * 
	 * @param test the current test case
	 * @param item an action contribution item
	 */
	public static void runAction(TestCase test, 
		IContributionItem item) 
	{
		test.assertTrue(item instanceof ActionContributionItem);
		((ActionContributionItem)item).getAction().run();
	}
	
	/**
	 * Runs the first action found in a menu manager with a
	 * particular label. 
	 *
	 * @param test the current test case
	 * @param mgr the containing menu manager
	 * @param label the action label
	 */
	public static void runActionWithLabel(TestCase test, 
		IMenuManager mgr, String label) 
	{
		IContributionItem [] items = mgr.getItems();
		for (int nX = 0; nX < items.length; nX ++) {
			IContributionItem item = items[nX];
			if (item instanceof SubContributionItem)
				item = ((SubContributionItem)item).getInnerItem();
			if (item instanceof ActionContributionItem) {
				IAction action = ((ActionContributionItem)item).getAction();
				if (label.equals(action.getText())) {
					action.run();
					return;
				}
			}
		}
		test.fail("Unable to find action: " + label);
	}


	/**
	 * Runs the first action found in a window with a
	 * particular label. 
	 * 
	 * @param test the current test case
	 * @param win the containing window
	 * @param label the action label
	 */
	public static void runActionWithLabel(TestCase test, 
		IWorkbenchWindow win, String label) 
	{
		WorkbenchWindow realWin = (WorkbenchWindow)win;
		IMenuManager mgr = realWin.getMenuManager();
		runActionWithLabel(test, mgr, label);
	}
		
	/**
	 * Runs an action identified by an id path in a 
	 * menu manager.
	 * 
	 * @param test the current test case
	 * @param mgr the containing menu manager
	 * @param label the action label
	 */
	public static void runActionUsingPath(TestCase test, 
		IMenuManager mgr, String idPath) 
	{
		IContributionItem item = mgr.findUsingPath(idPath);
		test.assertNotNull(item);
		runAction(test, item);
	}
	
	/**
	 * Runs an action identified by an id path in a 
	 * window.
	 * 
	 * @param test the current test case
	 * @param win the containing window
	 * @param label the action label
	 */
	public static void runActionUsingPath(TestCase test, 
		IWorkbenchWindow win, String idPath) 
	{
		WorkbenchWindow realWin = (WorkbenchWindow)win;
		IMenuManager mgr = realWin.getMenuManager();
		runActionUsingPath(test, mgr, idPath);
	}
	
	/**
	 * Returns the first action found in a menu manager with a
	 * particular label. 
	 *
	 * @param mgr the containing menu manager
	 * @param label the action label
	 * @return the first action with the label, or <code>null</code>
	 * 		if it is not found.
	 */
	public static IAction getActionWithLabel(IMenuManager mgr, 
		String label) 
	{
		IContributionItem [] items = mgr.getItems();
		for (int nX = 0; nX < items.length; nX ++) {
			IContributionItem item = items[nX];
			if (item instanceof SubContributionItem)
				item = ((SubContributionItem)item).getInnerItem();
			if (item instanceof ActionContributionItem) {
				IAction action = ((ActionContributionItem)item).getAction();
				if (label.equals(action.getText())) {
					return action;
				}
			}
		}
		return null;
	}
	
	/**
	 * Fire the "handleAboutToShow" method in a menu manager.
	 * This triggers the same behavior as when a user opens a menu.
	 * The menu to be populated with actions and those 
	 * actions to be enacted in SWT widgets.
	 * 
	 * @param mgr the menu manager to open
	 */  
	public static void fireAboutToShow(MenuManager mgr) 
		throws Throwable 
	{
		Class clazz = mgr.getClass();
		Method method = clazz.getDeclaredMethod("handleAboutToShow", new Class [0]);
		method.setAccessible(true);
		method.invoke(mgr, new Object[0]);
	}
}


Back to the top