jdk/test/java/awt/regtesthelpers/Util.java
author prr
Thu, 27 Feb 2014 10:43:53 -0800
changeset 23328 4c53a6ebc779
parent 23274 3b6993da89df
parent 23010 6dadb192ad81
child 28087 622b2f420bc3
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23274
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
     2
 * Copyright (c) 2006, 2014, 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;
23274
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
    54
import java.awt.Color;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.awt.event.InputEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.awt.event.WindowAdapter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.awt.event.WindowEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.awt.event.ActionEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import java.awt.event.FocusEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import java.awt.event.WindowListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import java.awt.event.WindowFocusListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import java.awt.event.FocusListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import java.awt.event.ActionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import java.awt.peer.FramePeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import java.lang.reflect.Field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
import java.util.concurrent.atomic.AtomicBoolean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
public final class Util {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private Util() {} // this is a helper class with static methods :)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @throws RuntimeException when creation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public static Robot createRobot() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return new Robot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        } catch (AWTException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throw new RuntimeException("Error: unable to create robot", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public static Frame createEmbeddedFrame(final Frame embedder)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
               InstantiationException, InvocationTargetException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        Toolkit tk = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        FramePeer frame_peer = (FramePeer) embedder.getPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        System.out.println("frame's peer = " + frame_peer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if ("sun.awt.windows.WToolkit".equals(tk.getClass().getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            Class comp_peer_class =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                Class.forName("sun.awt.windows.WComponentPeer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            System.out.println("comp peer class = " + comp_peer_class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            Field hwnd_field = comp_peer_class.getDeclaredField("hwnd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            hwnd_field.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            System.out.println("hwnd_field =" + hwnd_field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            long hwnd = hwnd_field.getLong(frame_peer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            System.out.println("hwnd = " + hwnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            Class clazz = Class.forName("sun.awt.windows.WEmbeddedFrame");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            Constructor constructor = clazz.getConstructor (new Class [] {Long.TYPE});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            return (Frame) constructor.newInstance (new Object[] {hwnd});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        } else if ("sun.awt.X11.XToolkit".equals(tk.getClass().getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            Class x_base_window_class = Class.forName("sun.awt.X11.XBaseWindow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            System.out.println("x_base_window_class = " + x_base_window_class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            Method get_window = x_base_window_class.getMethod("getWindow", new Class[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            System.out.println("get_window = " + get_window);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            long window = (Long) get_window.invoke(frame_peer, new Object[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            System.out.println("window = " + window);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            Class clazz = Class.forName("sun.awt.X11.XEmbeddedFrame");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            Constructor constructor = clazz.getConstructor (new Class [] {Long.TYPE, Boolean.TYPE});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return (Frame) constructor.newInstance (new Object[] {window, true});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        throw new RuntimeException("Unexpected toolkit - " + tk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
1171
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   128
     * 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
   129
     */
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   130
    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
   131
        win.setVisible(true);
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   132
        waitTillShown(win);
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
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   135
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Moves mouse pointer in the center of given {@code comp} component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * using {@code robot} parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public static void pointOnComp(final Component comp, final Robot robot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        Rectangle bounds = new Rectangle(comp.getLocationOnScreen(), comp.getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Moves mouse pointer in the center of a given {@code comp} component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * and performs a left mouse button click using the {@code robot} parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * with the {@code delay} delay between press and release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public static void clickOnComp(final Component comp, final Robot robot, int delay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        pointOnComp(comp, robot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        robot.delay(delay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        robot.mousePress(InputEvent.BUTTON1_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        robot.delay(delay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
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
     * Moves mouse pointer in the center of a given {@code comp} component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * and performs a left mouse button click using the {@code robot} parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * with the default delay between press and release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public static void clickOnComp(final Component comp, final Robot robot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        clickOnComp(comp, robot, 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
12829
506020af237a 7043963: AWT workaround missing for Mutter.
omajid
parents: 12535
diff changeset
   166
    public static Point getTitlePoint(Window decoratedWindow) {
506020af237a 7043963: AWT workaround missing for Mutter.
omajid
parents: 12535
diff changeset
   167
        Point p = decoratedWindow.getLocationOnScreen();
506020af237a 7043963: AWT workaround missing for Mutter.
omajid
parents: 12535
diff changeset
   168
        Dimension d = decoratedWindow.getSize();
506020af237a 7043963: AWT workaround missing for Mutter.
omajid
parents: 12535
diff changeset
   169
        return new Point(p.x + (int)(d.getWidth()/2),
506020af237a 7043963: AWT workaround missing for Mutter.
omajid
parents: 12535
diff changeset
   170
                         p.y + (int)(decoratedWindow.getInsets().top/2));
506020af237a 7043963: AWT workaround missing for Mutter.
omajid
parents: 12535
diff changeset
   171
    }
506020af237a 7043963: AWT workaround missing for Mutter.
omajid
parents: 12535
diff changeset
   172
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Clicks on a title of Frame/Dialog.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * WARNING: it may fail on some platforms when the window is not wide enough.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public static void clickOnTitle(final Window decoratedWindow, final Robot robot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (decoratedWindow instanceof Frame || decoratedWindow instanceof Dialog) {
12829
506020af237a 7043963: AWT workaround missing for Mutter.
omajid
parents: 12535
diff changeset
   179
            Point p = getTitlePoint(decoratedWindow);
506020af237a 7043963: AWT workaround missing for Mutter.
omajid
parents: 12535
diff changeset
   180
            robot.mouseMove(p.x, p.y);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            robot.delay(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            robot.mousePress(InputEvent.BUTTON1_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            robot.delay(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
23274
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   188
    /**
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   189
     * Tests whether screen pixel has the expected color performing several
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   190
     * attempts. This method is useful for asynchronous window manager where
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   191
     * it's impossible to determine when drawing actually takes place.
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   192
     *
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   193
     * @param x X position of pixel
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   194
     * @param y Y position of pixel
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   195
     * @param color expected color
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   196
     * @param attempts number of attempts to undertake
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   197
     * @param delay delay before each attempt
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   198
     * @param robot a robot to use for retrieving pixel color
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   199
     * @return true if pixel color matches the color expected, otherwise false
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   200
     */
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   201
    public static boolean testPixelColor(int x, int y, final Color color, int attempts, int delay, final Robot robot) {
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   202
        while (attempts-- > 0) {
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   203
            robot.delay(delay);
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   204
            Color screen = robot.getPixelColor(x, y);
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   205
            if (screen.equals(color)) {
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   206
                return true;
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   207
            }
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   208
        }
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   209
        return false;
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   210
    }
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   211
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   212
    /**
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   213
     * Tests whether the area within boundaries has the expected color
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   214
     * performing several attempts. This method is useful for asynchronous
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   215
     * window manager where it's impossible to determine when drawing actually
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   216
     * takes place.
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   217
     *
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   218
     * @param bounds position of area
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   219
     * @param color expected color
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   220
     * @param attempts number of attempts to undertake
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   221
     * @param delay delay before each attempt
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   222
     * @param robot a robot to use for retrieving pixel color
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   223
     * @return true if area color matches the color expected, otherwise false
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   224
     */
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   225
    public static boolean testBoundsColor(final Rectangle bounds, final Color color, int attempts, int delay, final Robot robot) {
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   226
        int right = bounds.x + bounds.width - 1;
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   227
        int bottom = bounds.y + bounds.height - 1;
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   228
        while (attempts-- > 0) {
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   229
            if (testPixelColor(bounds.x, bounds.y, color, 1, delay, robot)
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   230
                && testPixelColor(right, bounds.y, color, 1, 0, robot)
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   231
                && testPixelColor(right, bottom, color, 1, 0, robot)
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   232
                && testPixelColor(bounds.x, bottom, color, 1, 0, robot)) {
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   233
                return true;
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   234
            }
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   235
        }
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   236
        return false;
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   237
    }
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 15638
diff changeset
   238
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public static void waitForIdle(final Robot robot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        // we do not use robot for now, use SunToolkit.realSync() instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        ((sun.awt.SunToolkit)Toolkit.getDefaultToolkit()).realSync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public static Field getField(final Class klass, final String fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return AccessController.doPrivileged(new PrivilegedAction<Field>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            public Field run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    Field field = klass.getDeclaredField(fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    assert (field != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    field.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    return field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    throw new RuntimeException("Error: unexpected exception caught!", se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                } catch (NoSuchFieldException nsfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    throw new RuntimeException("Error: unexpected exception caught!", nsfe);
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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Waits for a notification and for a boolean condition to become true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * The method returns when the above conditions are fullfilled or when the timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param condition the object to be notified and the booelan condition to wait for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param timeout the maximum time to wait in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param catchExceptions if {@code true} the method catches InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @return the final boolean value of the {@code condition}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @throws InterruptedException if the awaiting proccess has been interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public static boolean waitForConditionEx(final AtomicBoolean condition, long timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
      throws InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            synchronized (condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                long startTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                while (!condition.get()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    condition.wait(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    if (System.currentTimeMillis() - startTime >= timeout ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                        break;
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
            return condition.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * The same as {@code waitForConditionEx(AtomicBoolean, long)} except that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * doesn't throw InterruptedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public static boolean waitForCondition(final AtomicBoolean condition, long timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            return waitForConditionEx(condition, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            throw new RuntimeException("Error: unexpected exception caught!", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * The same as {@code waitForConditionEx(AtomicBoolean, long)} but without a timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public static void waitForConditionEx(final AtomicBoolean condition)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
      throws InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            synchronized (condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                while (!condition.get()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    condition.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
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
     * The same as {@code waitForConditionEx(AtomicBoolean)} except that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * doesn't throw InterruptedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public static void waitForCondition(final AtomicBoolean condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            waitForConditionEx(condition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            throw new RuntimeException("Error: unexpected exception caught!", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public static void waitTillShownEx(final Component comp) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                comp.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            } catch (IllegalComponentStateException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public static void waitTillShown(final Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            waitTillShownEx(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            throw new RuntimeException("Error: unexpected exception caught!", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Drags from one point to another with the specified mouse button pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @param robot a robot to use for moving the mouse, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param startPoint a start point of the drag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @param endPoint an end point of the drag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @param button one of {@code InputEvent.BUTTON1_MASK},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *     {@code InputEvent.BUTTON2_MASK}, {@code InputEvent.BUTTON3_MASK}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @throws IllegalArgumentException if {@code button} is not one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *     {@code InputEvent.BUTTON1_MASK}, {@code InputEvent.BUTTON2_MASK},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *     {@code InputEvent.BUTTON3_MASK}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public static void drag(Robot robot, Point startPoint, Point endPoint, int button) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (!(button == InputEvent.BUTTON1_MASK || button == InputEvent.BUTTON2_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                || button == InputEvent.BUTTON3_MASK))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            throw new IllegalArgumentException("invalid mouse button");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        robot.mouseMove(startPoint.x, startPoint.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        robot.mousePress(button);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            mouseMove(robot, startPoint, endPoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            robot.mouseRelease(button);
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
     * Moves the mouse pointer from one point to another.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Uses Bresenham's algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @param robot a robot to use for moving the mouse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @param startPoint a start point of the drag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @param endPoint an end point of the drag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    public static void mouseMove(Robot robot, Point startPoint, Point endPoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        int dx = endPoint.x - startPoint.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        int dy = endPoint.y - startPoint.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        int ax = Math.abs(dx) * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        int ay = Math.abs(dy) * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        int sx = signWOZero(dx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        int sy = signWOZero(dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        int x = startPoint.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        int y = startPoint.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        int d = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (ax > ay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            d = ay - ax/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            while (true){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                robot.mouseMove(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                robot.delay(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                if (x == endPoint.x){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                if (d >= 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    y = y + sy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    d = d - ax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                x = x + sx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                d = d + ay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            d = ax - ay/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            while (true){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                robot.mouseMove(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                robot.delay(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                if (y == endPoint.y){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                if (d >= 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    x = x + sx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    d = d - ay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                y = y + sy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                d = d + ax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    private static int signWOZero(int i){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        return (i > 0)? 1: -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
    private static int sign(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        return n < 0 ? -1 : n == 0 ? 0 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    /** Returns {@code WindowListener} instance that diposes {@code Window} on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *  "window closing" event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @return    the {@code WindowListener} instance that could be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *            on a {@code Window}. After that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *            the {@code Window} is disposed when "window closed"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *            event is sent to the {@code Window}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    public static WindowListener getClosingWindowAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        return new WindowAdapter () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            public void windowClosing(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                e.getWindow().dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            }
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
     * The values directly map to the ones of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * sun.awt.X11.XWM & sun.awt.motif.MToolkit classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    public final static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        UNDETERMINED_WM = 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        NO_WM = 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        OTHER_WM = 3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        OPENLOOK_WM = 4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        MOTIF_WM = 5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        CDE_WM = 6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        ENLIGHTEN_WM = 7,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        KDE2_WM = 8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        SAWFISH_WM = 9,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        ICE_WM = 10,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        METACITY_WM = 11,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        COMPIZ_WM = 12,
12829
506020af237a 7043963: AWT workaround missing for Mutter.
omajid
parents: 12535
diff changeset
   469
        LG3D_WM = 13,
506020af237a 7043963: AWT workaround missing for Mutter.
omajid
parents: 12535
diff changeset
   470
        CWM_WM = 14,
506020af237a 7043963: AWT workaround missing for Mutter.
omajid
parents: 12535
diff changeset
   471
        MUTTER_WM = 15;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Returns -1 in case of not X Window or any problems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    public static int getWMID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        Class clazz = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            if ("sun.awt.X11.XToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                clazz = Class.forName("sun.awt.X11.XWM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            } else if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                clazz = Class.forName("sun.awt.motif.MToolkit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        } catch (ClassNotFoundException cnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            cnfe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        if (clazz == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            final Class _clazz = clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            Method m_getWMID = (Method)AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                            Method method = _clazz.getDeclaredMethod("getWMID", new Class[] {});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                            if (method != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                                method.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                            return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                        } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                        } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            return ((Integer)m_getWMID.invoke(null, new Object[] {})).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        } catch (IllegalAccessException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            iae.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        } catch (InvocationTargetException ite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            ite.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
15638
014faa554d7a 7079260: InputContext leaks memory
pchelko
parents: 12829
diff changeset
   518
    //Cleans all the references
014faa554d7a 7079260: InputContext leaks memory
pchelko
parents: 12829
diff changeset
   519
    public static void cleanUp() {
014faa554d7a 7079260: InputContext leaks memory
pchelko
parents: 12829
diff changeset
   520
        apListener = null;
014faa554d7a 7079260: InputContext leaks memory
pchelko
parents: 12829
diff changeset
   521
        fgListener = null;
014faa554d7a 7079260: InputContext leaks memory
pchelko
parents: 12829
diff changeset
   522
        wgfListener = null;
014faa554d7a 7079260: InputContext leaks memory
pchelko
parents: 12829
diff changeset
   523
    }
014faa554d7a 7079260: InputContext leaks memory
pchelko
parents: 12829
diff changeset
   524
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    ////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    // Some stuff to test focus.
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 WindowGainedFocusListener wgfListener = new WindowGainedFocusListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    private static FocusGainedListener fgListener = new FocusGainedListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    private static ActionPerformedListener apListener = new ActionPerformedListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    private abstract static class EventListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        AtomicBoolean notifier = new AtomicBoolean(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        Component comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        boolean printEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        public void listen(Component comp, boolean printEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            this.comp = comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            this.printEvent = printEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            notifier.set(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            setListener(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        public AtomicBoolean getNotifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            return notifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        abstract void setListener(Component comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        void printAndNotify(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            if (printEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                System.err.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            synchronized (notifier) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                notifier.set(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                notifier.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            }
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
    private static class WindowGainedFocusListener extends EventListener implements WindowFocusListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        void setListener(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            ((Window)comp).addWindowFocusListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        public void windowGainedFocus(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            ((Window)comp).removeWindowFocusListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            printAndNotify(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        public void windowLostFocus(WindowEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    private static class FocusGainedListener extends EventListener implements FocusListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        void setListener(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            comp.addFocusListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            comp.removeFocusListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            printAndNotify(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        public void focusLost(FocusEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    private static class ActionPerformedListener extends EventListener implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        void setListener(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            ((Button)comp).addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            ((Button)comp).removeActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            printAndNotify(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    private static boolean trackEvent(int eventID, Component comp, Runnable action, int time, boolean printEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        EventListener listener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        switch (eventID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        case WindowEvent.WINDOW_GAINED_FOCUS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            listener = wgfListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        case FocusEvent.FOCUS_GAINED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            listener = fgListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        case ActionEvent.ACTION_PERFORMED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            listener = apListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        listener.listen(comp, printEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        action.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        return Util.waitForCondition(listener.getNotifier(), time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * Tracks WINDOW_GAINED_FOCUS event for a window caused by an action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @param window the window to track the event for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @param action the action to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @param time the max time to wait for the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * @param printEvent should the event received be printed or doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @return true if the event has been received, otherwise false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    public static boolean trackWindowGainedFocus(Window window, Runnable action, int time, boolean printEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        return trackEvent(WindowEvent.WINDOW_GAINED_FOCUS, window, action, time, printEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * Tracks FOCUS_GAINED event for a component caused by an action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @see #trackWindowGainedFocus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    public static boolean trackFocusGained(Component comp, Runnable action, int time, boolean printEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        return trackEvent(FocusEvent.FOCUS_GAINED, comp, action, time, printEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * Tracks ACTION_PERFORMED event for a button caused by an action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * @see #trackWindowGainedFocus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    public static boolean trackActionPerformed(Button button, Runnable action, int time, boolean printEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        return trackEvent(ActionEvent.ACTION_PERFORMED, button, action, time, printEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
1171
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   651
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   652
    /*
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   653
     * 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
   654
     * @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
   655
     */
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   656
    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
   657
        return focusComponent(comp, time, false);
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   658
    }
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   659
    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
   660
        return trackFocusGained(comp,
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   661
                                new Runnable() {
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   662
                                    public void run() {
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   663
                                        comp.requestFocus();
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   664
                                    }
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   665
                                },
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   666
                                time, printEvent);
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   667
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   668
    }
12535
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   669
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   670
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   671
    /**
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   672
     * Invokes the <code>task</code> on the EDT thread.
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   673
     *
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   674
     * @return result of the <code>task</code>
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   675
     */
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   676
    public static <T> T invokeOnEDT(final java.util.concurrent.Callable<T> task) throws Exception {
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   677
        final java.util.List<T> result = new java.util.ArrayList<T>(1);
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   678
        final Exception[] exception = new Exception[1];
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   679
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   680
        javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   681
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   682
            @Override
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   683
            public void run() {
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   684
                try {
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   685
                    result.add(task.call());
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   686
                } catch (Exception e) {
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   687
                    exception[0] = e;
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   688
                }
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   689
            }
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   690
        });
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   691
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   692
        if (exception[0] != null) {
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   693
            throw exception[0];
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   694
        }
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   695
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   696
        return result.get(0);
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   697
    }
6d2ee83614e8 7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
alexsch
parents: 5506
diff changeset
   698
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
}