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

📄 uispecdisplay.java

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

import junit.framework.AssertionFailedError;
import org.uispec4j.Window;
import org.uispec4j.interception.handlers.InterceptionHandler;
import org.uispec4j.utils.ComponentUtils;
import org.uispec4j.utils.ExceptionContainer;
import org.uispec4j.utils.Utils;

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

/**
 * Virtual display used by the interception mechanism.
 *
 * @see UISpecToolkit
 */
public class UISpecDisplay {

  private static final UISpecDisplay singletonInstance = new UISpecDisplay();
  private Stack handlerStack = new Stack();
  private JPopupMenu currentPopup;
  private ExceptionContainer exceptionContainer = new ExceptionContainer();
  private List threads = new ArrayList();

  public static UISpecDisplay instance() {
    return singletonInstance;
  }

  public void showFrame(JFrame frame) {
    processWindow(new Window(frame));
  }

  public void showFrame(Frame frame) {
    if (frame instanceof JFrame) {
      showFrame((JFrame)frame);
    }
    else {
      processWindow(new Window(frame));
    }
  }

  public void showDialog(JDialog dialog) {
    processWindow(new Window(dialog));
  }

  public void showWindow(java.awt.Window window) {
    processWindow(new Window(window));
  }

  public void add(InterceptionHandler handler) {
    synchronized (handlerStack) {
      handlerStack.push(handler);
    }
  }

  public void reset() {
    synchronized (handlerStack) {
      handlerStack.clear();
    }
    exceptionContainer.reset();
    for (Iterator iterator = threads.iterator(); iterator.hasNext();) {
      Thread thread = (Thread)iterator.next();
      try {
        thread.join(10);
      }
      catch (InterruptedException e) {
        throw new RuntimeException(e);
      }
      if (thread.isAlive()) {
        thread.interrupt();
      }
    }
    threads.clear();
  }

  public void remove(InterceptionHandler handler) {
    synchronized (handlerStack) {
      handlerStack.remove(handler);
    }
  }

  public void rethrowIfNeeded() {
    exceptionContainer.rethrowIfNeeded();
  }

  private UISpecDisplay() {
    // Constructor is private since this is a singleton class
  }

  private void processWindow(Window window) {
    try {
      InterceptionHandler handler;
      synchronized (handlerStack) {
        if (!assertAcceptsWindow(window)) {
          return;
        }
        handler = (InterceptionHandler)handlerStack.pop();
      }
      handler.process(window);
    }
    catch (Throwable e) {
      ComponentUtils.close(window);
      store(e);
    }
  }

  public boolean assertAcceptsWindow(Window window) {
    if (!handlerStack.isEmpty()) {
      return true;
    }

    AssertionFailedError error =
      new AssertionFailedError("Unexpected window shown - this window should be handled with " +
                               "WindowInterceptor. " + Utils.LINE_SEPARATOR +
                               "Window contents:" + Utils.LINE_SEPARATOR +
                               window.getDescription());
    if (interceptionInProgress()) {
      store(error);
      ComponentUtils.close(window);
    }
    else {
      throw error;
    }
    return false;
  }

  private boolean interceptionInProgress() {
    return !handlerStack.isEmpty();
  }

  public void setCurrentPopup(JPopupMenu popupMenu) {
    this.currentPopup = popupMenu;
  }

  public JPopupMenu getCurrentPopup() {
    return currentPopup;
  }

  public void store(Throwable throwable) {
    exceptionContainer.set(throwable);
  }

  public int getHandlerCount() {
    return handlerStack.size();
  }

  public void runInNewThread(Runnable runnable) {
    Thread thread = new Thread(runnable);
    threads.add(thread);
    thread.start();
  }
}

⌨️ 快捷键说明

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