Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2011 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): Tim Boudreau |
39 |
* |
40 |
* Portions Copyrighted 2011 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.openide.util; |
43 |
|
44 |
import java.awt.AWTEvent; |
45 |
import java.awt.EventQueue; |
46 |
import java.awt.KeyboardFocusManager; |
47 |
import java.awt.Toolkit; |
48 |
import java.awt.Window; |
49 |
import java.awt.event.AWTEventListener; |
50 |
import java.awt.event.MouseEvent; |
51 |
import java.beans.PropertyChangeEvent; |
52 |
import java.beans.PropertyChangeListener; |
53 |
import java.lang.ref.Reference; |
54 |
import java.lang.ref.WeakReference; |
55 |
import java.util.Arrays; |
56 |
import java.util.Collections; |
57 |
import java.util.EnumSet; |
58 |
import java.util.HashSet; |
59 |
import java.util.Iterator; |
60 |
import java.util.LinkedHashSet; |
61 |
import java.util.LinkedList; |
62 |
import java.util.List; |
63 |
import java.util.Set; |
64 |
import java.util.concurrent.atomic.AtomicBoolean; |
65 |
import java.util.logging.Level; |
66 |
import java.util.logging.Logger; |
67 |
import org.openide.util.Parameters; |
68 |
import org.openide.util.RequestProcessor; |
69 |
import org.openide.util.RequestProcessor.Task; |
70 |
import org.openide.util.lookup.ServiceProvider; |
71 |
import org.openide.util.ApplicationActivationManager; |
72 |
|
73 |
/** |
74 |
* Default implementation of ApplicationActivationManager. |
75 |
* |
76 |
* @author Tim Boudreau |
77 |
*/ |
78 |
@ServiceProvider(service = ApplicationActivationManager.class) |
79 |
public class ApplicationActivationManagerImpl extends ApplicationActivationManager { |
80 |
|
81 |
private static final RequestProcessor rp = new RequestProcessor(ApplicationActivationManagerImpl.class); |
82 |
private static final int DEFAULT_ACTIVATION_DELAY = 800; |
83 |
private static final int DEFAULT_IDLE_DELAY = 120000; //two minutes - *really* idle |
84 |
private final Set<ListenerHolder> listeners = Collections.synchronizedSet(new LinkedHashSet<ListenerHolder>()); |
85 |
private final Object stateLock = new Object(); |
86 |
private final Object eventLock = new Object(); |
87 |
private final Object idleLock = new Object(); |
88 |
private final Set<State> state = EnumSet.noneOf(State.class); |
89 |
private final List<StateEvent> events = new LinkedList<StateEvent>(); |
90 |
private final AtomicBoolean inNotificationLoop = new AtomicBoolean(); |
91 |
private final Set<RunOnceListener> runOnceReferences = Collections.synchronizedSet(new HashSet<RunOnceListener>()); |
92 |
//avoid rapid transiently switching to the application, for example |
93 |
//moving between virtual desktops, to trigger work |
94 |
private final IdleListener idleListener = new IdleListener(); |
95 |
private final Task addIdleStateTask = rp.create(idleListener); |
96 |
private final KeyboardFocusListener keyboardFocusManagerListener = new KeyboardFocusListener(); |
97 |
private final Task updateActiveStateTask = rp.create(keyboardFocusManagerListener); |
98 |
private volatile boolean stateIsIdle; |
99 |
private volatile boolean listeningForIdle; |
100 |
private int idleDelay; |
101 |
private int activationDelay; |
102 |
private static final String IDLE_SYSTEM_PROPERTY = "application.idle.delay"; |
103 |
private static final String ACTIVATION_SYSTEM_PROPERTY = "application.activation.delay"; |
104 |
|
105 |
public ApplicationActivationManagerImpl() { |
106 |
//check system properties |
107 |
try { |
108 |
String prop = System.getProperty(IDLE_SYSTEM_PROPERTY); |
109 |
if (prop != null) { |
110 |
idleDelay = Integer.parseInt(prop); |
111 |
if (idleDelay <= 0) { |
112 |
throw new NumberFormatException("Negative " + IDLE_SYSTEM_PROPERTY); |
113 |
} |
114 |
} else { |
115 |
idleDelay = DEFAULT_IDLE_DELAY; |
116 |
} |
117 |
} catch (NumberFormatException nfe) { |
118 |
Logger.getLogger(ApplicationActivationManagerImpl.class.getName()).log(Level.WARNING, IDLE_SYSTEM_PROPERTY, nfe); |
119 |
idleDelay = DEFAULT_IDLE_DELAY; |
120 |
} |
121 |
try { |
122 |
String prop = System.getProperty(ACTIVATION_SYSTEM_PROPERTY); |
123 |
if (prop != null) { |
124 |
activationDelay = Integer.parseInt(prop); |
125 |
} else { |
126 |
activationDelay = DEFAULT_ACTIVATION_DELAY; |
127 |
} |
128 |
if (activationDelay <= 0) { |
129 |
throw new NumberFormatException("Negative " + ACTIVATION_SYSTEM_PROPERTY); |
130 |
} |
131 |
} catch (NumberFormatException nfe) { |
132 |
Logger.getLogger(ApplicationActivationManagerImpl.class.getName()).log(Level.WARNING, ACTIVATION_SYSTEM_PROPERTY, nfe); |
133 |
activationDelay = DEFAULT_ACTIVATION_DELAY; |
134 |
} |
135 |
//attach our listener |
136 |
EventQueue.invokeLater(new Runnable() { |
137 |
|
138 |
@Override |
139 |
public void run() { |
140 |
KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener("focusedWindow", keyboardFocusManagerListener); |
141 |
boolean active = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow() != null; |
142 |
if (active) { |
143 |
setState(EnumSet.of(State.ACTIVE)); |
144 |
} |
145 |
} |
146 |
}); |
147 |
} |
148 |
|
149 |
@Override |
150 |
public Set<State> getState() { |
151 |
synchronized (stateLock) { |
152 |
return EnumSet.copyOf(state); |
153 |
} |
154 |
} |
155 |
|
156 |
void setState(Set<State> newState) { |
157 |
Set<State> oldState; |
158 |
synchronized (stateLock) { |
159 |
oldState = getState(); |
160 |
if (newState.equals(oldState)) { |
161 |
return; |
162 |
} |
163 |
this.state.clear(); |
164 |
this.state.addAll(newState); |
165 |
onChange(this.state); |
166 |
} |
167 |
notify(oldState, newState); |
168 |
} |
169 |
|
170 |
private void notify(Set<State> old, Set<State> nue) { |
171 |
if (old != null) { |
172 |
synchronized (eventLock) { |
173 |
events.add(new StateEvent(old, nue)); |
174 |
} |
175 |
} |
176 |
boolean notify = inNotificationLoop.compareAndSet(false, true); |
177 |
//another caller can already be performing notifications, depending on |
178 |
//which thread last triggered a state change. If so, they will be |
179 |
//delivered |
180 |
if (notify) { |
181 |
//list of things to run on whatever thread this is not |
182 |
final List<Notification> notifyOnOtherThread = new LinkedList<Notification>(); |
183 |
boolean inEQ = EventQueue.isDispatchThread(); |
184 |
try { |
185 |
final List<StateEvent> notifyEvents = new LinkedList<StateEvent>(); |
186 |
synchronized (eventLock) { |
187 |
notifyEvents.addAll(this.events); |
188 |
this.events.clear(); |
189 |
} |
190 |
while (!notifyEvents.isEmpty()) { |
191 |
for (StateEvent evt : notifyEvents) { |
192 |
for (Iterator<ListenerHolder> it = listeners.iterator(); it.hasNext();) { |
193 |
ListenerHolder holder = it.next(); |
194 |
Listener l = holder.getListener(); |
195 |
if (l == null) { |
196 |
it.remove(); |
197 |
} else { |
198 |
//Filter out states this listener does not care about |
199 |
Set<State> oldState = holder.filter(evt.old); |
200 |
Set<State> newState = holder.filter(evt.nue); |
201 |
//If the result is no change, do not notify |
202 |
if (!oldState.equals(newState)) { |
203 |
//Synchronously notify whichever listeners want |
204 |
//to be notified on this thread |
205 |
if (holder.notifyInBackground != inEQ) { |
206 |
try { |
207 |
l.onChange(oldState, newState); |
208 |
} catch (RuntimeException e) { |
209 |
Logger.getLogger(ApplicationActivationManagerImpl.class.getName()).log(Level.SEVERE, null, e); |
210 |
} |
211 |
} else { |
212 |
//add the rest to the queued list, for |
213 |
//notification on a background thread |
214 |
notifyOnOtherThread.add(new Notification(l, new StateEvent(oldState, newState))); |
215 |
} |
216 |
} |
217 |
} |
218 |
} |
219 |
} |
220 |
//Collect any events that have arrived while |
221 |
//we have been looping |
222 |
synchronized (eventLock) { |
223 |
notifyEvents.clear(); |
224 |
notifyEvents.addAll(this.events); |
225 |
this.events.clear(); |
226 |
} |
227 |
} |
228 |
} finally { |
229 |
inNotificationLoop.set(false); |
230 |
} |
231 |
boolean renotify; |
232 |
synchronized (eventLock) { |
233 |
//it is possible for another thread to have entered while the |
234 |
//loop variable was set to false - so do a quick check to make |
235 |
//sure we don't have events that should be delivered but won't be |
236 |
//unless we call ourselves again |
237 |
renotify = !events.isEmpty(); |
238 |
} |
239 |
if (!notifyOnOtherThread.isEmpty()) { |
240 |
//Asynchronously notify any listeners we are on the wrong |
241 |
//thread for |
242 |
Runnable r = new Runnable() { |
243 |
|
244 |
@Override |
245 |
public void run() { |
246 |
for (Notification l : notifyOnOtherThread) { |
247 |
try { |
248 |
l.notifyListener(); |
249 |
} catch (RuntimeException e) { |
250 |
Logger.getLogger(ApplicationActivationManagerImpl.class.getName()).log(Level.SEVERE, null, e); |
251 |
} |
252 |
} |
253 |
} |
254 |
}; |
255 |
if (inEQ) { |
256 |
rp.post(r); |
257 |
} else { |
258 |
EventQueue.invokeLater(r); |
259 |
} |
260 |
} |
261 |
if (renotify) { |
262 |
notify(null, null); |
263 |
} |
264 |
} |
265 |
} |
266 |
|
267 |
private void startListeningForIdleEvents() { |
268 |
if (!listeningForIdle) { |
269 |
//ensure we don't have a diff between the value of listeningForIdle |
270 |
//and whether or not we are actually listening |
271 |
synchronized (idleLock) { |
272 |
if (!listeningForIdle) { |
273 |
listeningForIdle = true; |
274 |
Toolkit.getDefaultToolkit().addAWTEventListener(idleListener, AWTEvent.MOUSE_EVENT_MASK | AWTEvent.KEY_EVENT_MASK); |
275 |
addIdleStateTask.schedule(idleDelay); |
276 |
} |
277 |
} |
278 |
} |
279 |
} |
280 |
|
281 |
private void stopListeningForIdleEvents() { |
282 |
if (listeningForIdle) { |
283 |
synchronized (idleLock) { |
284 |
if (listeningForIdle) { |
285 |
addIdleStateTask.cancel(); |
286 |
Toolkit.getDefaultToolkit().removeAWTEventListener(idleListener); |
287 |
listeningForIdle = false; |
288 |
} |
289 |
} |
290 |
} |
291 |
} |
292 |
|
293 |
private final class IdleListener implements AWTEventListener, Runnable { |
294 |
|
295 |
private volatile long lastEventTime = System.currentTimeMillis(); |
296 |
|
297 |
@Override |
298 |
public void eventDispatched(AWTEvent event) { |
299 |
int id = event.getID(); |
300 |
//ignore mouse motion |
301 |
if (id == MouseEvent.MOUSE_MOVED || id == MouseEvent.MOUSE_ENTERED || id == MouseEvent.MOUSE_EXITED || id == MouseEvent.MOUSE_DRAGGED) { |
302 |
return; |
303 |
} |
304 |
lastEventTime = System.currentTimeMillis(); |
305 |
//postpone the idle task |
306 |
addIdleStateTask.schedule(idleDelay); |
307 |
if (stateIsIdle) { //avoids the lock in the common case that the state is not idle |
308 |
synchronized (stateLock) { |
309 |
Set<State> states = getState(); |
310 |
states.remove(State.IDLE); |
311 |
//will run notifications under lock |
312 |
setState(states); |
313 |
} |
314 |
} |
315 |
} |
316 |
|
317 |
@Override |
318 |
public void run() { |
319 |
//run after the idle delay |
320 |
long timeSinceLastEvent = System.currentTimeMillis() - lastEventTime; |
321 |
if (timeSinceLastEvent > idleDelay && getState().equals(EnumSet.of(State.ACTIVE))) { |
322 |
//add the idle state |
323 |
changeState(State.IDLE); |
324 |
} |
325 |
} |
326 |
} |
327 |
|
328 |
private void onChange(Set<State> states) { |
329 |
//update the stateIsIdle value so we avoid taking a lock on every |
330 |
//keystroke / mouse click |
331 |
stateIsIdle = states.contains(State.IDLE); |
332 |
if (states.contains(State.ACTIVE)) { |
333 |
startListeningForIdleEvents(); |
334 |
} else { |
335 |
stopListeningForIdleEvents(); |
336 |
} |
337 |
} |
338 |
|
339 |
private static final class StateEvent { |
340 |
|
341 |
private final Set<State> old; |
342 |
private final Set<State> nue; |
343 |
|
344 |
public StateEvent(Set<State> old, Set<State> nue) { |
345 |
assert !old.equals(nue); |
346 |
this.old = old; |
347 |
this.nue = nue; |
348 |
} |
349 |
|
350 |
public String toString() { |
351 |
return old + " to " + nue; |
352 |
} |
353 |
} |
354 |
|
355 |
private static final class Notification { |
356 |
|
357 |
private final Listener listener; |
358 |
private final StateEvent evt; |
359 |
|
360 |
public Notification(Listener listener, StateEvent evt) { |
361 |
this.listener = listener; |
362 |
this.evt = evt; |
363 |
} |
364 |
|
365 |
void notifyListener() { |
366 |
listener.onChange(evt.old, evt.nue); |
367 |
} |
368 |
} |
369 |
|
370 |
public void runWhenNextActive(Runnable toRun, boolean notifyInForeground) { |
371 |
Parameters.notNull("toRun", toRun); |
372 |
if (isActive()) { |
373 |
toRun.run(); |
374 |
} else { |
375 |
RunOnceListener l = new RunOnceListener(toRun); |
376 |
if (!runOnceReferences.contains(l)) { |
377 |
runOnceReferences.add(l); |
378 |
addListener(l, notifyInForeground, State.ACTIVE); |
379 |
} |
380 |
} |
381 |
} |
382 |
|
383 |
private final class RunOnceListener implements Listener { |
384 |
|
385 |
private final Runnable run; |
386 |
|
387 |
RunOnceListener(Runnable run) { |
388 |
this.run = run; |
389 |
} |
390 |
|
391 |
@Override |
392 |
public void onChange(Set<State> from, Set<State> to) { |
393 |
if (to.contains(State.ACTIVE) && runOnceReferences.remove(this)) { |
394 |
run.run(); |
395 |
} |
396 |
} |
397 |
|
398 |
public boolean equals(Object o) { |
399 |
return o instanceof RunOnceListener && ((RunOnceListener) o).run.equals(run); |
400 |
} |
401 |
|
402 |
public int hashCode() { |
403 |
return run.hashCode(); |
404 |
} |
405 |
} |
406 |
|
407 |
@Override |
408 |
public void addListener(Listener listener, boolean notifyInForeground, State... interestedIn) { |
409 |
Parameters.notNull("listener", listener); |
410 |
if (interestedIn.length == 0) { |
411 |
throw new IllegalArgumentException("No states"); |
412 |
} |
413 |
listeners.add(new ListenerHolder(listener, notifyInForeground, |
414 |
EnumSet.copyOf(Arrays.asList(interestedIn)))); |
415 |
} |
416 |
|
417 |
private static final class ListenerHolder { |
418 |
|
419 |
private final Reference<Listener> listenerRef; |
420 |
private final boolean notifyInBackground; |
421 |
private final Set<State> interestedIn; |
422 |
|
423 |
public ListenerHolder(Listener listener, boolean notifyInBackground, Set<State> interestedIn) { |
424 |
this.notifyInBackground = notifyInBackground; |
425 |
this.interestedIn = interestedIn; |
426 |
this.listenerRef = new WeakReference<Listener>(listener); |
427 |
} |
428 |
|
429 |
Listener getListener() { |
430 |
return listenerRef.get(); |
431 |
} |
432 |
|
433 |
Set<State> filter(Set<State> states) { |
434 |
Set<State> nue = EnumSet.copyOf(states); |
435 |
nue.retainAll(interestedIn); |
436 |
return nue; |
437 |
} |
438 |
} |
439 |
|
440 |
private boolean changeState(State toAdd, State... toRemove) { |
441 |
//add one state and optionally remove others, then update the state |
442 |
Set<State> oldState; |
443 |
Set<State> newState; |
444 |
synchronized (stateLock) { |
445 |
oldState = getState(); |
446 |
newState = EnumSet.copyOf(oldState); |
447 |
if (toAdd != null) { |
448 |
newState.add(toAdd); |
449 |
} |
450 |
newState.removeAll(Arrays.asList(toRemove)); |
451 |
if (newState.equals(oldState)) { |
452 |
return false; |
453 |
} |
454 |
this.state.clear(); |
455 |
this.state.addAll(newState); |
456 |
onChange(this.state); |
457 |
} |
458 |
notify(oldState, newState); |
459 |
return true; |
460 |
} |
461 |
|
462 |
private final class KeyboardFocusListener implements PropertyChangeListener, Runnable { |
463 |
|
464 |
volatile boolean active; |
465 |
State destState; |
466 |
|
467 |
@Override |
468 |
public void propertyChange(PropertyChangeEvent evt) { |
469 |
//Note one weirdness I've never been able to decipher: In a regular |
470 |
//swing application, when you show a dialog, focus goes from the |
471 |
//parent window to the dialog. In NetBeans, it goes from the |
472 |
//parent window to null to the dialog, and the same when closing. |
473 |
//This causes a plethora of extra BECOMING_ACTIVE / BECOMING_INACTIVE |
474 |
//events which are unavoidable |
475 |
Window old = (Window) evt.getOldValue(); |
476 |
Window nue = (Window) evt.getNewValue(); |
477 |
if ((old == null) != (nue == null)) { |
478 |
active = nue != null; |
479 |
if (!active) { |
480 |
//add the becoming inactive state, remove becoming active if present |
481 |
changeState(State.BECOMING_INACTIVE, State.BECOMING_ACTIVE); |
482 |
//set our single destination state |
483 |
synchronized (this) { |
484 |
destState = null; |
485 |
} |
486 |
//reschedule updating the final state |
487 |
updateActiveStateTask.schedule(activationDelay); |
488 |
} else { |
489 |
//add the becoming active state, remove the becoming inactive state |
490 |
changeState(State.BECOMING_ACTIVE, State.BECOMING_INACTIVE); |
491 |
//set our single destination state |
492 |
synchronized (this) { |
493 |
destState = State.ACTIVE; |
494 |
} |
495 |
//reschedule the task |
496 |
updateActiveStateTask.schedule(activationDelay); |
497 |
} |
498 |
} |
499 |
} |
500 |
|
501 |
@Override |
502 |
public void run() { |
503 |
//Called when the state has been BECOMING_* for a sufficient |
504 |
//amount of time |
505 |
State dest; |
506 |
synchronized (this) { |
507 |
dest = this.destState; |
508 |
} |
509 |
if (dest == null) { |
510 |
changeState(null, State.BECOMING_INACTIVE, State.ACTIVE); |
511 |
} else { |
512 |
changeState(destState, State.BECOMING_ACTIVE); |
513 |
} |
514 |
} |
515 |
} |
516 |
} |