/tags/fest-swing-1.0/src/main/java/org/fest/swing/core/matcher/JTextComponentMatcher.java

http://fest.googlecode.com/ · Java · 128 lines · 43 code · 14 blank · 71 comment · 1 complexity · d4cc8e8bea115d7c02b43bb76e6c398c MD5 · raw file

  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-2009 the original author or authors.
  15. */
  16. package org.fest.swing.core.matcher;
  17. import javax.swing.text.JTextComponent;
  18. import org.fest.swing.annotation.RunsInCurrentThread;
  19. import static java.lang.String.valueOf;
  20. import static org.fest.util.Strings.concat;
  21. /**
  22. * Understands matching a <code>{@link JTextComponent}</code> by type, name or text.
  23. *
  24. * @author Alex Ruiz
  25. */
  26. public class JTextComponentMatcher extends NamedComponentMatcherTemplate<JTextComponent> {
  27. private Object text;
  28. /**
  29. * Creates a new <code>{@link JTextComponentMatcher}</code> that matches a <code>{@link JTextComponent}</code> that:
  30. * <ol>
  31. * <li>has a matching name</li>
  32. * <li>(optionally) has matching text</li>
  33. * <li>(optionally) is showing on the screen</li>
  34. * <p>
  35. * The following code listing shows how to match a <code>{@link JTextComponent}</code> by name and text:
  36. * <pre>
  37. * JTextComponentMatcher m = {@link #withName(String) withName}("ok").{@link #andText(String) andText}("OK");
  38. * </pre>
  39. * </p>
  40. * <p>
  41. * The following code listing shows how to match a <code>{@link JTextComponent}</code>, that should be showing on the screen,
  42. * by name and text:
  43. * <pre>
  44. * JTextComponentMatcher m = {@link #withName(String) withName}("ok").{@link #andText(String) andText}("OK").{@link #andShowing() andShowing}();
  45. * </pre>
  46. * </p>
  47. * @param name the id to match.
  48. * @return the created matcher.
  49. */
  50. public static JTextComponentMatcher withName(String name) {
  51. return new JTextComponentMatcher(name, ANY);
  52. }
  53. /**
  54. * Creates a new <code>{@link JTextComponentMatcher}</code> that matches a <code>{@link JTextComponent}</code> by its text.
  55. * @param text the text to match.
  56. * @return the created matcher.
  57. */
  58. public static JTextComponentMatcher withText(String text) {
  59. return new JTextComponentMatcher(ANY, text);
  60. }
  61. /**
  62. * Creates a new <code>{@link JTextComponentMatcher}</code> that matches any <code>{@link JTextComponent}</code>.
  63. * @return the created matcher.
  64. */
  65. public static JTextComponentMatcher any() {
  66. return new JTextComponentMatcher(ANY, ANY);
  67. }
  68. private JTextComponentMatcher(Object name, Object text) {
  69. super(JTextComponent.class, name);
  70. this.text = text;
  71. }
  72. /**
  73. * Specifies the text to match. If this matcher was created using <code>{@link #withText(String)}</code>, this method
  74. * will simply update the text to match.
  75. * @param newText the new text to match.
  76. * @return this matcher.
  77. */
  78. public JTextComponentMatcher andText(String newText) {
  79. text = newText;
  80. return this;
  81. }
  82. /**
  83. * Indicates that the <code>{@link JTextComponent}</code> to match should be showing on the screen.
  84. * @return this matcher.
  85. */
  86. public JTextComponentMatcher andShowing() {
  87. requireShowing(true);
  88. return this;
  89. }
  90. /**
  91. * Indicates whether the text of the given <code>{@link JTextComponent}</code> is equal to the text in this matcher.
  92. * <p>
  93. * <b>Note:</b> This method is <b>not</b> executed in the event dispatch thread (EDT.) Clients are responsible for
  94. * invoking this method in the EDT.
  95. * </p>
  96. * @param button the <code>JTextComponent</code> to match.
  97. * @return <code>true</code> if the text in the <code>JTextComponent</code> is equal to the text in this matcher,
  98. * <code>false</code> otherwise.
  99. */
  100. @RunsInCurrentThread
  101. protected boolean isMatching(JTextComponent button) {
  102. if (!isNameMatching(button.getName())) return false;
  103. return arePropertyValuesMatching(text, button.getText());
  104. }
  105. @Override public String toString() {
  106. return concat(
  107. getClass().getName(), "[",
  108. "name=", quotedName(), ", ",
  109. "text=", quoted(text), ", ",
  110. "requireShowing=", valueOf(requireShowing()),
  111. "]"
  112. );
  113. }
  114. }