author | henryjen |
Tue, 10 Jun 2014 16:18:54 -0700 | |
changeset 24865 | 09b1d992ca72 |
parent 7494 | 3a6d20cc8595 |
permissions | -rw-r--r-- |
6484
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
1 |
/* |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
2 |
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
4 |
* |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
10 |
* |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
15 |
* accompanied this code). |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
16 |
* |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
20 |
* |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
23 |
* questions. |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
24 |
*/ |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
25 |
|
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
26 |
package java.awt; |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
27 |
|
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
28 |
/** |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
29 |
* A helper interface to run the nested event loop. |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
30 |
* <p> |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
31 |
* Objects that implement this interface are created with the |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
32 |
* {@link EventQueue#createSecondaryLoop} method. The interface |
7494 | 33 |
* provides two methods, {@link #enter} and {@link #exit}, |
6484
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
34 |
* which can be used to start and stop the event loop. |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
35 |
* <p> |
7494 | 36 |
* When the {@link #enter} method is called, the current |
6484
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
37 |
* thread is blocked until the loop is terminated by the |
7494 | 38 |
* {@link #exit} method. Also, a new event loop is started |
6484
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
39 |
* on the event dispatch thread, which may or may not be |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
40 |
* the current thread. The loop can be terminated on any |
7494 | 41 |
* thread by calling its {@link #exit} method. After the |
6484
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
42 |
* loop is terminated, the {@code SecondaryLoop} object can |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
43 |
* be reused to run a new nested event loop. |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
44 |
* <p> |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
45 |
* A typical use case of applying this interface is AWT |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
46 |
* and Swing modal dialogs. When a modal dialog is shown on |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
47 |
* the event dispatch thread, it enters a new secondary loop. |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
48 |
* Later, when the dialog is hidden or disposed, it exits |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
49 |
* the loop, and the thread continues its execution. |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
50 |
* <p> |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
51 |
* The following example illustrates a simple use case of |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
52 |
* secondary loops: |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
53 |
* |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
54 |
* <pre> |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
55 |
* SecondaryLoop loop; |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
56 |
* |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
57 |
* JButton jButton = new JButton("Button"); |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
58 |
* jButton.addActionListener(new ActionListener() { |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
59 |
* {@code @Override} |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
60 |
* public void actionPerformed(ActionEvent e) { |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
61 |
* Toolkit tk = Toolkit.getDefaultToolkit(); |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
62 |
* EventQueue eq = tk.getSystemEventQueue(); |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
63 |
* loop = eq.createSecondaryLoop(); |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
64 |
* |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
65 |
* // Spawn a new thread to do the work |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
66 |
* Thread worker = new WorkerThread(); |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
67 |
* worker.start(); |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
68 |
* |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
69 |
* // Enter the loop to block the current event |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
70 |
* // handler, but leave UI responsive |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
71 |
* if (!loop.enter()) { |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
72 |
* // Report an error |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
73 |
* } |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
74 |
* } |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
75 |
* }); |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
76 |
* |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
77 |
* class WorkerThread extends Thread { |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
78 |
* {@code @Override} |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
79 |
* public void run() { |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
80 |
* // Perform calculations |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
81 |
* doSomethingUseful(); |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
82 |
* |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
83 |
* // Exit the loop |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
84 |
* loop.exit(); |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
85 |
* } |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
86 |
* } |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
87 |
* </pre> |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
88 |
* |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
89 |
* @see Dialog#show |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
90 |
* @see EventQueue#createSecondaryLoop |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
91 |
* @see Toolkit#getSystemEventQueue |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
92 |
* |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
93 |
* @author Anton Tarasov, Artem Ananiev |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
94 |
* |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
95 |
* @since 1.7 |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
96 |
*/ |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
97 |
public interface SecondaryLoop { |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
98 |
|
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
99 |
/** |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
100 |
* Blocks the execution of the current thread and enters a new |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
101 |
* secondary event loop on the event dispatch thread. |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
102 |
* <p> |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
103 |
* This method can be called by any thread including the event |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
104 |
* dispatch thread. This thread will be blocked until the {@link |
7494 | 105 |
* #exit} method is called or the loop is terminated. A new |
6484
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
106 |
* secondary loop will be created on the event dispatch thread |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
107 |
* for dispatching events in either case. |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
108 |
* <p> |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
109 |
* This method can only start one new event loop at a time per |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
110 |
* object. If a secondary event loop has already been started |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
111 |
* by this object and is currently still running, this method |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
112 |
* returns {@code false} to indicate that it was not successful |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
113 |
* in starting a new event loop. Otherwise, this method blocks |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
114 |
* the calling thread and later returns {@code true} when the |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
115 |
* new event loop is terminated. At such time, this object can |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
116 |
* again be used to start another new event loop. |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
117 |
* |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
118 |
* @return {@code true} after termination of the secondary loop, |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
119 |
* if the secondary loop was started by this call, |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
120 |
* {@code false} otherwise |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
121 |
*/ |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
122 |
public boolean enter(); |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
123 |
|
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
124 |
/** |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
125 |
* Unblocks the execution of the thread blocked by the {@link |
7494 | 126 |
* #enter} method and exits the secondary loop. |
6484
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
127 |
* <p> |
7494 | 128 |
* This method resumes the thread that called the {@link #enter} |
6484
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
129 |
* method and exits the secondary loop that was created when |
7494 | 130 |
* the {@link #enter} method was invoked. |
6484
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
131 |
* <p> |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
132 |
* Note that if any other secondary loop is started while this |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
133 |
* loop is running, the blocked thread will not resume execution |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
134 |
* until the nested loop is terminated. |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
135 |
* <p> |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
136 |
* If this secondary loop has not been started with the {@link |
7494 | 137 |
* #enter} method, or this secondary loop has already finished |
138 |
* with the {@link #exit} method, this method returns {@code |
|
6484
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
139 |
* false}, otherwise {@code true} is returned. |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
140 |
* |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
141 |
* @return {@code true} if this loop was previously started and |
7494 | 142 |
* has not yet been finished with the {@link #exit} method, |
6484
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
143 |
* {@code false} otherwise |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
144 |
*/ |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
145 |
public boolean exit(); |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
146 |
|
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
diff
changeset
|
147 |
} |