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

📄 window.java

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

import junit.framework.Assert;
import junit.framework.AssertionFailedError;
import org.uispec4j.assertion.Assertion;
import org.uispec4j.assertion.UISpecAssert;
import org.uispec4j.xml.XmlWriter;

import javax.swing.*;
import java.awt.*;

/**
 * Wrapper for window components such as JFrame, JDialog, JInternalFrame.
 */
public class Window extends Panel {

  public static final String TYPE_NAME = "Window";
  public static final Class[] SWING_CLASSES = {JFrame.class,
                                               JDialog.class,
                                               JInternalFrame.class,
                                               Frame.class,
                                               java.awt.Window.class};
  private final Adapter adapter;

  public Window(JFrame frame) {
    super(frame);
    this.adapter = new JFrameAdapter(frame);
  }

  public Window(JDialog dialog) {
    super(dialog);
    this.adapter = new JDialogAdapter(dialog);
  }

  public Window(JInternalFrame frame) {
    super(frame);
    this.adapter = new JInternalFrameAdapter(frame);
  }

  public Window(Frame frame) {
    super(frame);
    this.adapter = new FrameAdapter(frame);
  }

  public Window(java.awt.Window window) {
    super(window);
    this.adapter = new WindowAdapter(window);
  }

  public String getDescriptionTypeName() {
    return "window";
  }

  protected void addAttributes(Component component, XmlWriter.Tag tag) {
    tag.addAttribute("title", adapter.getTitle());
  }

  public MenuBar getMenuBar() {
    return new MenuBar(adapter.getJMenuBar());
  }

  public String getTitle() {
    return adapter.getTitle();
  }

  public Assertion titleEquals(final String expected) {
    return new Assertion() {
      public void check() {
        Assert.assertEquals(expected, getTitle());
      }
    };
  }

  public void assertTitleEquals(String expected) {
    UISpecAssert.assertTrue(titleEquals(expected));
  }

  protected void getSubDescription(Container container, XmlWriter.Tag tag) {
    Container internalAwtContainer = adapter.getInternalAwtContainer();
    Panel contentPane = new Panel(internalAwtContainer);
    contentPane.getSubDescription(internalAwtContainer, tag);
  }

  public Container getInternalAwtContainer() {
    return adapter.getInternalAwtContainer();
  }

  public Assertion isModal() {
    return new Assertion() {
      public void check() {
        Assert.assertTrue(adapter.isModal());
      }
    };
  }

  static interface Adapter {
    JMenuBar getJMenuBar();

    String getTitle();

    boolean isModal();

    Container getInternalAwtContainer();
  }

  private static class JInternalFrameAdapter implements Adapter {
    private final JInternalFrame frame;

    JInternalFrameAdapter(JInternalFrame frame) {
      this.frame = frame;
    }

    public JMenuBar getJMenuBar() {
      return frame.getJMenuBar();
    }

    public String getTitle() {
      return frame.getTitle();
    }

    public boolean isModal() {
      return false;
    }

    public Container getInternalAwtContainer() {
      return frame.getContentPane();
    }
  }

  private static class JFrameAdapter implements Adapter {
    private final JFrame frame;

    JFrameAdapter(JFrame frame) {
      this.frame = frame;
    }

    public JMenuBar getJMenuBar() {
      return frame.getJMenuBar();
    }

    public String getTitle() {
      return frame.getTitle();
    }

    public boolean isModal() {
      return false;
    }

    public Container getInternalAwtContainer() {
      return frame.getContentPane();
    }
  }

  private static class JDialogAdapter implements Adapter {
    private final JDialog dialog;

    JDialogAdapter(JDialog dialog) {
      this.dialog = dialog;
    }

    public JMenuBar getJMenuBar() {
      return dialog.getJMenuBar();
    }

    public String getTitle() {
      return dialog.getTitle();
    }

    public boolean isModal() {
      return dialog.isModal();
    }

    public Container getInternalAwtContainer() {
      return dialog.getContentPane();
    }
  }

  private static class FrameAdapter implements Adapter {
    Frame frame;

    public FrameAdapter(Frame frame) {
      this.frame = frame;
    }

    public JMenuBar getJMenuBar() {
      throw new AssertionFailedError("This component has no menu bar");
    }

    public String getTitle() {
      return frame.getTitle();
    }

    public boolean isModal() {
      return false;
    }

    public Container getInternalAwtContainer() {
      return frame;
    }
  }

  private static class WindowAdapter implements Adapter {

    private java.awt.Window window;

    public WindowAdapter(java.awt.Window window) {
      this.window = window;
    }

    public JMenuBar getJMenuBar() {
      throw new AssertionFailedError("This component has no menu bar");
    }

    public String getTitle() {
      return "";
    }

    public boolean isModal() {
      return false;
    }

    public Container getInternalAwtContainer() {
      return window;
    }
  }
}

⌨️ 快捷键说明

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