1 package eu
.etaxonomy
.taxeditor
.test
;
3 import static org
.eclipse
.swtbot
.swt
.finder
.matchers
.WidgetMatcherFactory
.withMnemonic
;
4 import static org
.hamcrest
.Matchers
.allOf
;
5 import static org
.hamcrest
.Matchers
.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
>() {
37 public MenuItem
run() {
38 MenuItem menuItem
= null;
39 Control control
= (Control
) bot
.widget
;
40 Menu menu
= control
.getMenu();
41 for (String text
: texts
) {
42 Matcher
<?
> matcher
= allOf(instanceOf(MenuItem
.class),
44 menuItem
= show(menu
, matcher
);
45 if (menuItem
!= null) {
46 menu
= menuItem
.getMenu();
56 if (menuItem
== null) {
57 throw new WidgetNotFoundException("Could not find menu: "
58 + Arrays
.asList(texts
));
65 UIThreadRunnable
.syncExec(new VoidResult() {
67 hide(menuItem
.getParent());
72 private static MenuItem
show(final Menu menu
, final Matcher
<?
> matcher
) {
74 menu
.notifyListeners(SWT
.Show
, new Event());
75 MenuItem
[] items
= menu
.getItems();
76 for (final MenuItem menuItem
: items
) {
77 if (matcher
.matches(menuItem
)) {
81 menu
.notifyListeners(SWT
.Hide
, new Event());
86 private static void click(final MenuItem menuItem
) {
87 final Event event
= new Event();
88 event
.time
= (int) System
.currentTimeMillis();
89 event
.widget
= menuItem
;
90 event
.display
= menuItem
.getDisplay();
91 event
.type
= SWT
.Selection
;
93 UIThreadRunnable
.asyncExec(menuItem
.getDisplay(), new VoidResult() {
95 menuItem
.notifyListeners(SWT
.Selection
, event
);
100 private static void hide(final Menu menu
) {
101 menu
.notifyListeners(SWT
.Hide
, new Event());
102 if (menu
.getParentMenu() != null) {
103 hide(menu
.getParentMenu());