| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
package org.eclipse.swtbot.swt.finder.matchers; |
| 13 | |
|
| 14 | |
import java.util.List; |
| 15 | |
import java.util.ListIterator; |
| 16 | |
|
| 17 | |
import org.eclipse.swt.custom.CLabel; |
| 18 | |
import org.eclipse.swt.widgets.Label; |
| 19 | |
import org.eclipse.swt.widgets.Widget; |
| 20 | |
import org.eclipse.swtbot.swt.finder.finders.ControlFinder; |
| 21 | |
import org.eclipse.swtbot.swt.finder.finders.Finder; |
| 22 | |
import org.eclipse.swtbot.swt.finder.finders.MenuFinder; |
| 23 | |
import org.eclipse.swtbot.swt.finder.utils.internal.Assert; |
| 24 | |
import org.hamcrest.Description; |
| 25 | |
import org.hamcrest.Factory; |
| 26 | |
import org.hamcrest.Matcher; |
| 27 | |
import org.hamcrest.Matchers; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
public class WithLabel<T extends Widget> extends AbstractMatcher<T> { |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
private final WithMnemonic<Widget> mnemonicTextMatcher; |
| 46 | |
private final Finder finder; |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | 10 | WithLabel(String labelText, Finder finder) { |
| 55 | 10 | Assert.isNotNull(labelText, "The parameter labelText was null."); |
| 56 | 10 | Assert.isNotNull(finder, "The parameter finder was null."); |
| 57 | 10 | mnemonicTextMatcher = new WithMnemonic<Widget>(labelText); |
| 58 | 10 | this.finder = finder; |
| 59 | 10 | } |
| 60 | |
|
| 61 | |
protected boolean doMatch(Object obj) { |
| 62 | 295 | List<? extends Widget> allWidgets = finder.findControls(Matchers.<Widget> anything()); |
| 63 | |
|
| 64 | 295 | int widgetIndex = allWidgets.indexOf(obj); |
| 65 | |
|
| 66 | 295 | ListIterator<? extends Widget> listIterator = allWidgets.listIterator(widgetIndex); |
| 67 | |
|
| 68 | 5608 | while (listIterator.hasPrevious()) { |
| 69 | 5169 | Widget previousWidget = listIterator.previous(); |
| 70 | 5169 | if ((isLabel(previousWidget)) && mnemonicTextMatcher.matches(previousWidget)) |
| 71 | 151 | return true; |
| 72 | |
} |
| 73 | |
|
| 74 | 144 | return false; |
| 75 | |
} |
| 76 | |
|
| 77 | |
private boolean isLabel(Widget widget) { |
| 78 | 5169 | return widget instanceof Label || widget instanceof CLabel; |
| 79 | |
} |
| 80 | |
|
| 81 | |
public void describeTo(Description description) { |
| 82 | 73 | description.appendText("with label (").appendDescriptionOf(mnemonicTextMatcher).appendText(")"); |
| 83 | 73 | } |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
@Factory |
| 93 | |
public static <T extends Widget> Matcher<T> withLabel(String labelText) { |
| 94 | 1 | return new WithLabel<T>(labelText, new Finder(new ControlFinder(), new MenuFinder())); |
| 95 | |
} |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
@Factory |
| 106 | |
public static <T extends Widget> Matcher<T> withLabel(String labelText, Finder finder) { |
| 107 | 9 | return new WithLabel<T>(labelText, finder); |
| 108 | |
} |
| 109 | |
|
| 110 | |
} |