jdk/test/java/awt/regtesthelpers/Util.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1171 a2782dd9f312
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2006-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
package test.java.awt.regtesthelpers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * <p>This class contains utilities useful for regression testing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * <p>When using jtreg you would include this class into the build
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * list via something like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
     @library ../../../regtesthelpers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
     @build Util
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
     @run main YourTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
   </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Note that if you are about to create a test based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Applet-template, then put those lines into html-file, not in java-file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p> And put an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * import test.java.awt.regtesthelpers.Util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * into the java source of test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.Frame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.Dialog;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.Window;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.Button;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.awt.Robot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.awt.Toolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.awt.IllegalComponentStateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.awt.AWTException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.awt.AWTEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.awt.event.InputEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.awt.event.WindowAdapter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.awt.event.WindowEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.awt.event.ActionEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.awt.event.FocusEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import java.awt.event.WindowListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import java.awt.event.WindowFocusListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import java.awt.event.FocusListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import java.awt.event.ActionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import java.awt.peer.FramePeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import java.lang.reflect.Field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
import java.util.concurrent.atomic.AtomicBoolean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
public final class Util {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private Util() {} // this is a helper class with static methods :)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @throws RuntimeException when creation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public static Robot createRobot() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return new Robot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        } catch (AWTException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new RuntimeException("Error: unable to create robot", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static Frame createEmbeddedFrame(final Frame embedder)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
               InstantiationException, InvocationTargetException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        Toolkit tk = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        FramePeer frame_peer = (FramePeer) embedder.getPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        System.out.println("frame's peer = " + frame_peer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if ("sun.awt.windows.WToolkit".equals(tk.getClass().getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            Class comp_peer_class =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                Class.forName("sun.awt.windows.WComponentPeer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            System.out.println("comp peer class = " + comp_peer_class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            Field hwnd_field = comp_peer_class.getDeclaredField("hwnd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            hwnd_field.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            System.out.println("hwnd_field =" + hwnd_field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            long hwnd = hwnd_field.getLong(frame_peer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            System.out.println("hwnd = " + hwnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            Class clazz = Class.forName("sun.awt.windows.WEmbeddedFrame");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            Constructor constructor = clazz.getConstructor (new Class [] {Long.TYPE});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return (Frame) constructor.newInstance (new Object[] {hwnd});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } else if ("sun.awt.X11.XToolkit".equals(tk.getClass().getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            Class x_base_window_class = Class.forName("sun.awt.X11.XBaseWindow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            System.out.println("x_base_window_class = " + x_base_window_class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            Method get_window = x_base_window_class.getMethod("getWindow", new Class[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            System.out.println("get_window = " + get_window);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            long window = (Long) get_window.invoke(frame_peer, new Object[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            System.out.println("window = " + window);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            Class clazz = Class.forName("sun.awt.X11.XEmbeddedFrame");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            Constructor constructor = clazz.getConstructor (new Class [] {Long.TYPE, Boolean.TYPE});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return (Frame) constructor.newInstance (new Object[] {window, true});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        throw new RuntimeException("Unexpected toolkit - " + tk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Moves mouse pointer in the center of given {@code comp} component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * using {@code robot} parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public static void pointOnComp(final Component comp, final Robot robot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        Rectangle bounds = new Rectangle(comp.getLocationOnScreen(), comp.getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Moves mouse pointer in the center of a given {@code comp} component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * and performs a left mouse button click using the {@code robot} parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * with the {@code delay} delay between press and release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public static void clickOnComp(final Component comp, final Robot robot, int delay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        pointOnComp(comp, robot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        robot.delay(delay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        robot.mousePress(InputEvent.BUTTON1_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        robot.delay(delay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Moves mouse pointer in the center of a given {@code comp} component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * and performs a left mouse button click using the {@code robot} parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * with the default delay between press and release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public static void clickOnComp(final Component comp, final Robot robot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        clickOnComp(comp, robot, 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Clicks on a title of Frame/Dialog.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * WARNING: it may fail on some platforms when the window is not wide enough.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public static void clickOnTitle(final Window decoratedWindow, final Robot robot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        Point p = decoratedWindow.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        Dimension d = decoratedWindow.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (decoratedWindow instanceof Frame || decoratedWindow instanceof Dialog) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)decoratedWindow.getInsets().top/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            robot.delay(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            robot.mousePress(InputEvent.BUTTON1_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            robot.delay(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public static void waitForIdle(final Robot robot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // we do not use robot for now, use SunToolkit.realSync() instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        ((sun.awt.SunToolkit)Toolkit.getDefaultToolkit()).realSync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public static Field getField(final Class klass, final String fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return AccessController.doPrivileged(new PrivilegedAction<Field>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            public Field run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    Field field = klass.getDeclaredField(fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    assert (field != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    field.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    return field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    throw new RuntimeException("Error: unexpected exception caught!", se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                } catch (NoSuchFieldException nsfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    throw new RuntimeException("Error: unexpected exception caught!", nsfe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Waits for a notification and for a boolean condition to become true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * The method returns when the above conditions are fullfilled or when the timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param condition the object to be notified and the booelan condition to wait for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param timeout the maximum time to wait in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @param catchExceptions if {@code true} the method catches InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @return the final boolean value of the {@code condition}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @throws InterruptedException if the awaiting proccess has been interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public static boolean waitForConditionEx(final AtomicBoolean condition, long timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
      throws InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            synchronized (condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                long startTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                while (!condition.get()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    condition.wait(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    if (System.currentTimeMillis() - startTime >= timeout ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            return condition.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * The same as {@code waitForConditionEx(AtomicBoolean, long)} except that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * doesn't throw InterruptedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public static boolean waitForCondition(final AtomicBoolean condition, long timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return waitForConditionEx(condition, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            throw new RuntimeException("Error: unexpected exception caught!", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * The same as {@code waitForConditionEx(AtomicBoolean, long)} but without a timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public static void waitForConditionEx(final AtomicBoolean condition)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
      throws InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            synchronized (condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                while (!condition.get()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    condition.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * The same as {@code waitForConditionEx(AtomicBoolean)} except that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * doesn't throw InterruptedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public static void waitForCondition(final AtomicBoolean condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            waitForConditionEx(condition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            throw new RuntimeException("Error: unexpected exception caught!", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public static void waitTillShownEx(final Component comp) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                comp.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            } catch (IllegalComponentStateException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public static void waitTillShown(final Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            waitTillShownEx(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            throw new RuntimeException("Error: unexpected exception caught!", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Drags from one point to another with the specified mouse button pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param robot a robot to use for moving the mouse, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @param startPoint a start point of the drag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @param endPoint an end point of the drag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @param button one of {@code InputEvent.BUTTON1_MASK},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *     {@code InputEvent.BUTTON2_MASK}, {@code InputEvent.BUTTON3_MASK}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @throws IllegalArgumentException if {@code button} is not one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *     {@code InputEvent.BUTTON1_MASK}, {@code InputEvent.BUTTON2_MASK},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *     {@code InputEvent.BUTTON3_MASK}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public static void drag(Robot robot, Point startPoint, Point endPoint, int button) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (!(button == InputEvent.BUTTON1_MASK || button == InputEvent.BUTTON2_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                || button == InputEvent.BUTTON3_MASK))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            throw new IllegalArgumentException("invalid mouse button");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        robot.mouseMove(startPoint.x, startPoint.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        robot.mousePress(button);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            mouseMove(robot, startPoint, endPoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            robot.mouseRelease(button);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Moves the mouse pointer from one point to another.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Uses Bresenham's algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @param robot a robot to use for moving the mouse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @param startPoint a start point of the drag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param endPoint an end point of the drag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public static void mouseMove(Robot robot, Point startPoint, Point endPoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        int dx = endPoint.x - startPoint.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        int dy = endPoint.y - startPoint.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        int ax = Math.abs(dx) * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        int ay = Math.abs(dy) * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        int sx = signWOZero(dx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        int sy = signWOZero(dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        int x = startPoint.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        int y = startPoint.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        int d = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (ax > ay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            d = ay - ax/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            while (true){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                robot.mouseMove(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                robot.delay(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                if (x == endPoint.x){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                if (d >= 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    y = y + sy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    d = d - ax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                x = x + sx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                d = d + ay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            d = ax - ay/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            while (true){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                robot.mouseMove(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                robot.delay(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                if (y == endPoint.y){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                if (d >= 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    x = x + sx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    d = d - ay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                y = y + sy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                d = d + ax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    private static int signWOZero(int i){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return (i > 0)? 1: -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private static int sign(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return n < 0 ? -1 : n == 0 ? 0 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /** Returns {@code WindowListener} instance that diposes {@code Window} on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *  "window closing" event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @return    the {@code WindowListener} instance that could be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *            on a {@code Window}. After that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *            the {@code Window} is disposed when "window closed"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *            event is sent to the {@code Window}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    public static WindowListener getClosingWindowAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        return new WindowAdapter () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            public void windowClosing(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                e.getWindow().dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * The values directly map to the ones of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * sun.awt.X11.XWM & sun.awt.motif.MToolkit classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    public final static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        UNDETERMINED_WM = 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        NO_WM = 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        OTHER_WM = 3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        OPENLOOK_WM = 4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        MOTIF_WM = 5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        CDE_WM = 6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        ENLIGHTEN_WM = 7,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        KDE2_WM = 8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        SAWFISH_WM = 9,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        ICE_WM = 10,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        METACITY_WM = 11,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        COMPIZ_WM = 12,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        LG3D_WM = 13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * Returns -1 in case of not X Window or any problems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    public static int getWMID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        Class clazz = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            if ("sun.awt.X11.XToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                clazz = Class.forName("sun.awt.X11.XWM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            } else if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                clazz = Class.forName("sun.awt.motif.MToolkit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        } catch (ClassNotFoundException cnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            cnfe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if (clazz == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            final Class _clazz = clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            Method m_getWMID = (Method)AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                            Method method = _clazz.getDeclaredMethod("getWMID", new Class[] {});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                            if (method != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                                method.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                            return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                        } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            return ((Integer)m_getWMID.invoke(null, new Object[] {})).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        } catch (IllegalAccessException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            iae.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        } catch (InvocationTargetException ite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            ite.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    ////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    // Some stuff to test focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    ////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    private static WindowGainedFocusListener wgfListener = new WindowGainedFocusListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    private static FocusGainedListener fgListener = new FocusGainedListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    private static ActionPerformedListener apListener = new ActionPerformedListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    private abstract static class EventListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        AtomicBoolean notifier = new AtomicBoolean(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        Component comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        boolean printEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        public void listen(Component comp, boolean printEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            this.comp = comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            this.printEvent = printEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            notifier.set(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            setListener(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        public AtomicBoolean getNotifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            return notifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        abstract void setListener(Component comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        void printAndNotify(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            if (printEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                System.err.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            synchronized (notifier) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                notifier.set(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                notifier.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    private static class WindowGainedFocusListener extends EventListener implements WindowFocusListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        void setListener(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            ((Window)comp).addWindowFocusListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        public void windowGainedFocus(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            ((Window)comp).removeWindowFocusListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            printAndNotify(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        public void windowLostFocus(WindowEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    private static class FocusGainedListener extends EventListener implements FocusListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        void setListener(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            comp.addFocusListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            comp.removeFocusListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            printAndNotify(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        public void focusLost(FocusEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    private static class ActionPerformedListener extends EventListener implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        void setListener(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            ((Button)comp).addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            ((Button)comp).removeActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            printAndNotify(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    private static boolean trackEvent(int eventID, Component comp, Runnable action, int time, boolean printEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        EventListener listener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        switch (eventID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        case WindowEvent.WINDOW_GAINED_FOCUS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            listener = wgfListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        case FocusEvent.FOCUS_GAINED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            listener = fgListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        case ActionEvent.ACTION_PERFORMED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            listener = apListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        listener.listen(comp, printEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        action.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        return Util.waitForCondition(listener.getNotifier(), time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * Tracks WINDOW_GAINED_FOCUS event for a window caused by an action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * @param window the window to track the event for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @param action the action to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @param time the max time to wait for the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @param printEvent should the event received be printed or doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @return true if the event has been received, otherwise false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    public static boolean trackWindowGainedFocus(Window window, Runnable action, int time, boolean printEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        return trackEvent(WindowEvent.WINDOW_GAINED_FOCUS, window, action, time, printEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * Tracks FOCUS_GAINED event for a component caused by an action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @see #trackWindowGainedFocus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    public static boolean trackFocusGained(Component comp, Runnable action, int time, boolean printEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        return trackEvent(FocusEvent.FOCUS_GAINED, comp, action, time, printEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * Tracks ACTION_PERFORMED event for a button caused by an action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @see #trackWindowGainedFocus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    public static boolean trackActionPerformed(Button button, Runnable action, int time, boolean printEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        return trackEvent(ActionEvent.ACTION_PERFORMED, button, action, time, printEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
}