| 1 | 8 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
package org.eclipse.swtbot.swt.finder.widgets; |
| 12 | |
|
| 13 | |
import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic; |
| 14 | |
|
| 15 | |
import java.util.List; |
| 16 | |
|
| 17 | |
import org.eclipse.swt.SWT; |
| 18 | |
import org.eclipse.swt.widgets.Menu; |
| 19 | |
import org.eclipse.swt.widgets.MenuItem; |
| 20 | |
import org.eclipse.swt.widgets.Widget; |
| 21 | |
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; |
| 22 | |
import org.eclipse.swtbot.swt.finder.finders.MenuFinder; |
| 23 | |
import org.eclipse.swtbot.swt.finder.results.BoolResult; |
| 24 | |
import org.eclipse.swtbot.swt.finder.results.VoidResult; |
| 25 | |
import org.eclipse.swtbot.swt.finder.results.WidgetResult; |
| 26 | |
import org.eclipse.swtbot.swt.finder.utils.MessageFormat; |
| 27 | |
import org.hamcrest.Matcher; |
| 28 | |
import org.hamcrest.SelfDescribing; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
public class SWTBotMenu extends AbstractSWTBot<MenuItem> { |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
public SWTBotMenu(MenuItem w, SelfDescribing description) throws WidgetNotFoundException { |
| 42 | 27 | super(w, description); |
| 43 | 27 | } |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public SWTBotMenu(MenuItem w) throws WidgetNotFoundException { |
| 50 | 2 | this(w, null); |
| 51 | 2 | } |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public SWTBotMenu click() { |
| 57 | 9 | log.debug(MessageFormat.format("Clicking on {0}", this)); |
| 58 | 9 | waitForEnabled(); |
| 59 | 9 | toggleSelection(); |
| 60 | 9 | notify(SWT.Selection); |
| 61 | 9 | log.debug(MessageFormat.format("Clicked on {0}", this)); |
| 62 | 9 | return this; |
| 63 | |
} |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
private void toggleSelection() { |
| 69 | 9 | syncExec(new VoidResult() { |
| 70 | |
public void run() { |
| 71 | 9 | if (hasStyle(widget, SWT.CHECK) | hasStyle(widget, SWT.RADIO)) |
| 72 | 0 | widget.setSelection(!widget.getSelection()); |
| 73 | 9 | } |
| 74 | |
}); |
| 75 | 9 | } |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
public SWTBotMenu menu(final String menuName) throws WidgetNotFoundException { |
| 85 | 4 | final Matcher<? extends Widget> matcher = withMnemonic(menuName); |
| 86 | 4 | MenuItem menuItem = syncExec(new WidgetResult<MenuItem>() { |
| 87 | |
public MenuItem run() { |
| 88 | 4 | Menu bar = widget.getMenu(); |
| 89 | 4 | Matcher<MenuItem> withMnemonic = withMnemonic(menuName); |
| 90 | 4 | List<MenuItem> menus = new MenuFinder().findMenus(bar, withMnemonic, true); |
| 91 | 4 | if (!menus.isEmpty()) |
| 92 | 4 | return menus.get(0); |
| 93 | 0 | return null; |
| 94 | |
} |
| 95 | |
}); |
| 96 | 4 | return new SWTBotMenu(menuItem, matcher); |
| 97 | |
} |
| 98 | |
|
| 99 | |
@Override |
| 100 | |
public boolean isEnabled() { |
| 101 | 9 | return syncExec(new BoolResult() { |
| 102 | |
public Boolean run() { |
| 103 | 9 | return widget.isEnabled(); |
| 104 | |
} |
| 105 | |
}); |
| 106 | |
} |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
public boolean isChecked() { |
| 116 | 0 | return syncExec(new BoolResult() { |
| 117 | |
public Boolean run() { |
| 118 | 0 | return widget.getSelection(); |
| 119 | |
} |
| 120 | |
}); |
| 121 | |
} |
| 122 | |
} |