|
1 | 1 | /* |
2 | | - * Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
29 | 29 | * @run main bug4506788 |
30 | 30 | */ |
31 | 31 |
|
32 | | -import java.awt.*; |
33 | | -import java.awt.event.*; |
34 | | -import java.lang.reflect.InvocationTargetException; |
35 | | -import javax.swing.*; |
36 | | -import javax.swing.event.*; |
37 | | -import javax.swing.text.*; |
| 32 | +import java.awt.Dimension; |
| 33 | +import java.awt.Point; |
| 34 | +import java.awt.Robot; |
| 35 | +import java.awt.event.KeyEvent; |
| 36 | +import java.awt.event.InputEvent; |
| 37 | +import javax.swing.JEditorPane; |
| 38 | +import javax.swing.JFrame; |
| 39 | +import javax.swing.SwingUtilities; |
| 40 | +import javax.swing.event.CaretEvent; |
| 41 | +import javax.swing.event.CaretListener; |
| 42 | +import javax.swing.text.DefaultStyledDocument; |
| 43 | +import javax.swing.text.MutableAttributeSet; |
| 44 | +import javax.swing.text.SimpleAttributeSet; |
| 45 | +import javax.swing.text.StyleConstants; |
| 46 | +import javax.swing.text.StyledEditorKit; |
38 | 47 |
|
39 | 48 | public class bug4506788 { |
40 | 49 |
|
41 | | - private volatile boolean passed = false; |
42 | | - private JEditorPane jep; |
| 50 | + private static volatile boolean passed; |
| 51 | + private static volatile Point p; |
| 52 | + private static volatile Dimension dim; |
| 53 | + private static JEditorPane jep; |
| 54 | + private static JFrame f; |
43 | 55 |
|
44 | | - public static void main(final String[] args) { |
45 | | - bug4506788 app = new bug4506788(); |
46 | | - app.init(); |
47 | | - app.start(); |
48 | | - } |
49 | | - |
50 | | - public void init() { |
51 | | - try { |
52 | | - SwingUtilities.invokeAndWait(new Runnable() { |
53 | | - @Override |
54 | | - public void run() { |
55 | | - createAndShowGUI(); |
56 | | - } |
57 | | - }); |
58 | | - } catch (InterruptedException | InvocationTargetException ex) { |
59 | | - ex.printStackTrace(); |
60 | | - throw new RuntimeException("FAILED: SwingUtilities.invokeAndWait method failed then creating and showing GUI"); |
61 | | - } |
62 | | - } |
63 | | - |
64 | | - public void start() { |
65 | | - Robot robot; |
| 56 | + public static void main(final String[] args) throws Exception { |
66 | 57 | try { |
67 | | - robot = new Robot(); |
| 58 | + Robot robot = new Robot(); |
68 | 59 | robot.setAutoDelay(100); |
69 | | - } catch (AWTException e) { |
70 | | - throw new RuntimeException("Robot could not be created"); |
71 | | - } |
72 | | - |
73 | | - robot.waitForIdle(); |
74 | | - |
75 | | - Point p; |
76 | | - try { |
77 | | - p = getJEPLocOnScreen(); |
78 | | - } catch (Exception e) { |
79 | | - throw new RuntimeException("Could not get JEditorPane location on screen"); |
80 | | - } |
81 | | - |
82 | | - robot.mouseMove(p.x, p.y); |
83 | | - robot.mousePress(InputEvent.BUTTON1_MASK); |
84 | | - robot.mouseRelease(InputEvent.BUTTON1_MASK); |
85 | | - robot.keyPress(KeyEvent.VK_HOME); |
86 | | - robot.keyRelease(KeyEvent.VK_HOME); |
87 | | - robot.keyPress(KeyEvent.VK_RIGHT); |
88 | | - robot.keyRelease(KeyEvent.VK_RIGHT); |
89 | | - robot.keyPress(KeyEvent.VK_X); |
90 | | - robot.keyRelease(KeyEvent.VK_X); |
91 | | - robot.keyPress(KeyEvent.VK_RIGHT); |
92 | | - robot.keyRelease(KeyEvent.VK_RIGHT); |
93 | | - |
94 | | - robot.waitForIdle(); |
95 | 60 |
|
96 | | - if (!passed) { |
97 | | - throw new RuntimeException("Test failed."); |
98 | | - } |
99 | | - } |
| 61 | + SwingUtilities.invokeAndWait(() -> createAndShowGUI()); |
100 | 62 |
|
101 | | - private Point getJEPLocOnScreen() throws Exception { |
| 63 | + robot.waitForIdle(); |
| 64 | + robot.delay(1000); |
102 | 65 |
|
103 | | - final Point[] result = new Point[1]; |
| 66 | + SwingUtilities.invokeAndWait(() -> { |
| 67 | + p = jep.getLocationOnScreen(); |
| 68 | + dim = jep.getSize(); |
| 69 | + }); |
104 | 70 |
|
105 | | - SwingUtilities.invokeAndWait(new Runnable() { |
106 | | - @Override |
107 | | - public void run() { |
108 | | - result[0] = jep.getLocationOnScreen(); |
| 71 | + robot.mouseMove(p.x + dim.width / 2, p.y + dim.height / 2); |
| 72 | + robot.waitForIdle(); |
| 73 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 74 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 75 | + robot.waitForIdle(); |
| 76 | + robot.keyPress(KeyEvent.VK_HOME); |
| 77 | + robot.keyRelease(KeyEvent.VK_HOME); |
| 78 | + robot.waitForIdle(); |
| 79 | + robot.keyPress(KeyEvent.VK_RIGHT); |
| 80 | + robot.keyRelease(KeyEvent.VK_RIGHT); |
| 81 | + robot.waitForIdle(); |
| 82 | + robot.keyPress(KeyEvent.VK_X); |
| 83 | + robot.keyRelease(KeyEvent.VK_X); |
| 84 | + robot.waitForIdle(); |
| 85 | + robot.keyPress(KeyEvent.VK_RIGHT); |
| 86 | + robot.keyRelease(KeyEvent.VK_RIGHT); |
| 87 | + robot.waitForIdle(); |
| 88 | + |
| 89 | + if (!passed) { |
| 90 | + throw new RuntimeException("Test failed."); |
109 | 91 | } |
110 | | - }); |
111 | | - |
112 | | - return result[0]; |
| 92 | + } finally { |
| 93 | + SwingUtilities.invokeAndWait(() -> { |
| 94 | + if (f != null) { |
| 95 | + f.dispose(); |
| 96 | + } |
| 97 | + }); |
| 98 | + } |
113 | 99 | } |
114 | 100 |
|
115 | | - private void createAndShowGUI() { |
| 101 | + private static void createAndShowGUI() { |
116 | 102 | jep = new JEditorPane(); |
117 | 103 | String text = "abc"; |
118 | | - JFrame f = new JFrame(); |
| 104 | + f = new JFrame("bug4506788"); |
119 | 105 | jep.setEditorKit(new StyledEditorKit()); |
120 | 106 | jep.setText(text); |
121 | 107 | jep.addCaretListener(new CaretListener() { |
122 | 108 | @Override |
123 | 109 | public void caretUpdate(CaretEvent e) { |
| 110 | + System.out.println("getDot " + e.getDot()); |
124 | 111 | passed = (e.getDot() == 3); |
125 | 112 | } |
126 | 113 | }); |
|
0 commit comments