2929 * @run main ProgressMonitorEscapeKeyPress
3030 */
3131
32- import java .awt .AWTException ;
33- import java .awt .EventQueue ;
34- import java .awt .Robot ;
35- import java .awt .event .KeyEvent ;
32+
3633import javax .swing .JFrame ;
3734import javax .swing .ProgressMonitor ;
3835import 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 ;
3940
4041public class ProgressMonitorEscapeKeyPress {
4142
43+ static volatile int counter = 0 ;
4244 static ProgressMonitor monitor ;
43- static int counter = 0 ;
44- static TestThread robotThread ;
45+ static TestThread testThread ;
4546 static JFrame frame ;
47+ static CountDownLatch progressLatch ;
48+ static Robot robot ;
4649
4750
4851 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.." );
7270 }
73- }
74-
75- disposeTestUI ();
7671
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 ();
7978 }
8079 }
8180
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+ }
9290
9391
9492 private static void disposeTestUI () throws Exception {
@@ -100,26 +98,25 @@ private static void disposeTestUI() throws Exception {
10098
10199
102100class TestThread extends Thread {
103-
104- Robot testRobot ;
105-
106- TestThread () throws AWTException {
107- super ();
108- testRobot = new Robot ();
109- }
110-
111101 @ Override
112102 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+ }
123120 }
124121 }
125122}
0 commit comments