jdk/test/java/awt/Modal/helpers/Flag.java
changeset 25202 a617d3450e3e
equal deleted inserted replaced
25201:4adc75e0c4e5 25202:a617d3450e3e
       
     1 /*
       
     2  * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 import java.awt.Component;
       
    25 import java.awt.IllegalComponentStateException;
       
    26 
       
    27 public class Flag {
       
    28 
       
    29     public static final int ATTEMPTS = 50;
       
    30 
       
    31     private volatile boolean flag;
       
    32     private final Object monitor = new Object();
       
    33     private final long delay;
       
    34 
       
    35     public Flag() {
       
    36         this.delay = 500;
       
    37     }
       
    38 
       
    39     public void reset() {
       
    40         flag = false;
       
    41     }
       
    42 
       
    43     public void flagTriggered() {
       
    44         synchronized (monitor) {
       
    45             flag = true;
       
    46             monitor.notifyAll();
       
    47         }
       
    48     }
       
    49 
       
    50     public boolean flag() {
       
    51         return flag;
       
    52     }
       
    53 
       
    54     public void waitForFlagTriggered() throws InterruptedException {
       
    55         waitForFlagTriggered(delay, ATTEMPTS);
       
    56     }
       
    57 
       
    58     public void waitForFlagTriggered(int attempts) throws InterruptedException {
       
    59         waitForFlagTriggered(delay, attempts);
       
    60     }
       
    61 
       
    62     public void waitForFlagTriggered(long delay) throws InterruptedException {
       
    63         waitForFlagTriggered(delay, ATTEMPTS);
       
    64     }
       
    65 
       
    66     private void waitForFlagTriggered(long delay, int attempts) throws InterruptedException {
       
    67         int a = 0;
       
    68         synchronized (monitor) {
       
    69             while (!flag && (a++ < attempts)) {
       
    70                 monitor.wait(delay);
       
    71             }
       
    72         }
       
    73     }
       
    74 
       
    75     public static void waitTillShown(final Component comp) throws InterruptedException {
       
    76         while (true) {
       
    77             try {
       
    78                 Thread.sleep(100);
       
    79                 comp.getLocationOnScreen();
       
    80                 break;
       
    81             } catch (IllegalComponentStateException e) {}
       
    82         }
       
    83     }
       
    84 }