All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.assertj.swing.text.TextReader Maven / Gradle / Ivy

There is a newer version: 3.17.1
Show newest version
/**
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * Copyright 2012-2015 the original author or authors.
 */
package org.assertj.swing.text;

import java.awt.Component;

import javax.annotation.Nonnull;

import org.assertj.swing.annotation.RunsInCurrentThread;

/**
 * Reads the text of an AWT or Swing {@code Component}.
 * 
 * @param  the type of {@code Component} this reader supports.
 * 
 * @author Alex Ruiz
 */
public abstract class TextReader {
  /**
   * @return the type of AWT or Swing {@code Component} this reader supports.
   */
  public abstract @Nonnull Class supportedComponent();

  /**
   * Indicates whether the given AWT or Swing {@code Component} contains or displays the given text.
   * 
   * @param c the given {@code Component}.
   * @param text the given text.
   * @return {@code true} if the given {@code Component} contains or displays the given text; {@code false} otherwise.
   * @throws IllegalArgumentException if this reader does not support the type of the given {@code Component} (e.g. this
   *           reader supports {@code JButton}s and a {@code JLabel} is passed).
   * @see #supportedComponent()
   */
  @RunsInCurrentThread
  public final boolean containsText(@Nonnull Component c, @Nonnull String text) {
    checkCorrectType(c);
    T casted = supportedComponent().cast(c);
    return checkContainsText(casted, text);
  }

  private void checkCorrectType(Component c) {
    Class type = supportedComponent();
    if (!type.isAssignableFrom(c.getClass())) {
      String msg = String.format("Expecting component of type %s but got %s", type.getName(), c.getClass().getName());
      throw new IllegalArgumentException(msg);
    }
  }

  /**
   * Indicates whether the given AWT or Swing {@code Component} contains or displays the given text. Implementations
   * must ensure that they are executed in the current thread.
   * 
   * @param component the given {@code Component}.
   * @param text the given text.
   * @return {@code true} if the given {@code Component} contains or displays the given text; {@code false} otherwise.
   */
  @RunsInCurrentThread
  protected abstract boolean checkContainsText(@Nonnull T component, @Nonnull String text);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy