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 javax.swing.*;
25 import java.awt.*;
26 import java.awt.event.*;
27 import java.awt.image.BufferedImage;
28 import java.util.ArrayList;
29 import java.util.LinkedList;
30 import java.util.List;
31 import java.util.concurrent.Callable;
32 import sun.swing.*;
33
34 /**
35 * <p>This class contains utilities useful for regression testing.
36 * <p>When using jtreg you would include this class via something like:
37 * <pre>
38 *
39 * @library ../../regtesthelpers
40 * @build Util
41 * </pre>
42 */
43
44 public class Util {
45 /**
46 * Convert a rectangle from coordinate system of Component c to
47 * screen coordinate system.
48 *
49 * @param r a non-null Rectangle
50 * @param c a Component whose coordinate system is used for conversion
51 */
52 public static void convertRectToScreen(Rectangle r, Component c) {
213 public static <T> T invokeOnEDT(final Callable<T> task) throws Exception {
214 final List<T> result = new ArrayList<>(1);
215 final Exception[] exception = new Exception[1];
216
217 SwingUtilities.invokeAndWait(new Runnable() {
218 @Override
219 public void run() {
220 try {
221 result.add(task.call());
222 } catch (Exception e) {
223 exception[0] = e;
224 }
225 }
226 });
227
228 if (exception[0] != null) {
229 throw exception[0];
230 }
231
232 return result.get(0);
233 }
234 /**
235 * Gets key codes from system mnemonic key mask
236 * @return key codes list
237 */
238 public static ArrayList<Integer> getSystemMnemonicKeyCodes() {
239 return Util.getKeyCodesFromKeyMask(SwingUtilities2.getSystemMnemonicKeyMask());
240 }
241
242 /**
243 * Gets the key codes list from modifiers
244 * @param modifiers an integer combination of the modifier constants
245 * @return key codes list
246 */
247 public static ArrayList<Integer> getKeyCodesFromKeyMask(int modifiers) {
248 ArrayList<Integer> result = new ArrayList<>();
249 if ((modifiers & InputEvent.CTRL_MASK) != 0) {
250 result.add(KeyEvent.VK_CONTROL);
251 }
252 if ((modifiers & InputEvent.ALT_MASK) != 0) {
253 result.add(KeyEvent.VK_ALT);
254 }
255 if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
256 result.add(KeyEvent.VK_SHIFT);
257 }
258 if ((modifiers & InputEvent.META_MASK) != 0) {
259 result.add(KeyEvent.VK_META);
|
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 javax.swing.*;
25 import java.awt.*;
26 import java.awt.event.*;
27 import java.awt.image.BufferedImage;
28 import java.util.ArrayList;
29 import java.util.LinkedList;
30 import java.util.List;
31 import java.util.concurrent.Callable;
32 //import sun.swing.*;
33
34 /**
35 * <p>This class contains utilities useful for regression testing.
36 * <p>When using jtreg you would include this class via something like:
37 * <pre>
38 *
39 * @library ../../regtesthelpers
40 * @build Util
41 * </pre>
42 */
43
44 public class Util {
45 /**
46 * Convert a rectangle from coordinate system of Component c to
47 * screen coordinate system.
48 *
49 * @param r a non-null Rectangle
50 * @param c a Component whose coordinate system is used for conversion
51 */
52 public static void convertRectToScreen(Rectangle r, Component c) {
213 public static <T> T invokeOnEDT(final Callable<T> task) throws Exception {
214 final List<T> result = new ArrayList<>(1);
215 final Exception[] exception = new Exception[1];
216
217 SwingUtilities.invokeAndWait(new Runnable() {
218 @Override
219 public void run() {
220 try {
221 result.add(task.call());
222 } catch (Exception e) {
223 exception[0] = e;
224 }
225 }
226 });
227
228 if (exception[0] != null) {
229 throw exception[0];
230 }
231
232 return result.get(0);
233 }
234
235 /**
236 * Gets the key codes list from modifiers
237 * @param modifiers an integer combination of the modifier constants
238 * @return key codes list
239 */
240 public static ArrayList<Integer> getKeyCodesFromKeyMask(int modifiers) {
241 ArrayList<Integer> result = new ArrayList<>();
242 if ((modifiers & InputEvent.CTRL_MASK) != 0) {
243 result.add(KeyEvent.VK_CONTROL);
244 }
245 if ((modifiers & InputEvent.ALT_MASK) != 0) {
246 result.add(KeyEvent.VK_ALT);
247 }
248 if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
249 result.add(KeyEvent.VK_SHIFT);
250 }
251 if ((modifiers & InputEvent.META_MASK) != 0) {
252 result.add(KeyEvent.VK_META);
|