Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5f54519188ab3bd3e65d6c78d07892fea9c813ab (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
/*******************************************************************************
 * Copyright (c) 2008 Ketan Padegaonkar 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:
 *     Ketan Padegaonkar - initial API and implementation
 *******************************************************************************/
package org.eclipse.swtbot.eclipse.finder.widgets;

import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.AssertionFailedException;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.IHandlerService;

/**
 * A SWTBotViewMenu represents a menu item within a view's menu.
 *
 * @author @author Stephen Paulin <paulin [at] spextreme [dot] com>
 * @version $Id$
 * @since 1.2
 */
public class SWTBotViewMenu {
	private IAction					action			= null;
	private ActionContributionItem	actionItem		= null;
	private String					text			= null;
	/**
	 * Holds the results of the click action.
	 */
	protected Object				menuClickResult	= null;
	/**
	 * Holds command if setup.
	 */
	protected Command				cmdItem			= null;
	/**
	 * Holds the id of the command if one exists.
	 */
	protected String				commandID		= null;

	/**
	 * Constructs a SWTBot View Menu item.
	 *
	 * @param commandItem The command contribution item.
	 * @throws WidgetNotFoundException Thrown if both values are <code>null</code>.
	 * @throws AssertionFailedException If the contribution item is <code>null</code>.
	 */
	public SWTBotViewMenu(Command commandItem) throws WidgetNotFoundException {
		cmdItem = commandItem;

		commandID = cmdItem.getId();
		try {
			text = cmdItem.getName();
		} catch (NotDefinedException e) {
			text = ""; //$NON-NLS-1$
		}
	}

	/**
	 * Constructs a SWTBot View Menu item.
	 *
	 * @param contributionItem The action contribution item.
	 * @throws WidgetNotFoundException Thrown if both values are <code>null</code>.
	 * @throws AssertionFailedException If the contribution item is <code>null</code>.
	 */
	public SWTBotViewMenu(ActionContributionItem contributionItem) throws WidgetNotFoundException {
		Assert.isNotNull(contributionItem);
		Assert.isNotNull(contributionItem.getAction());

		actionItem = contributionItem;
		action = actionItem.getAction();
		commandID = actionItem.getId();
		text = actionItem.getAction().getText();

		if (commandID == null)
			commandID = actionItem.getAction().getActionDefinitionId();
	}

	/**
	 * Simulates the click action of the menu.
	 *
	 * @throws WidgetNotFoundException Thrown if the action or command id are not valid.
	 */
	public void click() throws WidgetNotFoundException {
		if (commandID != null) {
			menuClickResult = null;
			final IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);

			UIThreadRunnable.asyncExec(new VoidResult() {
				public void run() {
					try {
						menuClickResult = handlerService.executeCommand(commandID, null);
					} catch (Exception e) {
						throw new RuntimeException("Failed to execute the command - " + commandID, e); //$NON-NLS-1$
					}
				}
			});
		} else if (action != null)
			UIThreadRunnable.asyncExec(new VoidResult() {
				public void run() {
					action.run();
				}
			});
		else
			throw new WidgetNotFoundException("There is no action or contribution id to execute."); //$NON-NLS-1$
	}

	/**
	 * After a click completes, this may be use to access the results returned by the command. If a click had not
	 * previously been done then this value will be <code>null</code>.
	 *
	 * @return The object data from the click or <code>null</code> if a click never occurred.
	 */
	public Object getClickResult() {
		return menuClickResult;
	}

	/**
	 * GEts the text label for the menu item.
	 *
	 * @return The text label.
	 * @throws WidgetNotFoundException Thrown if the action is <code>null</code>.
	 */
	public String getText() throws WidgetNotFoundException {
		return text;
	}

	/**
	 * Gets if the menu item is checked (has a check mark next to it).
	 *
	 * @return <code>true</code> if checked. Otherwise <code>false</code>.
	 */
	public boolean isChecked() {
		if (action != null)
			return action.isChecked();

		// FIXME This needs to find if a contribution item (Command) has been checked...
		return false;
	}
}

Back to the top