| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
package org.eclipse.swtbot.swt.finder.matchers; |
| 12 | |
|
| 13 | |
import java.lang.reflect.InvocationTargetException; |
| 14 | |
import java.util.ArrayList; |
| 15 | |
|
| 16 | |
import org.eclipse.swt.widgets.Item; |
| 17 | |
import org.eclipse.swtbot.swt.finder.utils.SWTUtils; |
| 18 | |
import org.hamcrest.Description; |
| 19 | |
import org.hamcrest.Factory; |
| 20 | |
import org.hamcrest.Matcher; |
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
public class WithItem<T extends Item> extends AbstractMatcher<T> { |
| 27 | |
private final Matcher<?> itemMatcher; |
| 28 | |
private final ArrayList<T> matches; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | 9 | WithItem(Matcher<?> itemMatcher) { |
| 37 | 9 | this.itemMatcher = itemMatcher; |
| 38 | 9 | matches = new ArrayList<T>(); |
| 39 | 9 | } |
| 40 | |
|
| 41 | |
public void describeTo(Description description) { |
| 42 | 1 | description.appendText("with item matching ("); |
| 43 | 1 | this.itemMatcher.describeTo(description); |
| 44 | 1 | description.appendText(")"); |
| 45 | 1 | } |
| 46 | |
|
| 47 | |
protected boolean doMatch(Object obj) { |
| 48 | 8 | boolean result = false; |
| 49 | |
try { |
| 50 | 32 | for (T item : getItems(obj)) { |
| 51 | 24 | if (this.itemMatcher.matches(item)) { |
| 52 | 18 | this.matches.add(item); |
| 53 | 18 | result = true; |
| 54 | |
} |
| 55 | |
} |
| 56 | 0 | } catch (Exception e) { |
| 57 | |
|
| 58 | |
} |
| 59 | 8 | return result; |
| 60 | |
} |
| 61 | |
|
| 62 | |
@SuppressWarnings("unchecked") |
| 63 | |
private T[] getItems(Object obj) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { |
| 64 | 8 | return (T[]) SWTUtils.invokeMethod(obj, "getItems"); |
| 65 | |
} |
| 66 | |
|
| 67 | |
public ArrayList<T> getAllMatches() { |
| 68 | 8 | return this.matches; |
| 69 | |
} |
| 70 | |
|
| 71 | |
public T get(int index) { |
| 72 | 0 | return this.matches.get(index); |
| 73 | |
} |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
@Factory |
| 85 | |
public static <T extends Item> WithItem<T> withItem(Matcher<?> matcher) { |
| 86 | 9 | return new WithItem<T>(matcher); |
| 87 | |
} |
| 88 | |
} |