1 package eu
.etaxonomy
.taxeditor
.test
;
3 import static org
.eclipse
.swtbot
.swt
.finder
.matchers
.WidgetMatcherFactory
.withMnemonic
;
4 import static org
.hamcrest
.CoreMatchers
.allOf
;
5 import static org
.hamcrest
.CoreMatchers
.instanceOf
;
7 import java
.util
.Arrays
;
9 import org
.eclipse
.swt
.SWT
;
10 import org
.eclipse
.swt
.widgets
.Control
;
11 import org
.eclipse
.swt
.widgets
.Event
;
12 import org
.eclipse
.swt
.widgets
.Menu
;
13 import org
.eclipse
.swt
.widgets
.MenuItem
;
14 import org
.eclipse
.swtbot
.swt
.finder
.exceptions
.WidgetNotFoundException
;
15 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
16 import org
.eclipse
.swtbot
.swt
.finder
.results
.VoidResult
;
17 import org
.eclipse
.swtbot
.swt
.finder
.results
.WidgetResult
;
18 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.AbstractSWTBot
;
19 import org
.hamcrest
.Matcher
;
21 public class ContextMenuHelper
{
24 * Clicks the context menu matching the text.
27 * the text on the context menu.
28 * @throws WidgetNotFoundException
29 * if the widget is not found.
31 public static void clickContextMenu(final AbstractSWTBot
<?
> bot
,
32 final String
... texts
) {
35 final MenuItem menuItem
= UIThreadRunnable
36 .syncExec(new WidgetResult
<MenuItem
>() {
38 public MenuItem
run() {
39 MenuItem menuItem
= null;
40 Control control
= (Control
) bot
.widget
;
41 Menu menu
= control
.getMenu();
42 for (String text
: texts
) {
43 Matcher
<?
> matcher
= allOf(instanceOf(MenuItem
.class),
45 menuItem
= show(menu
, matcher
);
46 if (menuItem
!= null) {
47 menu
= menuItem
.getMenu();
57 if (menuItem
== null) {
58 throw new WidgetNotFoundException("Could not find menu: "
59 + Arrays
.asList(texts
));
66 UIThreadRunnable
.syncExec(new VoidResult() {
69 hide(menuItem
.getParent());
74 private static MenuItem
show(final Menu menu
, final Matcher
<?
> matcher
) {
76 menu
.notifyListeners(SWT
.Show
, new Event());
77 MenuItem
[] items
= menu
.getItems();
78 for (final MenuItem menuItem
: items
) {
79 if (matcher
.matches(menuItem
)) {
83 menu
.notifyListeners(SWT
.Hide
, new Event());
88 private static void click(final MenuItem menuItem
) {
89 final Event event
= new Event();
90 event
.time
= (int) System
.currentTimeMillis();
91 event
.widget
= menuItem
;
92 event
.display
= menuItem
.getDisplay();
93 event
.type
= SWT
.Selection
;
95 UIThreadRunnable
.asyncExec(menuItem
.getDisplay(), new VoidResult() {
98 menuItem
.notifyListeners(SWT
.Selection
, event
);
103 private static void hide(final Menu menu
) {
104 menu
.notifyListeners(SWT
.Hide
, new Event());
105 if (menu
.getParentMenu() != null) {
106 hide(menu
.getParentMenu());