Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
WithMnemonic |
|
| 1.5;1.5 |
1 | /******************************************************************************* | |
2 | * Copyright (c) 2008 Ketan Padegaonkar and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * Ketan Padegaonkar - initial API and implementation | |
10 | * Ketan Padegaonkar - http://swtbot.org/bugzilla/show_bug.cgi?id=126 | |
11 | *******************************************************************************/ | |
12 | package org.eclipse.swtbot.swt.finder.matchers; | |
13 | ||
14 | import java.lang.reflect.InvocationTargetException; | |
15 | import java.lang.reflect.Method; | |
16 | ||
17 | import org.eclipse.swt.widgets.Widget; | |
18 | import org.hamcrest.Description; | |
19 | import org.hamcrest.Factory; | |
20 | import org.hamcrest.Matcher; | |
21 | ||
22 | /** | |
23 | * Matches {@link org.eclipse.swt.widgets.Widget}s with the specified text. Skips mnemonics, so "Username" will match | |
24 | * items with text "&Username" and "User&name" | |
25 | * | |
26 | * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> | |
27 | * @version $Id$ | |
28 | * @since 2.0 | |
29 | */ | |
30 | public class WithMnemonic<T extends Widget> extends AbstractMatcher<T> { | |
31 | private final String text; | |
32 | private final boolean ignoreCase; | |
33 | ||
34 | /** | |
35 | * Constructs the Mnemonic text matcher with the given text. | |
36 | * | |
37 | * @param text the mnemonic to match on the {@link org.eclipse.swt.widgets.Widget} | |
38 | */ | |
39 | WithMnemonic(String text) { | |
40 | 1685 | this(text, false); |
41 | 1685 | } |
42 | ||
43 | /** | |
44 | * Constructs the Mnemonic text matcher with the given text. | |
45 | * | |
46 | * @param text the mnemonic to match on the {@link org.eclipse.swt.widgets.Widget} | |
47 | * @param ignoreCase Determines if this should ignore case during the comparison. | |
48 | */ | |
49 | 1685 | WithMnemonic(String text, boolean ignoreCase) { |
50 | 1685 | this.text = text.replaceAll("&", ""); //$NON-NLS-1$ //$NON-NLS-2$ |
51 | 1685 | this.ignoreCase = ignoreCase; |
52 | 1685 | } |
53 | ||
54 | /** | |
55 | * Extends the behaviour of WithText my removing the mnemonics "&" that are used as keyboard accessors from the | |
56 | * text. | |
57 | * | |
58 | * @see org.eclipse.swtbot.swt.finder.matchers.WithText#getText(java.lang.Object) | |
59 | * @param obj The object to get the text from. | |
60 | * @return The newly formated string. | |
61 | * @throws NoSuchMethodException if the method "getText" does not exist on the object. | |
62 | * @throws IllegalAccessException if the java access control does not allow invocation. | |
63 | * @throws InvocationTargetException if the method "getText" throws an exception. | |
64 | * @see Method#invoke(Object, Object[]) | |
65 | */ | |
66 | String getText(Object obj) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { | |
67 | 42332 | return WithText.getText(obj).replaceAll("&", "").split("\t")[0]; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
68 | } | |
69 | ||
70 | public void describeTo(Description description) { | |
71 | 6918 | description.appendText("with mnemonic '").appendText(text).appendText("'"); //$NON-NLS-1$ //$NON-NLS-2$ |
72 | 6918 | } |
73 | ||
74 | protected boolean doMatch(Object obj) { | |
75 | try { | |
76 | 42332 | boolean result = false; |
77 | 42332 | if (ignoreCase) |
78 | 0 | result = getText(obj).equalsIgnoreCase(text); |
79 | else | |
80 | 42332 | result = getText(obj).equals(text); |
81 | 42326 | return result; |
82 | 6 | } catch (Exception e) { |
83 | // do nothing | |
84 | } | |
85 | 6 | return false; |
86 | } | |
87 | ||
88 | /** | |
89 | * Matches a widget that has the specified text, after striping the mnemonics "&" | |
90 | * | |
91 | * @param text the text. | |
92 | * @return a matcher. | |
93 | * @since 2.0 | |
94 | */ | |
95 | @Factory | |
96 | public static <T extends Widget> Matcher<T> withMnemonic(String text) { | |
97 | 1334 | return new WithMnemonic<T>(text); |
98 | } | |
99 | ||
100 | } |