|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| JButtonMatcher | Line # 32 | 16 | 0% | 11 | 0 | 100% |
1.0
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| No Tests | |||
| 1 | /* | |
| 2 | * Created on Jul 17, 2008 | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | |
| 5 | * in compliance with the License. You may obtain a copy of the License at | |
| 6 | * | |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 | * | |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License | |
| 10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | |
| 11 | * or implied. See the License for the specific language governing permissions and limitations under | |
| 12 | * the License. | |
| 13 | * | |
| 14 | * Copyright @2008-2010 the original author or authors. | |
| 15 | */ | |
| 16 | package org.fest.swing.core.matcher; | |
| 17 | ||
| 18 | import static java.lang.String.valueOf; | |
| 19 | import static org.fest.util.Strings.concat; | |
| 20 | ||
| 21 | import java.util.regex.Pattern; | |
| 22 | ||
| 23 | import javax.swing.JButton; | |
| 24 | ||
| 25 | import org.fest.swing.annotation.RunsInCurrentThread; | |
| 26 | ||
| 27 | /** | |
| 28 | * Understands matching a <code>{@link JButton}</code> by name, text and visibility on the screen. | |
| 29 | * | |
| 30 | * @author Alex Ruiz | |
| 31 | */ | |
| 32 | public final class JButtonMatcher extends NamedComponentMatcherTemplate<JButton> { | |
| 33 | ||
| 34 | private Object text; | |
| 35 | ||
| 36 | /** | |
| 37 | * Creates a new <code>{@link JButtonMatcher}</code> that matches a <code>{@link JButton}</code> that: | |
| 38 | * <ol> | |
| 39 | * <li>has a matching name</li> | |
| 40 | * <li>(optionally) has matching text</li> | |
| 41 | * <li>(optionally) is showing on the screen</li> | |
| 42 | * <p> | |
| 43 | * The following code listing shows how to match a <code>{@link JButton}</code> by name and text: | |
| 44 | * <pre> | |
| 45 | * JButtonMatcher m = {@link #withName(String) withName}("ok").{@link #andText(String) andText}("OK"); | |
| 46 | * </pre> | |
| 47 | * </p> | |
| 48 | * <p> | |
| 49 | * The following code listing shows how to match a <code>{@link JButton}</code>, that should be showing on the screen, | |
| 50 | * by name and text: | |
| 51 | * <pre> | |
| 52 | * JButtonMatcher m = {@link #withName(String) withName}("ok").{@link #andText(String) andText}("OK").{@link #andShowing() andShowing}(); | |
| 53 | * </pre> | |
| 54 | * </p> | |
| 55 | * @param name the name to match. | |
| 56 | * @return the created matcher. | |
| 57 | */ | |
| 58 | 12 |
public static JButtonMatcher withName(String name) { |
| 59 | 12 | return new JButtonMatcher(name, ANY); |
| 60 | } | |
| 61 | ||
| 62 | /** | |
| 63 | * Creates a new <code>{@link JButtonMatcher}</code> that matches a <code>{@link JButton}</code> by its text. | |
| 64 | * <p> | |
| 65 | * The following code listing shows how to match a <code>{@link JButton}</code> by text: | |
| 66 | * <pre> | |
| 67 | * JButtonMatcher m = {@link #withText(String) withText}("OK"); | |
| 68 | * </pre> | |
| 69 | * </p> | |
| 70 | * <p> | |
| 71 | * The following code listing shows how to match a <code>{@link JButton}</code>, that should be showing on the screen, | |
| 72 | * by text: | |
| 73 | * <pre> | |
| 74 | * JButtonMatcher m = {@link #withText(String) withText}("OK").{@link #andShowing() andShowing}(); | |
| 75 | * </pre> | |
| 76 | * </p> | |
| 77 | * @param text the text to match. It can be a regular expression. | |
| 78 | * @return the created matcher. | |
| 79 | */ | |
| 80 | 15 |
public static JButtonMatcher withText(String text) { |
| 81 | 15 | return new JButtonMatcher(ANY, text); |
| 82 | } | |
| 83 | ||
| 84 | /** | |
| 85 | * Creates a new <code>{@link JButtonMatcher}</code> that matches a <code>{@link JButton}</code> by its text. | |
| 86 | * <p> | |
| 87 | * The following code listing shows how to match a <code>{@link JButton}</code> by text, using a regular expression | |
| 88 | * pattern: | |
| 89 | * <pre> | |
| 90 | * JButtonMatcher m = {@link #withText(Pattern) withText}(Pattern.compile("O.*")); | |
| 91 | * </pre> | |
| 92 | * </p> | |
| 93 | * <p> | |
| 94 | * The following code listing shows how to match a <code>{@link JButton}</code>, that should be showing on the screen, | |
| 95 | * by text, using a regular expression pattern: | |
| 96 | * <pre> | |
| 97 | * JButtonMatcher m = {@link #withText(Pattern) withText}(Pattern.compile("O.*")).{@link #andShowing() andShowing}(); | |
| 98 | * </pre> | |
| 99 | * </p> | |
| 100 | * @param textPattern the regular expression pattern to match. | |
| 101 | * @return the created matcher. | |
| 102 | * @since 1.2 | |
| 103 | */ | |
| 104 | 3 |
public static JButtonMatcher withText(Pattern textPattern) { |
| 105 | 3 | return new JButtonMatcher(ANY, textPattern); |
| 106 | } | |
| 107 | ||
| 108 | /** | |
| 109 | * Creates a new <code>{@link JButtonMatcher}</code> that matches any <code>{@link JButton}</code>. | |
| 110 | * @return the created matcher. | |
| 111 | */ | |
| 112 | 2 |
public static JButtonMatcher any() { |
| 113 | 2 | return new JButtonMatcher(ANY, ANY); |
| 114 | } | |
| 115 | ||
| 116 | 32 |
private JButtonMatcher(Object name, Object text) { |
| 117 | 32 | super(JButton.class, name); |
| 118 | 32 | this.text = text; |
| 119 | } | |
| 120 | ||
| 121 | /** | |
| 122 | * Specifies the text to match. If this matcher was created using <code>{@link #withText(String)}</code> or | |
| 123 | * <code>{@link #withText(Pattern)}</code>, this method will simply update the text to match. | |
| 124 | * @param newText the new text to match. It can be a regular expression. | |
| 125 | * @return this matcher. | |
| 126 | */ | |
| 127 | 6 |
public JButtonMatcher andText(String newText) { |
| 128 | 6 | text = newText; |
| 129 | 6 | return this; |
| 130 | } | |
| 131 | ||
| 132 | /** | |
| 133 | * Specifies the text to match. If this matcher was created using <code>{@link #withText(String)}</code> or | |
| 134 | * <code>{@link #withText(Pattern)}</code>, this method will simply update the text to match. | |
| 135 | * @param textPattern the regular expression pattern to match. | |
| 136 | * @return this matcher. | |
| 137 | * @since 1.2 | |
| 138 | */ | |
| 139 | 4 |
public JButtonMatcher andText(Pattern textPattern) { |
| 140 | 4 | text = textPattern; |
| 141 | 4 | return this; |
| 142 | } | |
| 143 | ||
| 144 | /** | |
| 145 | * Indicates that the <code>{@link JButton}</code> to match should be showing on the screen. | |
| 146 | * @return this matcher. | |
| 147 | */ | |
| 148 | 14 |
public JButtonMatcher andShowing() { |
| 149 | 14 | requireShowing(true); |
| 150 | 14 | return this; |
| 151 | } | |
| 152 | ||
| 153 | /** | |
| 154 | * Indicates whether: | |
| 155 | * <ul> | |
| 156 | * <li>the name of the given <code>JButton</code> is equal to the name in this matcher, and</li> | |
| 157 | * <li>the text of the given <code>JButton</code> matches the text (or pattern) in this matcher</li> | |
| 158 | * </ul> | |
| 159 | * <p> | |
| 160 | * <b>Note:</b> This method is <b>not</b> guaranteed to be executed in the event dispatch thread (EDT.) Clients are | |
| 161 | * responsible for calling this method from the EDT. | |
| 162 | * </p> | |
| 163 | * @param button the <code>JButton</code> to match. | |
| 164 | * @return <code>true</code> if the <code>JButton</code> matches the search criteria in this matcher. | |
| 165 | */ | |
| 166 | 50 |
@RunsInCurrentThread |
| 167 | protected boolean isMatching(JButton button) { | |
| 168 | 5 | if (!isNameMatching(button.getName())) return false; |
| 169 | 45 | return arePropertyValuesMatching(text, button.getText()); |
| 170 | } | |
| 171 | ||
| 172 | 1 |
@Override public String toString() { |
| 173 | 1 | return concat( |
| 174 | getClass().getName(), "[", | |
| 175 | "name=", quotedName(), ", ", | |
| 176 | "text=", quoted(text), ", ", | |
| 177 | "requireShowing=", valueOf(requireShowing()), | |
| 178 | "]" | |
| 179 | ); | |
| 180 | } | |
| 181 | } | |
|
||||||||||||