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