⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 panel.java

📁 基于Junit的 功能和单元测试的的测试工具。只支持Swing.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.uispec4j;

import junit.framework.Assert;
import junit.framework.AssertionFailedError;
import org.uispec4j.assertion.Assertion;
import org.uispec4j.finder.ComponentFinder;
import org.uispec4j.finder.ComponentMatcher;
import org.uispec4j.finder.ComponentMatchers;
import org.uispec4j.utils.UIComponentAnalyzer;
import org.uispec4j.utils.UIComponentFactory;

import javax.swing.*;
import java.awt.Component;
import java.awt.Container;
import java.util.ArrayList;

/**
 * General container for UI components.<p>
 * This class offers a set of "getXxx" methods for retrieving the different kinds of UIComponent
 * instances laid out in a GUI panel.<p>
 * It also provides a set of generic find/get methods, with the following naming logic:
 * <ul>
 * <li>'find...' stands for a unitary search that returns null when nothing was found</li>
 * <li>'getXxxComponent' stands for a unitary search that throws an exception
 * when nothing was found</li>
 * <li>'getXxxComponent<em>s</em>' stands for plural search and returns an empty array
 * when nothing was found</li>
 * <li>'getXxxComponent<em>s</em>' stands for plural search and returns an empty array
 * when nothing was found</li>
 * <li>'containsXxxComponent<em>s</em>' returns an assertion for checking the presence
 * of a component</li>
 * </ul>
 * NOTE: A Panel can be created from any AWT Container, but when a Panel is searched with the
 * {@link #getPanel(String)} method only components of type JPanel JInternalFrame, etc. will be
 * considered.
 *
 * @noinspection ReturnOfNull
 */
public class Panel extends AbstractUIComponent {
  public static final String TYPE_NAME = "panel";
  public static final Class[] SWING_CLASSES = {JPanel.class, JInternalFrame.class,
      JViewport.class, JScrollPane.class,
      JRootPane.class};

  private Container container;
  private ComponentFinder finder;

  public Panel(Container container) {
    this.container = container;
    this.finder = new ComponentFinder(container);
  }

  public String getDescriptionTypeName() {
    return TYPE_NAME;
  }

  public Component getAwtComponent() {
    return container;
  }

  public Container getAwtContainer() {
    return container;
  }

  public Button getButton(String name) throws ItemNotFoundException, ComponentAmbiguityException {
    return (Button) getComponent(finder, Button.class, name);
  }

  public Button getButton() throws ItemNotFoundException, ComponentAmbiguityException {
    return (Button) getComponent(finder, Button.class, null);
  }

  public Button getButton(final ComponentMatcher matcher) throws ItemNotFoundException, ComponentAmbiguityException {
    return (Button) getComponent(finder, getMatcherByClass(Button.class, matcher));
  }

  public ToggleButton getToggleButton(String name) throws ItemNotFoundException, ComponentAmbiguityException {
    return (ToggleButton) getComponent(finder, ToggleButton.class, name);
  }

  public ToggleButton getToggleButton() throws ItemNotFoundException, ComponentAmbiguityException {
    return (ToggleButton) getComponent(finder, ToggleButton.class, null);
  }

  public ToggleButton getToggleButton(ComponentMatcher matcher) throws ItemNotFoundException, ComponentAmbiguityException {
    return (ToggleButton) getComponent(finder, getMatcherByClass(ToggleButton.class, matcher));
  }

  public CheckBox getCheckBox(String name) throws ItemNotFoundException, ComponentAmbiguityException {
    return (CheckBox) getComponent(finder, CheckBox.class, name);
  }

  public CheckBox getCheckBox() throws ItemNotFoundException, ComponentAmbiguityException {
    return (CheckBox) getComponent(finder, CheckBox.class, null);
  }

  public CheckBox getCheckBox(ComponentMatcher matcher) throws ItemNotFoundException, ComponentAmbiguityException {
    return (CheckBox) getComponent(finder, getMatcherByClass(CheckBox.class, matcher));
  }

  public Panel getPanel() throws ItemNotFoundException, ComponentAmbiguityException {
    return (Panel) getComponent(finder, Panel.class, null);
  }

  public Panel getPanel(String name) throws ItemNotFoundException, ComponentAmbiguityException {
    return (Panel) getComponent(finder, Panel.class, name);
  }

  public Panel getPanel(ComponentMatcher matcher) throws ItemNotFoundException, ComponentAmbiguityException {
    return (Panel) getComponent(finder, getMatcherByClass(Panel.class, matcher));
  }

  public ProgressBar getProgressBar(String name) throws ItemNotFoundException, ComponentAmbiguityException {
    return (ProgressBar) getComponent(finder, ProgressBar.class, name);
  }

  public ProgressBar getProgressBar() throws ItemNotFoundException, ComponentAmbiguityException {
    return (ProgressBar) getComponent(finder, ProgressBar.class, null);
  }

  public ProgressBar getProgressBar(ComponentMatcher matcher) throws ItemNotFoundException, ComponentAmbiguityException {
    return (ProgressBar) getComponent(finder, getMatcherByClass(ProgressBar.class, matcher));
  }

  public Desktop getDesktop(String name) throws ItemNotFoundException, ComponentAmbiguityException {
    return (Desktop) getComponent(finder, Desktop.class, name);
  }

  public Desktop getDesktop() throws ItemNotFoundException, ComponentAmbiguityException {
    return (Desktop) getComponent(finder, Desktop.class, null);
  }

  public Desktop getDesktop(ComponentMatcher matcher) throws ItemNotFoundException, ComponentAmbiguityException {
    return (Desktop) getComponent(finder, getMatcherByClass(Desktop.class, matcher));
  }

  public TextBox getTextBox(String name) throws ItemNotFoundException, ComponentAmbiguityException {
    Class[] swingClasses = UIComponentAnalyzer.getSwingClasses(TextBox.class);
    String typeName = UIComponentAnalyzer.getTypeName(TextBox.class);
    Component swingComponent = finder.getComponent(name, swingClasses, typeName);
    return (TextBox) UIComponentFactory.createUIComponent(swingComponent);
  }

  public TextBox getTextBox() throws ItemNotFoundException, ComponentAmbiguityException {
    return (TextBox) getComponent(finder, TextBox.class, null);
  }

  public TextBox getTextBox(ComponentMatcher matcher) throws ItemNotFoundException, ComponentAmbiguityException {
    return (TextBox) getComponent(finder, getMatcherByClass(TextBox.class, matcher));
  }

  /**
   * Retrieves input-only text boxes. This is useful for avoiding ambiguity exceptions
   * when the input text boxes are laid out near labels, as in most forms.<p>
   * "Input text boxes" are defined as subclasses of the JTextComponent class - in other words,
   * JLabel components are excluded from the search. Please note that the is is not necessarily
   * visible from the user, since JTextComponent subclasses can be customized to look as ordinary,
   * read-only labels.
   */
  public TextBox getInputTextBox(String name) throws ComponentAmbiguityException, ItemNotFoundException {
    java.util.List inputComponentClasses = new ArrayList();
    Class[] swingClasses = TextBox.SWING_CLASSES;
    for (int i = 0; i < swingClasses.length; i++) {
      if (!swingClasses[i].equals(JLabel.class)) {
        inputComponentClasses.add(swingClasses[i]);
      }
    }
    Class[] inputClassesArray =
        (Class[]) inputComponentClasses.toArray(new Class[inputComponentClasses.size()]);
    return (TextBox) getComponent(finder, TextBox.class, inputClassesArray, name);
  }

  /**
   * Retrieves input-only text boxes.
   *
   * @see #getInputTextBox(String)
   */
  public TextBox getInputTextBox() throws ComponentAmbiguityException, ItemNotFoundException {
    return getInputTextBox(null);
  }

  public TabGroup getTabGroup(String name) throws ItemNotFoundException, ComponentAmbiguityException {
    return (TabGroup) getComponent(finder, TabGroup.class, name);
  }

  public TabGroup getTabGroup() throws ItemNotFoundException, ComponentAmbiguityException {
    return (TabGroup) getComponent(finder, TabGroup.class, null);
  }

  public TabGroup getTabGroup(ComponentMatcher matcher) throws ItemNotFoundException, ComponentAmbiguityException {
    return (TabGroup) getComponent(finder, getMatcherByClass(TabGroup.class, matcher));
  }

  public ComboBox getComboBox(String name) throws ItemNotFoundException, ComponentAmbiguityException {
    return (ComboBox) getComponent(finder, ComboBox.class, name);
  }

  public ComboBox getComboBox() throws ItemNotFoundException, ComponentAmbiguityException {
    return (ComboBox) getComponent(finder, ComboBox.class, null);
  }

  public ComboBox getComboBox(ComponentMatcher matcher) throws ItemNotFoundException, ComponentAmbiguityException {
    return (ComboBox) getComponent(finder, getMatcherByClass(ComboBox.class, matcher));
  }

  public Spinner getSpinner() throws ItemNotFoundException, ComponentAmbiguityException {
    return (Spinner) getComponent(finder, Spinner.class, null);
  }

  public Spinner getSpinner(String name) throws ItemNotFoundException, ComponentAmbiguityException {
    return (Spinner) getComponent(finder, Spinner.class, name);
  }

  public Spinner getSpinner(ComponentMatcher matcher) throws ItemNotFoundException, ComponentAmbiguityException {
    return (Spinner) getComponent(finder, getMatcherByClass(Spinner.class, matcher));
  }

  public DateSpinner getDateSpinner() throws ItemNotFoundException, ComponentAmbiguityException {
    Spinner component = (Spinner) getComponent(finder, Spinner.getSpinnerMatcherByModel(SpinnerDateModel.class));
    return new DateSpinner((JSpinner) component.getAwtComponent());
  }

  public DateSpinner getDateSpinner(String componentName) {
    ComponentMatcher matcher = ComponentMatchers.intersection(new ComponentMatcher[]{
        Spinner.getSpinnerMatcherByModel(SpinnerDateModel.class), getMatcherFromName(componentName)
    });
    Spinner component = (Spinner) getComponent(finder, matcher);
    return new DateSpinner((JSpinner) component.getAwtComponent());
  }

  public DateSpinner getDateSpinner(ComponentMatcher matcher) {
    ComponentMatcher intersection = ComponentMatchers.intersection(new ComponentMatcher[]{
        Spinner.getSpinnerMatcherByModel(SpinnerDateModel.class), matcher
    });
    Spinner component = (Spinner) getComponent(finder, intersection);
    return new DateSpinner((JSpinner) component.getAwtComponent());
  }

  public ListSpinner getListSpinner() throws ItemNotFoundException, ComponentAmbiguityException {
    Spinner component = (Spinner) getComponent(finder, Spinner.getSpinnerMatcherByModel(SpinnerListModel.class));
    return new ListSpinner((JSpinner) component.getAwtComponent());
  }

  public ListSpinner getListSpinner(String componentName) {
    ComponentMatcher matcher = ComponentMatchers.intersection(new ComponentMatcher[]{
        Spinner.getSpinnerMatcherByModel(SpinnerListModel.class), getMatcherFromName(componentName)
    });
    Spinner component = (Spinner) getComponent(finder, matcher);
    return new ListSpinner((JSpinner) component.getAwtComponent());
  }

  public ListSpinner getListSpinner(ComponentMatcher matcher) {
    ComponentMatcher intersection = ComponentMatchers.intersection(new ComponentMatcher[]{
        Spinner.getSpinnerMatcherByModel(SpinnerListModel.class), matcher
    });
    Spinner component = (Spinner) getComponent(finder, intersection);
    return new ListSpinner((JSpinner) component.getAwtComponent());
  }

  public NumberSpinner getNumberSpinner() throws ItemNotFoundException, ComponentAmbiguityException {
    Spinner component = (Spinner) getComponent(finder, Spinner.getSpinnerMatcherByModel(SpinnerNumberModel.class));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -