001: package org.uispec4j;
002:
003: import junit.framework.AssertionFailedError;
004: import org.uispec4j.assertion.Assertion;
005: import org.uispec4j.assertion.UISpecAssert;
006: import org.uispec4j.utils.Utils;
007:
008: import javax.swing.*;
009: import java.awt.*;
010:
011: /**
012: * Wrapper for Multiple Desktop Interface (MDI) widgets implemented as JDesktopPane components.
013: */
014: public class Desktop extends AbstractUIComponent {
015: public static final String TYPE_NAME = "desktop";
016: public static final Class[] SWING_CLASSES = { JDesktopPane.class };
017:
018: private final JDesktopPane jDesktopPane;
019:
020: public Desktop(JDesktopPane jDesktopPane) {
021: this .jDesktopPane = jDesktopPane;
022: }
023:
024: public Component getAwtComponent() {
025: return jDesktopPane;
026: }
027:
028: public String getDescriptionTypeName() {
029: return "desktop";
030: }
031:
032: /**
033: * Returs all the internal windows contained in the desktop.
034: */
035: public Window[] getWindows() {
036: JInternalFrame[] allFrames = jDesktopPane.getAllFrames();
037: Window[] result = new Window[allFrames.length];
038: for (int i = 0; i < allFrames.length; i++) {
039: result[i] = new Window(allFrames[i]);
040: }
041: return result;
042: }
043:
044: public Assertion containsWindow(final String title) {
045: return new Assertion() {
046: public void check() {
047: JInternalFrame[] allFrames = jDesktopPane
048: .getAllFrames();
049: for (int i = 0; i < allFrames.length; i++) {
050: if (Utils.equals(title, allFrames[i].getTitle())) {
051: return;
052: }
053: }
054: throw new AssertionFailedError("No window with title '"
055: + title + "' found");
056: }
057: };
058: }
059:
060: /**
061: * Returns a window given its title, and waits if it is not available yet.
062: * This method will fail after a given timeout if the window is not found.
063: *
064: * @see UISpec4J#setWindowInterceptionTimeLimit(long)
065: */
066: public Window getWindow(String title)
067: throws ComponentAmbiguityException {
068: InternalFrameIntercepted assertion = new InternalFrameIntercepted(
069: title);
070: UISpecAssert.waitUntil(assertion, UISpec4J
071: .getWindowInterceptionTimeLimit());
072: return assertion.getResult();
073: }
074:
075: private class InternalFrameIntercepted extends Assertion {
076:
077: private String windowTitle;
078: private Window result;
079:
080: public InternalFrameIntercepted(String windowTitle) {
081: this .windowTitle = windowTitle;
082: }
083:
084: public void check() throws Exception {
085: Window[] windows = getWindows();
086: for (int i = 0; i < windows.length; i++) {
087: Window window = windows[i];
088: if (windowTitle.equals(window.getTitle())) {
089: if (result != null) {
090: throw new ComponentAmbiguityException(
091: "There are several windows with title '"
092: + windowTitle + "'");
093: }
094: result = window;
095: }
096: }
097: if (result == null) {
098: throw new ItemNotFoundException("Window '"
099: + windowTitle + "' not found");
100: }
101: }
102:
103: public Window getResult() {
104: return result;
105: }
106: }
107: }
|