| 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.Component; |
| 21 | import java.awt.Cursor; |
| 22 | import java.awt.MenuComponent; |
| 23 | import java.awt.MenuContainer; |
| 24 | |
| 25 | import javax.swing.SwingUtilities; |
| 26 | |
| 27 | class DispatchedEvent |
| 28 | { |
| 29 | public DispatchedEvent(Object source) |
| 30 | { |
| 31 | _source = source; |
| 32 | } |
| 33 | |
| 34 | public void setCursor() |
| 35 | { |
| 36 | synchronized (_mutex) |
| 37 | { |
| 38 | _parent = findVisibleParent(); |
| 39 | if (_parent != null) |
| 40 | { |
| 41 | _lastCursor = (_parent.isCursorSet() ? _parent.getCursor() : null); |
| 42 | _parent.setCursor(WAIT_CURSOR); |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public boolean resetCursor() |
| 48 | { |
| 49 | synchronized (_mutex) |
| 50 | { |
| 51 | if (_parent != null) |
| 52 | { |
| 53 | _parent.setCursor(_lastCursor); |
| 54 | _parent = null; |
| 55 | return true; |
| 56 | } |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | private Component findVisibleParent() |
| 62 | { |
| 63 | Component result = null; |
| 64 | if (_source instanceof Component) |
| 65 | { |
| 66 | result = SwingUtilities.getRoot((Component) _source); |
| 67 | } |
| 68 | else if (_source instanceof MenuComponent) |
| 69 | { |
| 70 | MenuContainer mParent = ((MenuComponent) _source).getParent(); |
| 71 | if (mParent instanceof Component) |
| 72 | { |
| 73 | result = SwingUtilities.getRoot((Component) mParent); |
| 74 | } |
| 75 | } |
| 76 | if ((result != null) && result.isVisible()) |
| 77 | { |
| 78 | return result; |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | return null; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | static private final Cursor WAIT_CURSOR = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR); |
| 87 | |
| 88 | private final Object _mutex = new Object(); |
| 89 | private final Object _source; |
| 90 | private Component _parent; |
| 91 | private Cursor _lastCursor; |
| 92 | } |