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