Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SWTBotView |
|
| 1.5454545454545454;1.545 | ||||
SWTBotView$1 |
|
| 1.5454545454545454;1.545 | ||||
SWTBotView$2 |
|
| 1.5454545454545454;1.545 |
1 | 0 | /******************************************************************************* |
2 | * Copyright (c) 2008-2009 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 | * Ralf Ebert www.ralfebert.de - (bug 271630) SWTBot Improved RCP / Workbench support | |
11 | *******************************************************************************/ | |
12 | package org.eclipse.swtbot.eclipse.finder.widgets; | |
13 | ||
14 | import static org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable.syncExec; | |
15 | import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic; | |
16 | import static org.hamcrest.Matchers.anything; | |
17 | import static org.hamcrest.Matchers.equalTo; | |
18 | ||
19 | import java.util.List; | |
20 | ||
21 | import javax.swing.text.View; | |
22 | ||
23 | import org.eclipse.swt.widgets.Control; | |
24 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; | |
25 | import org.eclipse.swtbot.eclipse.finder.finders.CommandFinder; | |
26 | import org.eclipse.swtbot.eclipse.finder.finders.ViewMenuFinder; | |
27 | import org.eclipse.swtbot.eclipse.finder.widgets.utils.PartLabelDescription; | |
28 | import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; | |
29 | import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; | |
30 | import org.eclipse.swtbot.swt.finder.results.VoidResult; | |
31 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu; | |
32 | import org.eclipse.ui.IViewReference; | |
33 | import org.hamcrest.SelfDescribing; | |
34 | ||
35 | /** | |
36 | * This represents the eclipse {@link View} item. | |
37 | * | |
38 | * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> | |
39 | * @author Ralf Ebert www.ralfebert.de (bug 271630) | |
40 | * @version $Id$ | |
41 | */ | |
42 | public class SWTBotView extends SWTBotWorkbenchPart<IViewReference> { | |
43 | ||
44 | private final ViewMenuFinder menuFinder; | |
45 | /** | |
46 | * Creates an instance of a view part. | |
47 | * | |
48 | * @param partReference the view reference representing this view. | |
49 | * @param bot the bot that's used to find controls within this view. | |
50 | * @since 2.0 | |
51 | */ | |
52 | public SWTBotView(IViewReference partReference, SWTWorkbenchBot bot) { | |
53 | 18 | this(partReference, bot, new PartLabelDescription<IViewReference>(partReference)); |
54 | 18 | } |
55 | ||
56 | /** | |
57 | * Creates an instance of a view part. | |
58 | * | |
59 | * @param partReference the part reference. | |
60 | * @param bot the helper bot. | |
61 | * @param description the description of the workbench part. | |
62 | */ | |
63 | public SWTBotView(IViewReference partReference, SWTWorkbenchBot bot, SelfDescribing description) { | |
64 | 18 | super(partReference, bot, description); |
65 | 18 | this.menuFinder = new ViewMenuFinder(); |
66 | 18 | } |
67 | ||
68 | public void setFocus() { | |
69 | 0 | syncExec(new VoidResult() { |
70 | public void run() { | |
71 | 0 | ((Control) getWidget()).setFocus(); |
72 | 0 | } |
73 | }); | |
74 | 0 | } |
75 | ||
76 | /** | |
77 | * @return the view reference for this view. | |
78 | */ | |
79 | public IViewReference getViewReference() { | |
80 | 0 | return partReference; |
81 | } | |
82 | ||
83 | public boolean isActive() { | |
84 | 0 | return partReference.getPage().getActivePartReference() == partReference; |
85 | } | |
86 | ||
87 | /** | |
88 | * Close the partReference. | |
89 | */ | |
90 | public void close() { | |
91 | 2 | UIThreadRunnable.syncExec(new VoidResult() { |
92 | public void run() { | |
93 | 2 | partReference.getPage().hideView(partReference); |
94 | 2 | } |
95 | }); | |
96 | 2 | } |
97 | ||
98 | /** | |
99 | * Gets a list of all menus within the partReference. This will also include sub menus. | |
100 | * | |
101 | * @return The list of menus | |
102 | */ | |
103 | public List<SWTBotViewMenu> menus() { | |
104 | 1 | return menuFinder.findMenus(partReference, anything(), true); |
105 | } | |
106 | ||
107 | /** | |
108 | * Gets a menu item matching the give label within the partReference menu if one exists. | |
109 | * | |
110 | * @param label The label matching name in the menu. | |
111 | * @return The {@link SWTBotMenu} item. | |
112 | * @throws WidgetNotFoundException Thrown if the menu can not be found or if the partReference does not contain a | |
113 | * menu. | |
114 | */ | |
115 | public SWTBotViewMenu menu(String label) throws WidgetNotFoundException { | |
116 | 3 | return menu(label, 0); |
117 | } | |
118 | ||
119 | /** | |
120 | * Gets a menu item matching the give label within the partReference menu if one exists. | |
121 | * | |
122 | * @param label The label matching name in the menu. | |
123 | * @param index The index of the menu to choose. | |
124 | * @return The {@link SWTBotMenu} item. | |
125 | * @throws WidgetNotFoundException Thrown if the menu can not be found or if the partReference does not contain a | |
126 | * menu. | |
127 | */ | |
128 | public SWTBotViewMenu menu(String label, int index) throws WidgetNotFoundException { | |
129 | try { | |
130 | 3 | List<SWTBotViewMenu> menuItems = menuFinder.findMenus(partReference, withMnemonic(label), true); |
131 | 3 | if ((menuItems == null) || (menuItems.size() < 1)) { |
132 | 1 | CommandFinder finder = new CommandFinder(); |
133 | 1 | List<SWTBotCommand> command = finder.findCommand(equalTo(label)); |
134 | 1 | return command.get(index); |
135 | } | |
136 | 2 | return menuItems.get(index); |
137 | 0 | } catch (Exception e) { |
138 | 0 | throw new WidgetNotFoundException("Could not find view menu with label " + label + " at index " + index, e); //$NON-NLS-1$ //$NON-NLS-2$ |
139 | } | |
140 | } | |
141 | } |