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

📄 componentfinder.java

📁 基于Junit的 功能和单元测试的的测试工具。只支持Swing.
💻 JAVA
字号:
package org.uispec4j.finder;

import org.uispec4j.ComponentAmbiguityException;
import org.uispec4j.ItemNotFoundException;
import org.uispec4j.UIComponent;
import org.uispec4j.utils.UIComponentFactory;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * Generic utility for retrieving AWT/Swing components in a container.
 */
public class ComponentFinder {
  private Container container;

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

  public Component getComponent(ComponentMatcher matcher) throws ComponentAmbiguityException, ItemNotFoundException {
    return getComponent(new ComponentMatcher[]{matcher}, null, null, new Class[0]);
  }

  public Component getComponent(String name, Class[] swingClasses, String componentType) throws ComponentAmbiguityException, ItemNotFoundException {
    try {
      return getComponent(getMatchers(name), componentType, name, swingClasses);
    }
    catch (ItemNotFoundException e) {
      Component[] components = findComponents(ComponentMatcher.ALL, swingClasses);
      List names = new ArrayList();
      for (int i = 0; i < components.length; i++) {
        Component component = components[i];
        UIComponent uiComponent = UIComponentFactory.createUIComponent(component);
        String componentLabel = uiComponent.getLabel();
        String componentName = uiComponent.getName();
        if (componentLabel != null) {
          names.add(componentLabel);
        }
        else if (componentName != null) {
          names.add(componentName);
        }
      }
      Collections.sort(names);
      String message =
        Messages.computeNotFoundMessage(componentType, name, names);
      throw new ItemNotFoundException(message);
    }
  }

  public Component findComponent(ComponentMatcher matcher) throws ComponentAmbiguityException {
    return findComponent(new ComponentMatcher[]{matcher}, null, null, new Class[0]);
  }

  public Component findComponent(String name, Class[] swingClasses, String componentType) throws ComponentAmbiguityException {
    return findComponent(getMatchers(name), componentType, name, swingClasses);
  }

  public Component[] getComponents(ComponentMatcher matcher) {
    return findComponents(matcher, new Class[0]);
  }

  public Component[] getComponents(String name, Class[] swingClasses) {
    return getComponents(getMatchers(name), swingClasses);
  }

  private Component findComponent(ComponentMatcher[] matchers, String type, String name, Class[] swingClasses) throws ComponentAmbiguityException {
    Component[] foundComponents = getComponents(matchers, swingClasses);
    if (foundComponents.length > 1) {
      throw new ComponentAmbiguityException(Messages.computeAmbiguityMessage(foundComponents, type, name));
    }
    return (foundComponents.length == 0) ? null : foundComponents[0];
  }

  private Component getComponent(ComponentMatcher[] matchers, String type, String name, Class[] swingClasses) throws ItemNotFoundException, ComponentAmbiguityException {
    Component foundComponent = findComponent(matchers, type, name, swingClasses);
    if (foundComponent == null) {
      throw new ItemNotFoundException(Messages.computeNotFoundMessage(type, name, null));
    }
    return foundComponent;
  }

  private Component[] getComponents(ComponentMatcher[] matchers, Class[] swingClasses) {
    for (int i = 0; i < matchers.length; i++) {
      Component[] foundComponents = findComponents(matchers[i], swingClasses);
      if (foundComponents.length > 0) {
        return foundComponents;
      }
    }
    return new Component[0];
  }

  private Component[] findComponents(ComponentMatcher matcher, Class[] swingClasses) {
    List foundComponents = new ArrayList();
    retrieveComponents(container, foundComponents, matcher, swingClasses);
    return (Component[])foundComponents.toArray(new Component[foundComponents.size()]);
  }

  private static void retrieveComponents(Container container, List components, ComponentMatcher matcher, Class[] swingClasses) {
    if (container instanceof JScrollPane) {
      JScrollPane scroll = (JScrollPane)container;
      retrieveComponents(scroll.getViewport(), components, matcher, swingClasses);
      return;
    }
    if (container == null) {
      return;
    }
    List containers = new ArrayList();
    boolean isCardLayout = (container.getLayout() instanceof CardLayout);
    for (int i = 0, max = container.getComponentCount(); i < max; i++) {
      Component component = container.getComponent(i);
      if (isCardLayout && !component.isVisible()) {
        continue;
      }
      if (isClassMatching(component, swingClasses) && matcher.matches(component)) {
        components.add(component);
      }
      if (component instanceof Container) {
        containers.add(component);
      }
    }
    for (Iterator iterator = containers.iterator(); iterator.hasNext();) {
      retrieveComponents((Container)iterator.next(), components, matcher, swingClasses);
    }
  }

  private static boolean isClassMatching(Component component, Class[] swingClasses) {
    if (swingClasses.length == 0) {
      return true;
    }
    for (int i = 0; i < swingClasses.length; i++) {
      Class expectedClass = swingClasses[i];
      if (expectedClass.isAssignableFrom(component.getClass())) {
        return true;
      }
    }
    return false;
  }

  private static ComponentMatcher[] getMatchers(String name) {
    if (name == null) {
      return new ComponentMatcher[]{ComponentMatcher.ALL};
    }
    else {
      return getPredefinedNameMatchers(name);
    }
  }

  private static ComponentMatcher[] getPredefinedNameMatchers(String name) {
    return new ComponentMatcher[]{
      ComponentMatchers.displayedNameIdentity(name),
      ComponentMatchers.displayedNameSubstring(name),
      ComponentMatchers.innerNameIdentity(name),
      ComponentMatchers.innerNameSubstring(name)
    };
  }
}

⌨️ 快捷键说明

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