29
29
* @run main ProgressMonitorEscapeKeyPress
30
30
*/
31
31
32
- import java .awt .AWTException ;
33
- import java .awt .EventQueue ;
34
- import java .awt .Robot ;
35
- import java .awt .event .KeyEvent ;
32
+
36
33
import javax .swing .JFrame ;
37
34
import javax .swing .ProgressMonitor ;
38
35
import javax .swing .SwingUtilities ;
36
+ import java .awt .Robot ;
37
+ import java .awt .event .KeyEvent ;
38
+ import java .util .concurrent .CountDownLatch ;
39
+ import java .util .concurrent .TimeUnit ;
39
40
40
41
public class ProgressMonitorEscapeKeyPress {
41
42
43
+ static volatile int counter = 0 ;
42
44
static ProgressMonitor monitor ;
43
- static int counter = 0 ;
44
- static TestThread robotThread ;
45
+ static TestThread testThread ;
45
46
static JFrame frame ;
47
+ static CountDownLatch progressLatch ;
48
+ static Robot robot ;
46
49
47
50
48
51
public static void main (String [] args ) throws Exception {
49
-
50
- createTestUI ();
51
-
52
- monitor = new ProgressMonitor (frame , "Progress" , null , 0 , 100 );
53
-
54
- robotThread = new TestThread ();
55
- robotThread .start ();
56
-
57
- for (counter = 0 ; counter <= 100 ; counter += 10 ) {
58
- Thread .sleep (1000 );
59
-
60
- EventQueue .invokeAndWait (new Runnable () {
61
- @ Override
62
- public void run () {
63
- if (!monitor .isCanceled ()) {
64
- monitor .setProgress (counter );
65
- System .out .println ("Progress bar is in progress" );
66
- }
67
- }
68
- });
69
-
70
- if (monitor .isCanceled ()) {
71
- break ;
52
+ try {
53
+ progressLatch = new CountDownLatch (20 );
54
+ createTestUI ();
55
+ robot = new Robot ();
56
+ robot .setAutoDelay (50 );
57
+ robot .setAutoWaitForIdle (true );
58
+ testThread = new TestThread ();
59
+ testThread .start ();
60
+ Thread .sleep (100 );
61
+ if (progressLatch .await (15 , TimeUnit .SECONDS )) {
62
+ System .out .println ("Progress monitor completed 20%, lets press Esc..." );
63
+ robot .keyPress (KeyEvent .VK_ESCAPE );
64
+ robot .keyRelease (KeyEvent .VK_ESCAPE );
65
+ System .out .println ("ESC pressed...." );
66
+ } else {
67
+ System .out .println ("Failure : No status available from Progress monitor..." );
68
+ throw new RuntimeException (
69
+ "Can't get the status from Progress monitor even after waiting too long.." );
72
70
}
73
- }
74
-
75
- disposeTestUI ();
76
71
77
- if (counter >= monitor .getMaximum ()) {
78
- throw new RuntimeException ("Escape key did not cancel the ProgressMonitor" );
72
+ if (counter >= monitor .getMaximum ()) {
73
+ throw new RuntimeException ("Escape key did not cancel the ProgressMonitor" );
74
+ }
75
+ System .out .println ("Test Passed..." );
76
+ } finally {
77
+ disposeTestUI ();
79
78
}
80
79
}
81
80
82
- private static void createTestUI () throws Exception {
83
- SwingUtilities .invokeAndWait (new Runnable () {
84
- @ Override
85
- public void run () {
86
- frame = new JFrame ("Test" );
87
- frame .setSize (300 , 300 );
88
- frame .setLocationByPlatform (true );
89
- frame .setVisible (true );
90
- }});
91
- }
81
+ private static void createTestUI () throws Exception {
82
+ SwingUtilities .invokeAndWait (() -> {
83
+ frame = new JFrame ("Test" );
84
+ frame .setSize (300 , 300 );
85
+ monitor = new ProgressMonitor (frame , "Progress" , "1" , 0 , 100 );
86
+ frame .setDefaultCloseOperation (JFrame .DISPOSE_ON_CLOSE );
87
+ frame .setLocationByPlatform (true );
88
+ });
89
+ }
92
90
93
91
94
92
private static void disposeTestUI () throws Exception {
@@ -100,26 +98,25 @@ private static void disposeTestUI() throws Exception {
100
98
101
99
102
100
class TestThread extends Thread {
103
-
104
- Robot testRobot ;
105
-
106
- TestThread () throws AWTException {
107
- super ();
108
- testRobot = new Robot ();
109
- }
110
-
111
101
@ Override
112
102
public void run () {
113
- try {
114
- // Sleep for 5 seconds - so that the ProgressMonitor starts
115
- Thread .sleep (5000 );
116
-
117
- // Press and release Escape key
118
- testRobot .keyPress (KeyEvent .VK_ESCAPE );
119
- testRobot .delay (20 );
120
- testRobot .keyRelease (KeyEvent .VK_ESCAPE );
121
- } catch (InterruptedException ex ) {
122
- throw new RuntimeException ("Exception in TestThread" );
103
+ System .out .println ("TestThread started........." );
104
+ for (ProgressMonitorEscapeKeyPress .counter = 0 ;
105
+ ProgressMonitorEscapeKeyPress .counter <= 100 ;
106
+ ProgressMonitorEscapeKeyPress .counter += 1 ) {
107
+ ProgressMonitorEscapeKeyPress .robot .delay (100 );
108
+ ProgressMonitor monitor = ProgressMonitorEscapeKeyPress .monitor ;
109
+ if (!monitor .isCanceled ()) {
110
+ monitor .setNote ("" + ProgressMonitorEscapeKeyPress .counter );
111
+ monitor .setProgress (ProgressMonitorEscapeKeyPress .counter );
112
+ ProgressMonitorEscapeKeyPress .progressLatch .countDown ();
113
+ System .out .println ("Progress bar is in progress....."
114
+ + ProgressMonitorEscapeKeyPress .counter + "%" );
115
+ }
116
+ if (monitor .isCanceled ()) {
117
+ System .out .println ("$$$$$$$$$$$$$$$ Monitor canceled" );
118
+ break ;
119
+ }
123
120
}
124
121
}
125
122
}
1 commit comments
openjdk-notifier[bot] commentedon Jan 20, 2025
Review
Issues