| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Original copyright is held by Nathan Arthur |
| 16 | // [Java Specialists' Newsletter, Issue #75, 2003-07-29] |
| 17 | |
| 18 | package net.sourceforge.hivegui.autowait; |
| 19 | |
| 20 | import java.awt.AWTEvent; |
| 21 | import java.awt.EventQueue; |
| 22 | |
| 23 | import org.apache.commons.logging.Log; |
| 24 | import org.apache.commons.logging.LogFactory; |
| 25 | |
| 26 | public class WaitCursorEventQueue extends EventQueue implements DelayTimerCallback |
| 27 | { |
| 28 | static private final Log _logger = LogFactory.getLog(WaitCursorEventQueue.class); |
| 29 | |
| 30 | public WaitCursorEventQueue(int delay) |
| 31 | { |
| 32 | _waitTimer = new DelayTimer(this, delay); |
| 33 | _cursorManager = new CursorManager(_waitTimer); |
| 34 | } |
| 35 | |
| 36 | public void close() |
| 37 | { |
| 38 | _waitTimer.quit(); |
| 39 | pop(); |
| 40 | } |
| 41 | |
| 42 | @Override protected void dispatchEvent(AWTEvent event) |
| 43 | { |
| 44 | if (_logger.isDebugEnabled()) |
| 45 | { |
| 46 | _logger.debug("ENTER dispatchEvent() event=" + event); |
| 47 | } |
| 48 | _cursorManager.push(event.getSource()); |
| 49 | _waitTimer.startTimer(); |
| 50 | try |
| 51 | { |
| 52 | super.dispatchEvent(event); |
| 53 | } |
| 54 | finally |
| 55 | { |
| 56 | _waitTimer.stopTimer(); |
| 57 | _cursorManager.pop(); |
| 58 | if (_logger.isDebugEnabled()) |
| 59 | { |
| 60 | _logger.debug("LEAVE dispatchEvent() event=" + event); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | @Override public AWTEvent getNextEvent() throws InterruptedException |
| 66 | { |
| 67 | // started by pop(), this catches modal dialogs |
| 68 | _waitTimer.stopTimer(); |
| 69 | // closing that do work afterwards |
| 70 | return super.getNextEvent(); |
| 71 | } |
| 72 | |
| 73 | public void trigger() |
| 74 | { |
| 75 | _cursorManager.setCursor(); |
| 76 | } |
| 77 | |
| 78 | private final CursorManager _cursorManager; |
| 79 | private final DelayTimer _waitTimer; |
| 80 | } |