jdk/test/java/awt/regtesthelpers/Util.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 1171 a2782dd9f312
child 12535 6d2ee83614e8
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1171
diff changeset
     2
 * Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1171
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1171
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1171
diff changeset
    21
 * questions.
2
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
    /**
1171
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   127
     * Makes the window visible and waits until it's shown.
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   128
     */
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   129
    public static void showWindowWait(Window win) {
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   130
        win.setVisible(true);
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   131
        waitTillShown(win);
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   132
    }
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   133
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   134
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Moves mouse pointer in the center of given {@code comp} component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * using {@code robot} parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public static void pointOnComp(final Component comp, final Robot robot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        Rectangle bounds = new Rectangle(comp.getLocationOnScreen(), comp.getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Moves mouse pointer in the center of a given {@code comp} component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * and performs a left mouse button click using the {@code robot} parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * with the {@code delay} delay between press and release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public static void clickOnComp(final Component comp, final Robot robot, int delay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        pointOnComp(comp, robot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        robot.delay(delay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        robot.mousePress(InputEvent.BUTTON1_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        robot.delay(delay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Moves mouse pointer in the center of a given {@code comp} component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * and performs a left mouse button click using the {@code robot} parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * with the default delay between press and release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public static void clickOnComp(final Component comp, final Robot robot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        clickOnComp(comp, robot, 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Clicks on a title of Frame/Dialog.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * WARNING: it may fail on some platforms when the window is not wide enough.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public static void clickOnTitle(final Window decoratedWindow, final Robot robot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        Point p = decoratedWindow.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        Dimension d = decoratedWindow.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (decoratedWindow instanceof Frame || decoratedWindow instanceof Dialog) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)decoratedWindow.getInsets().top/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            robot.delay(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            robot.mousePress(InputEvent.BUTTON1_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            robot.delay(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public static void waitForIdle(final Robot robot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // we do not use robot for now, use SunToolkit.realSync() instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        ((sun.awt.SunToolkit)Toolkit.getDefaultToolkit()).realSync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public static Field getField(final Class klass, final String fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return AccessController.doPrivileged(new PrivilegedAction<Field>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            public Field run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    Field field = klass.getDeclaredField(fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    assert (field != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    field.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    return field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    throw new RuntimeException("Error: unexpected exception caught!", se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                } catch (NoSuchFieldException nsfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    throw new RuntimeException("Error: unexpected exception caught!", nsfe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Waits for a notification and for a boolean condition to become true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * The method returns when the above conditions are fullfilled or when the timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @param condition the object to be notified and the booelan condition to wait for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param timeout the maximum time to wait in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param catchExceptions if {@code true} the method catches InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @return the final boolean value of the {@code condition}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @throws InterruptedException if the awaiting proccess has been interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public static boolean waitForConditionEx(final AtomicBoolean condition, long timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
      throws InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            synchronized (condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                long startTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                while (!condition.get()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    condition.wait(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    if (System.currentTimeMillis() - startTime >= timeout ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return condition.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * The same as {@code waitForConditionEx(AtomicBoolean, long)} except that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * doesn't throw InterruptedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public static boolean waitForCondition(final AtomicBoolean condition, long timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return waitForConditionEx(condition, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw new RuntimeException("Error: unexpected exception caught!", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * The same as {@code waitForConditionEx(AtomicBoolean, long)} but without a timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public static void waitForConditionEx(final AtomicBoolean condition)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
      throws InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            synchronized (condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                while (!condition.get()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    condition.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * The same as {@code waitForConditionEx(AtomicBoolean)} except that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * doesn't throw InterruptedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public static void waitForCondition(final AtomicBoolean condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            waitForConditionEx(condition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            throw new RuntimeException("Error: unexpected exception caught!", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public static void waitTillShownEx(final Component comp) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                comp.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            } catch (IllegalComponentStateException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public static void waitTillShown(final Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            waitTillShownEx(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            throw new RuntimeException("Error: unexpected exception caught!", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Drags from one point to another with the specified mouse button pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param robot a robot to use for moving the mouse, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @param startPoint a start point of the drag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param endPoint an end point of the drag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param button one of {@code InputEvent.BUTTON1_MASK},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *     {@code InputEvent.BUTTON2_MASK}, {@code InputEvent.BUTTON3_MASK}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @throws IllegalArgumentException if {@code button} is not one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *     {@code InputEvent.BUTTON1_MASK}, {@code InputEvent.BUTTON2_MASK},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *     {@code InputEvent.BUTTON3_MASK}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public static void drag(Robot robot, Point startPoint, Point endPoint, int button) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (!(button == InputEvent.BUTTON1_MASK || button == InputEvent.BUTTON2_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                || button == InputEvent.BUTTON3_MASK))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            throw new IllegalArgumentException("invalid mouse 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
        robot.mouseMove(startPoint.x, startPoint.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        robot.mousePress(button);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            mouseMove(robot, startPoint, endPoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            robot.mouseRelease(button);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Moves the mouse pointer from one point to another.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Uses Bresenham's algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @param robot a robot to use for moving the mouse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @param startPoint a start point of the drag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @param endPoint an end point of the drag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public static void mouseMove(Robot robot, Point startPoint, Point endPoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        int dx = endPoint.x - startPoint.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        int dy = endPoint.y - startPoint.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        int ax = Math.abs(dx) * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        int ay = Math.abs(dy) * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        int sx = signWOZero(dx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        int sy = signWOZero(dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        int x = startPoint.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        int y = startPoint.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        int d = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (ax > ay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            d = ay - ax/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            while (true){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                robot.mouseMove(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                robot.delay(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                if (x == endPoint.x){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                if (d >= 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    y = y + sy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    d = d - ax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                x = x + sx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                d = d + ay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            d = ax - ay/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            while (true){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                robot.mouseMove(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                robot.delay(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                if (y == endPoint.y){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                if (d >= 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    x = x + sx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    d = d - ay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                y = y + sy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                d = d + ax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    private static int signWOZero(int i){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return (i > 0)? 1: -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    private static int sign(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        return n < 0 ? -1 : n == 0 ? 0 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /** Returns {@code WindowListener} instance that diposes {@code Window} on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *  "window closing" event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @return    the {@code WindowListener} instance that could be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *            on a {@code Window}. After that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *            the {@code Window} is disposed when "window closed"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *            event is sent to the {@code Window}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    public static WindowListener getClosingWindowAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return new WindowAdapter () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            public void windowClosing(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                e.getWindow().dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * The values directly map to the ones of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * sun.awt.X11.XWM & sun.awt.motif.MToolkit classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    public final static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        UNDETERMINED_WM = 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        NO_WM = 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        OTHER_WM = 3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        OPENLOOK_WM = 4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        MOTIF_WM = 5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        CDE_WM = 6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        ENLIGHTEN_WM = 7,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        KDE2_WM = 8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        SAWFISH_WM = 9,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        ICE_WM = 10,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        METACITY_WM = 11,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        COMPIZ_WM = 12,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        LG3D_WM = 13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * Returns -1 in case of not X Window or any problems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    public static int getWMID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        Class clazz = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            if ("sun.awt.X11.XToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                clazz = Class.forName("sun.awt.X11.XWM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            } else if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                clazz = Class.forName("sun.awt.motif.MToolkit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        } catch (ClassNotFoundException cnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            cnfe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if (clazz == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            final Class _clazz = clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            Method m_getWMID = (Method)AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                            Method method = _clazz.getDeclaredMethod("getWMID", new Class[] {});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                            if (method != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                                method.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                            return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                        } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            return ((Integer)m_getWMID.invoke(null, new Object[] {})).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        } catch (IllegalAccessException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            iae.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        } catch (InvocationTargetException ite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            ite.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    ////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    // Some stuff to test focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    ////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    private static WindowGainedFocusListener wgfListener = new WindowGainedFocusListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    private static FocusGainedListener fgListener = new FocusGainedListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    private static ActionPerformedListener apListener = new ActionPerformedListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    private abstract static class EventListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        AtomicBoolean notifier = new AtomicBoolean(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        Component comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        boolean printEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        public void listen(Component comp, boolean printEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            this.comp = comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            this.printEvent = printEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            notifier.set(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            setListener(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        public AtomicBoolean getNotifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            return notifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        abstract void setListener(Component comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        void printAndNotify(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            if (printEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                System.err.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            synchronized (notifier) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                notifier.set(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                notifier.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    private static class WindowGainedFocusListener extends EventListener implements WindowFocusListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        void setListener(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            ((Window)comp).addWindowFocusListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        public void windowGainedFocus(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            ((Window)comp).removeWindowFocusListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            printAndNotify(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        public void windowLostFocus(WindowEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    private static class FocusGainedListener extends EventListener implements FocusListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        void setListener(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            comp.addFocusListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            comp.removeFocusListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            printAndNotify(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        public void focusLost(FocusEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    private static class ActionPerformedListener extends EventListener implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        void setListener(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            ((Button)comp).addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            ((Button)comp).removeActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            printAndNotify(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    private static boolean trackEvent(int eventID, Component comp, Runnable action, int time, boolean printEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        EventListener listener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        switch (eventID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        case WindowEvent.WINDOW_GAINED_FOCUS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            listener = wgfListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        case FocusEvent.FOCUS_GAINED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            listener = fgListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        case ActionEvent.ACTION_PERFORMED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            listener = apListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        listener.listen(comp, printEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        action.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        return Util.waitForCondition(listener.getNotifier(), time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * Tracks WINDOW_GAINED_FOCUS event for a window caused by an action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @param window the window to track the event for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @param action the action to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @param time the max time to wait for the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @param printEvent should the event received be printed or doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @return true if the event has been received, otherwise false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    public static boolean trackWindowGainedFocus(Window window, Runnable action, int time, boolean printEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        return trackEvent(WindowEvent.WINDOW_GAINED_FOCUS, window, 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 FOCUS_GAINED event for a component 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 trackFocusGained(Component comp, Runnable action, int time, boolean printEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        return trackEvent(FocusEvent.FOCUS_GAINED, comp, action, time, printEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * Tracks ACTION_PERFORMED event for a button caused by an action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @see #trackWindowGainedFocus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    public static boolean trackActionPerformed(Button button, Runnable action, int time, boolean printEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        return trackEvent(ActionEvent.ACTION_PERFORMED, button, action, time, printEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    }
1171
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   585
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   586
    /*
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   587
     * Requests focus on the component provided and waits for the result.
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   588
     * @return true if the component has been focused, false otherwise.
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   589
     */
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   590
    public static boolean focusComponent(Component comp, int time) {
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   591
        return focusComponent(comp, time, false);
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   592
    }
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   593
    public static boolean focusComponent(final Component comp, int time, boolean printEvent) {
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   594
        return trackFocusGained(comp,
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   595
                                new Runnable() {
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   596
                                    public void run() {
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   597
                                        comp.requestFocus();
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   598
                                    }
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   599
                                },
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   600
                                time, printEvent);
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   601
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   602
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
}