|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DialogMatcher | Line # 31 | 16 | 0% | 11 | 0 | 100% |
1.0
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| No Tests | |||
| 1 | /* | |
| 2 | * Created on Jul 16, 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.awt.Dialog; | |
| 22 | import java.util.regex.Pattern; | |
| 23 | ||
| 24 | import org.fest.swing.annotation.RunsInCurrentThread; | |
| 25 | ||
| 26 | /** | |
| 27 | * Understands matching a <code>{@link Dialog}</code> by name, title and visibility on the screen. | |
| 28 | * | |
| 29 | * @author Alex Ruiz | |
| 30 | */ | |
| 31 | public final class DialogMatcher extends NamedComponentMatcherTemplate<Dialog> { | |
| 32 | ||
| 33 | private Object title; | |
| 34 | ||
| 35 | /** | |
| 36 | * Creates a new <code>{@link DialogMatcher}</code> that matches a <code>{@link Dialog}</code> that: | |
| 37 | * <ol> | |
| 38 | * <li>has a matching name</li> | |
| 39 | * <li>(optionally) has matching title</li> | |
| 40 | * <li>(optionally) is showing on the screen</li> | |
| 41 | * <p> | |
| 42 | * The following code listing shows how to match a <code>{@link Dialog}</code> by name and title: | |
| 43 | * <pre> | |
| 44 | * DialogMatcher m = {@link #withName(String) withName}("saveFile").{@link #andTitle(String) andTitle}("Save File"); | |
| 45 | * </pre> | |
| 46 | * </p> | |
| 47 | * <p> | |
| 48 | * The following code listing shows how to match a <code>{@link Dialog}</code>, that should be showing on the screen, | |
| 49 | * by name and title: | |
| 50 | * <pre> | |
| 51 | * DialogMatcher m = {@link #withName(String) withName}("saveFile").{@link #andTitle(String) andTitle}("Save File").{@link #andShowing() andShowing}(); | |
| 52 | * </pre> | |
| 53 | * </p> | |
| 54 | * @param name the id to match. | |
| 55 | * @return the created matcher. | |
| 56 | */ | |
| 57 | 12 |
public static DialogMatcher withName(String name) { |
| 58 | 12 | return new DialogMatcher(name, ANY); |
| 59 | } | |
| 60 | ||
| 61 | /** | |
| 62 | * Creates a new <code>{@link DialogMatcher}</code> that matches a <code>{@link Dialog}</code> by its title. | |
| 63 | * <p> | |
| 64 | * The following code listing shows how to match a <code>{@link Dialog}</code> title: | |
| 65 | * <pre> | |
| 66 | * DialogMatcher m = {@link #withTitle(String) withTitle}("Save File"); | |
| 67 | * </pre> | |
| 68 | * </p> | |
| 69 | * <p> | |
| 70 | * The following code listing shows how to match a <code>{@link Dialog}</code>, that should be showing on the screen, | |
| 71 | * by title: | |
| 72 | * <pre> | |
| 73 | * DialogMatcher m = {@link #withTitle(String) withTitle}("Save File").{@link #andShowing() andShowing}(); | |
| 74 | * </pre> | |
| 75 | * </p> | |
| 76 | * @param title the title to match. It can be a regular expression. | |
| 77 | * @return the created matcher. | |
| 78 | */ | |
| 79 | 7 |
public static DialogMatcher withTitle(String title) { |
| 80 | 7 | return new DialogMatcher(ANY, title); |
| 81 | } | |
| 82 | ||
| 83 | /** | |
| 84 | * Creates a new <code>{@link DialogMatcher}</code> that matches a <code>{@link Dialog}</code> by its title. | |
| 85 | * <p> | |
| 86 | * The following code listing shows how to match a <code>{@link Dialog}</code> title, using a regular expression | |
| 87 | * pattern: | |
| 88 | * <pre> | |
| 89 | * DialogMatcher m = {@link #withTitle(Pattern) withTitle}(Pattern.compile("Sav.*")); | |
| 90 | * </pre> | |
| 91 | * </p> | |
| 92 | * <p> | |
| 93 | * The following code listing shows how to match a <code>{@link Dialog}</code>, that should be showing on the screen, | |
| 94 | * by title, using a regular expression pattern: | |
| 95 | * <pre> | |
| 96 | * DialogMatcher m = {@link #withTitle(Pattern) withTitle}(Pattern.compile("Sav.*")).{@link #andShowing() andShowing}(); | |
| 97 | * </pre> | |
| 98 | * </p> | |
| 99 | * @param titlePattern the regular expression pattern to match. | |
| 100 | * @return the created matcher. | |
| 101 | * @since 1.2 | |
| 102 | */ | |
| 103 | 2 |
public static DialogMatcher withTitle(Pattern titlePattern) { |
| 104 | 2 | return new DialogMatcher(ANY, titlePattern); |
| 105 | } | |
| 106 | ||
| 107 | /** | |
| 108 | * Creates a new <code>{@link DialogMatcher}</code> that matches any <code>{@link Dialog}</code>. | |
| 109 | * @return the created matcher. | |
| 110 | */ | |
| 111 | 2 |
public static DialogMatcher any() { |
| 112 | 2 | return new DialogMatcher(ANY, ANY); |
| 113 | } | |
| 114 | ||
| 115 | 23 |
private DialogMatcher(Object name, Object title) { |
| 116 | 23 | super(Dialog.class, name); |
| 117 | 23 | this.title = title; |
| 118 | } | |
| 119 | ||
| 120 | /** | |
| 121 | * Specifies the title to match. If this matcher was created using <code>{@link #withTitle(String)}</code> or | |
| 122 | * <code>{@link #withTitle(Pattern)}</code>, this method will simply update the title to match. | |
| 123 | * @param newTitle the new title to match. It can be a regular expression. | |
| 124 | * @return this matcher. | |
| 125 | */ | |
| 126 | 6 |
public DialogMatcher andTitle(String newTitle) { |
| 127 | 6 | title = newTitle; |
| 128 | 6 | return this; |
| 129 | } | |
| 130 | ||
| 131 | ||
| 132 | /** | |
| 133 | * Specifies the title to match. If this matcher was created using <code>{@link #withTitle(String)}</code>, or | |
| 134 | * <code>{@link #withTitle(Pattern)}</code> this method will simply update the title to match. | |
| 135 | * @param titlePattern the regular expression pattern to match. | |
| 136 | * @return this matcher. | |
| 137 | * @since 1.2 | |
| 138 | */ | |
| 139 | 4 |
public DialogMatcher andTitle(Pattern titlePattern) { |
| 140 | 4 | title = titlePattern; |
| 141 | 4 | return this; |
| 142 | } | |
| 143 | ||
| 144 | /** | |
| 145 | * Indicates that the <code>{@link Dialog}</code> to match should be showing on the screen. | |
| 146 | * @return this matcher. | |
| 147 | */ | |
| 148 | 5 |
public DialogMatcher andShowing() { |
| 149 | 5 | requireShowing(true); |
| 150 | 5 | return this; |
| 151 | } | |
| 152 | ||
| 153 | /** | |
| 154 | * Indicates whether: | |
| 155 | * <ul> | |
| 156 | * <li>the name of the given <code>Dialog</code> is equal to the name in this matcher, and</li> | |
| 157 | * <li>the title of the given <code>Dialog</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 dialog the <code>Dialog</code> to match. | |
| 164 | * @return <code>true</code> if the <code>Dialog</code> matches the search criteria in this matcher. | |
| 165 | */ | |
| 166 | 19 |
@RunsInCurrentThread |
| 167 | protected boolean isMatching(Dialog dialog) { | |
| 168 | 5 | if (!isNameMatching(dialog.getName())) return false; |
| 169 | 14 | return arePropertyValuesMatching(title, dialog.getTitle()); |
| 170 | } | |
| 171 | ||
| 172 | 1 |
@Override public String toString() { |
| 173 | 1 | return concat( |
| 174 | getClass().getName(), "[", | |
| 175 | "name=", quotedName(), ", ", | |
| 176 | "title=", quoted(title), ", ", | |
| 177 | "requireShowing=", valueOf(requireShowing()), | |
| 178 | "]" | |
| 179 | ); | |
| 180 | } | |
| 181 | } | |
|
||||||||||||