|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| JTextComponentMatcher | 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.text.JTextComponent; | |
| 24 | ||
| 25 | import org.fest.swing.annotation.RunsInCurrentThread; | |
| 26 | ||
| 27 | /** | |
| 28 | * Understands matching a <code>{@link JTextComponent}</code> by type, name or text. | |
| 29 | * | |
| 30 | * @author Alex Ruiz | |
| 31 | */ | |
| 32 | public final class JTextComponentMatcher extends NamedComponentMatcherTemplate<JTextComponent> { | |
| 33 | ||
| 34 | private Object text; | |
| 35 | ||
| 36 | /** | |
| 37 | * Creates a new <code>{@link JTextComponentMatcher}</code> that matches a <code>{@link JTextComponent}</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 JTextComponent}</code> by name and text: | |
| 44 | * <pre> | |
| 45 | * JTextComponentMatcher m = {@link #withName(String) withName}("lastName").{@link #andText(String) andText}("Wang"); | |
| 46 | * </pre> | |
| 47 | * </p> | |
| 48 | * <p> | |
| 49 | * The following code listing shows how to match a <code>{@link JTextComponent}</code>, that should be showing on the screen, | |
| 50 | * by name and text: | |
| 51 | * <pre> | |
| 52 | * JTextComponentMatcher m = {@link #withName(String) withName}("lastName").{@link #andText(String) andText}("Wang").{@link #andShowing() andShowing}(); | |
| 53 | * </pre> | |
| 54 | * </p> | |
| 55 | * @param name the id to match. | |
| 56 | * @return the created matcher. | |
| 57 | */ | |
| 58 | 12 |
public static JTextComponentMatcher withName(String name) { |
| 59 | 12 | return new JTextComponentMatcher(name, ANY); |
| 60 | } | |
| 61 | ||
| 62 | /** | |
| 63 | * Creates a new <code>{@link JTextComponentMatcher}</code> that matches a <code>{@link JTextComponent}</code> by its | |
| 64 | * text. | |
| 65 | * <p> | |
| 66 | * The following code listing shows how to match a <code>{@link JTextComponent}</code> by text: | |
| 67 | * <pre> | |
| 68 | * JTextComponentMatcher m = {@link #withText(String) withText}("Wang"); | |
| 69 | * </pre> | |
| 70 | * </p> | |
| 71 | * <p> | |
| 72 | * The following code listing shows how to match a <code>{@link JTextComponent}</code>, that should be showing on the | |
| 73 | * screen, by text: | |
| 74 | * <pre> | |
| 75 | * JTextComponentMatcher m = {@link #withText(String) withText}("Wang").{@link #andShowing() andShowing}(); | |
| 76 | * </pre> | |
| 77 | * </p> | |
| 78 | * @param text the text to match. It can be a regular expression. | |
| 79 | * @return the created matcher. | |
| 80 | */ | |
| 81 | 7 |
public static JTextComponentMatcher withText(String text) { |
| 82 | 7 | return new JTextComponentMatcher(ANY, text); |
| 83 | } | |
| 84 | ||
| 85 | /** | |
| 86 | * Creates a new <code>{@link JTextComponentMatcher}</code> that matches a <code>{@link JTextComponent}</code> by its | |
| 87 | * text. | |
| 88 | * <p> | |
| 89 | * The following code listing shows how to match a <code>{@link JTextComponent}</code> by text, using a regular | |
| 90 | * expression pattern: | |
| 91 | * <pre> | |
| 92 | * JTextComponentMatcher m = {@link #withText(Pattern) withText}(Pattern.compile("W.*")); | |
| 93 | * </pre> | |
| 94 | * </p> | |
| 95 | * <p> | |
| 96 | * The following code listing shows how to match a <code>{@link JTextComponent}</code>, that should be showing on the | |
| 97 | * screen, by text, using a regular expression pattern: | |
| 98 | * <pre> | |
| 99 | * JTextComponentMatcher m = {@link #withText(Pattern) withText}(Pattern.compile("W.*")).{@link #andShowing() andShowing}(); | |
| 100 | * </pre> | |
| 101 | * </p> | |
| 102 | * @param textPattern the text to match. It can be a regular expression. | |
| 103 | * @return the created matcher. | |
| 104 | */ | |
| 105 | 2 |
public static JTextComponentMatcher withText(Pattern textPattern) { |
| 106 | 2 | return new JTextComponentMatcher(ANY, textPattern); |
| 107 | } | |
| 108 | ||
| 109 | /** | |
| 110 | * Creates a new <code>{@link JTextComponentMatcher}</code> that matches any <code>{@link JTextComponent}</code>. | |
| 111 | * @return the created matcher. | |
| 112 | */ | |
| 113 | 2 |
public static JTextComponentMatcher any() { |
| 114 | 2 | return new JTextComponentMatcher(ANY, ANY); |
| 115 | } | |
| 116 | ||
| 117 | 23 |
private JTextComponentMatcher(Object name, Object text) { |
| 118 | 23 | super(JTextComponent.class, name); |
| 119 | 23 | this.text = text; |
| 120 | } | |
| 121 | ||
| 122 | /** | |
| 123 | * Specifies the text to match. If this matcher was created using <code>{@link #withText(String)}</code> or | |
| 124 | * <code>{@link #withText(Pattern)}</code>, this method will simply update the text to match. | |
| 125 | * @param newText the new text to match. It can be a regular expression. | |
| 126 | * @return this matcher. | |
| 127 | */ | |
| 128 | 6 |
public JTextComponentMatcher andText(String newText) { |
| 129 | 6 | text = newText; |
| 130 | 6 | return this; |
| 131 | } | |
| 132 | ||
| 133 | /** | |
| 134 | * Specifies the text to match. If this matcher was created using <code>{@link #withText(String)}</code> or | |
| 135 | * <code>{@link #withText(Pattern)}</code>, this method will simply update the text to match. | |
| 136 | * @param textPattern the regular expression pattern to match. | |
| 137 | * @return this matcher. | |
| 138 | * @since 1.2 | |
| 139 | */ | |
| 140 | 4 |
public JTextComponentMatcher andText(Pattern textPattern) { |
| 141 | 4 | text = textPattern; |
| 142 | 4 | return this; |
| 143 | } | |
| 144 | ||
| 145 | /** | |
| 146 | * Indicates that the <code>{@link JTextComponent}</code> to match should be showing on the screen. | |
| 147 | * @return this matcher. | |
| 148 | */ | |
| 149 | 5 |
public JTextComponentMatcher andShowing() { |
| 150 | 5 | requireShowing(true); |
| 151 | 5 | return this; |
| 152 | } | |
| 153 | ||
| 154 | /** | |
| 155 | * Indicates whether the text of the given <code>{@link JTextComponent}</code> is equal to the text in this matcher. | |
| 156 | * <p> | |
| 157 | * <b>Note:</b> This method is <b>not</b> guaranteed to be executed in the event dispatch thread (EDT.) Clients are | |
| 158 | * responsible for calling this method from the EDT. | |
| 159 | * </p> | |
| 160 | * @param button the <code>JTextComponent</code> to match. | |
| 161 | * @return <code>true</code> if the text in the <code>JTextComponent</code> is equal to the text in this matcher, | |
| 162 | * <code>false</code> otherwise. | |
| 163 | */ | |
| 164 | 19 |
@RunsInCurrentThread |
| 165 | protected boolean isMatching(JTextComponent button) { | |
| 166 | 5 | if (!isNameMatching(button.getName())) return false; |
| 167 | 14 | return arePropertyValuesMatching(text, button.getText()); |
| 168 | } | |
| 169 | ||
| 170 | 1 |
@Override public String toString() { |
| 171 | 1 | return concat( |
| 172 | getClass().getName(), "[", | |
| 173 | "name=", quotedName(), ", ", | |
| 174 | "text=", quoted(text), ", ", | |
| 175 | "requireShowing=", valueOf(requireShowing()), | |
| 176 | "]" | |
| 177 | ); | |
| 178 | } | |
| 179 | } | |
|
||||||||||||