Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SWTBotViewMenu |
|
| 2.111111111111111;2.111 | ||||
SWTBotViewMenu$1 |
|
| 2.111111111111111;2.111 | ||||
SWTBotViewMenu$2 |
|
| 2.111111111111111;2.111 |
1 | 1 | /******************************************************************************* |
2 | * Copyright (c) 2008 Ketan Padegaonkar and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * Ketan Padegaonkar - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.swtbot.eclipse.finder.widgets; | |
12 | ||
13 | import org.eclipse.core.commands.Command; | |
14 | import org.eclipse.core.commands.ParameterizedCommand; | |
15 | import org.eclipse.core.commands.common.NotDefinedException; | |
16 | import org.eclipse.core.runtime.Assert; | |
17 | import org.eclipse.core.runtime.AssertionFailedException; | |
18 | import org.eclipse.jface.action.ActionContributionItem; | |
19 | import org.eclipse.jface.action.IAction; | |
20 | import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; | |
21 | import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; | |
22 | import org.eclipse.swtbot.swt.finder.results.VoidResult; | |
23 | import org.eclipse.ui.PlatformUI; | |
24 | import org.eclipse.ui.handlers.IHandlerService; | |
25 | ||
26 | /** | |
27 | * A SWTBotViewMenu represents a menu item within a view's menu. | |
28 | * | |
29 | * @author @author Stephen Paulin <paulin [at] spextreme [dot] com> | |
30 | * @version $Id$ | |
31 | * @since 1.2 | |
32 | */ | |
33 | public class SWTBotViewMenu { | |
34 | 12 | private IAction action = null; |
35 | 11 | private ActionContributionItem actionItem = null; |
36 | 11 | private String text = null; |
37 | /** | |
38 | * Holds the results of the click action. | |
39 | */ | |
40 | 11 | protected Object menuClickResult = null; |
41 | /** | |
42 | * Holds command if setup. | |
43 | */ | |
44 | 11 | protected Command cmdItem = null; |
45 | /** | |
46 | * Holds parameterized command if setup. | |
47 | */ | |
48 | 11 | protected ParameterizedCommand paraCmdItem = null; |
49 | /** | |
50 | * Holds the id of the command if one exists. | |
51 | */ | |
52 | 11 | protected String commandID = null; |
53 | ||
54 | /** | |
55 | * Constructs a SWTBot View Menu item. | |
56 | * | |
57 | * @param commandItem The command contribution item. | |
58 | * @throws WidgetNotFoundException Thrown if both values are <code>null</code>. | |
59 | * @throws AssertionFailedException If the contribution item is <code>null</code>. | |
60 | */ | |
61 | 2 | public SWTBotViewMenu(Command commandItem) throws WidgetNotFoundException { |
62 | 2 | cmdItem = commandItem; |
63 | ||
64 | 2 | commandID = cmdItem.getId(); |
65 | try { | |
66 | 2 | text = cmdItem.getName(); |
67 | 0 | } catch (NotDefinedException e) { |
68 | 0 | text = ""; //$NON-NLS-1$ |
69 | } | |
70 | 2 | } |
71 | ||
72 | /** | |
73 | * Constructs a SWTBot View Menu item. | |
74 | * | |
75 | * @param commandItem The parameterized command contribution item. | |
76 | * @throws WidgetNotFoundException Thrown if both values are <code>null</code>. | |
77 | * @throws AssertionFailedException If the contribution item is <code>null</code>. | |
78 | */ | |
79 | 4 | public SWTBotViewMenu(ParameterizedCommand commandItem) throws WidgetNotFoundException { |
80 | 4 | paraCmdItem = commandItem; |
81 | ||
82 | 4 | commandID = paraCmdItem.getId(); |
83 | try { | |
84 | 4 | text = paraCmdItem.getName(); |
85 | 0 | } catch (NotDefinedException e) { |
86 | 0 | text = ""; //$NON-NLS-1$ |
87 | } | |
88 | 4 | } |
89 | ||
90 | /** | |
91 | * Constructs a SWTBot View Menu item. | |
92 | * | |
93 | * @param contributionItem The action contribution item. | |
94 | * @throws WidgetNotFoundException Thrown if both values are <code>null</code>. | |
95 | * @throws AssertionFailedException If the contribution item is <code>null</code>. | |
96 | */ | |
97 | 5 | public SWTBotViewMenu(ActionContributionItem contributionItem) throws WidgetNotFoundException { |
98 | 5 | Assert.isNotNull(contributionItem); |
99 | 5 | Assert.isNotNull(contributionItem.getAction()); |
100 | ||
101 | 5 | actionItem = contributionItem; |
102 | 5 | action = actionItem.getAction(); |
103 | 5 | commandID = actionItem.getId(); |
104 | 5 | text = actionItem.getAction().getText(); |
105 | ||
106 | 5 | if (commandID == null) |
107 | 5 | commandID = actionItem.getAction().getActionDefinitionId(); |
108 | 5 | } |
109 | ||
110 | /** | |
111 | * Simulates the click action of the menu. | |
112 | * | |
113 | * @throws WidgetNotFoundException Thrown if the action or command id are not valid. | |
114 | */ | |
115 | public void click() throws WidgetNotFoundException { | |
116 | 2 | if (commandID != null) { |
117 | 1 | menuClickResult = null; |
118 | 1 | final IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class); |
119 | ||
120 | 1 | UIThreadRunnable.asyncExec(new VoidResult() { |
121 | public void run() { | |
122 | try { | |
123 | 1 | menuClickResult = handlerService.executeCommand(commandID, null); |
124 | 0 | } catch (Exception e) { |
125 | 0 | throw new RuntimeException("Failed to execute the command - " + commandID, e); //$NON-NLS-1$ |
126 | } | |
127 | 1 | } |
128 | }); | |
129 | 1 | } else if (action != null) |
130 | 1 | UIThreadRunnable.asyncExec(new VoidResult() { |
131 | public void run() { | |
132 | 1 | action.run(); |
133 | 1 | } |
134 | }); | |
135 | else | |
136 | 0 | throw new WidgetNotFoundException("There is no action or contribution id to execute."); //$NON-NLS-1$ |
137 | 2 | } |
138 | ||
139 | /** | |
140 | * After a click completes, this may be use to access the results returned by the command. If a click had not | |
141 | * previously been done then this value will be <code>null</code>. | |
142 | * | |
143 | * @return The object data from the click or <code>null</code> if a click never occurred. | |
144 | */ | |
145 | public Object getClickResult() { | |
146 | 0 | return menuClickResult; |
147 | } | |
148 | ||
149 | /** | |
150 | * GEts the text label for the menu item. | |
151 | * | |
152 | * @return The text label. | |
153 | * @throws WidgetNotFoundException Thrown if the action is <code>null</code>. | |
154 | */ | |
155 | public String getText() throws WidgetNotFoundException { | |
156 | 0 | return text; |
157 | } | |
158 | ||
159 | /** | |
160 | * Gets if the menu item is checked (has a check mark next to it). | |
161 | * | |
162 | * @return <code>true</code> if checked. Otherwise <code>false</code>. | |
163 | */ | |
164 | public boolean isChecked() { | |
165 | 0 | if (action != null) |
166 | 0 | return action.isChecked(); |
167 | ||
168 | // FIXME This needs to find if a contribution item (Command) has been checked... | |
169 | 0 | return false; |
170 | } | |
171 | } |