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 | package net.sourceforge.hivegui.component; |
16 | |
17 | import java.awt.AWTEvent; |
18 | import java.awt.Toolkit; |
19 | import java.awt.Window; |
20 | import java.awt.event.AWTEventListener; |
21 | import java.awt.event.WindowEvent; |
22 | |
23 | import org.apache.commons.logging.Log; |
24 | import org.apache.commons.logging.LogFactory; |
25 | |
26 | /** |
27 | * @author Jean-Francois Poilpret |
28 | */ |
29 | final public class ActiveWindowHolder implements AWTEventListener |
30 | { |
31 | static final private Log _logger = LogFactory.getLog(ActiveWindowHolder.class); |
32 | |
33 | // Call this method once before showing any window |
34 | static synchronized public void init() |
35 | { |
36 | if (_instance == null) |
37 | { |
38 | _instance = new ActiveWindowHolder(); |
39 | } |
40 | } |
41 | |
42 | static public ActiveWindowHolder instance() |
43 | { |
44 | if (_instance == null) |
45 | { |
46 | init(); |
47 | } |
48 | return _instance; |
49 | } |
50 | |
51 | public Window getActiveWindow() |
52 | { |
53 | return _current; |
54 | } |
55 | |
56 | private ActiveWindowHolder() |
57 | { |
58 | _logger.debug("<init>"); |
59 | _current = null; |
60 | Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.WINDOW_EVENT_MASK); |
61 | } |
62 | |
63 | public void eventDispatched(AWTEvent e) |
64 | { |
65 | switch (e.getID()) |
66 | { |
67 | case WindowEvent.WINDOW_ACTIVATED: |
68 | _current = ((WindowEvent) e).getWindow(); |
69 | break; |
70 | |
71 | case WindowEvent.WINDOW_DEACTIVATED: |
72 | _current = null; |
73 | break; |
74 | |
75 | default: |
76 | // Do nothing (we are only interested in activation events) |
77 | break; |
78 | } |
79 | } |
80 | |
81 | private Window _current; |
82 | static private ActiveWindowHolder _instance; |
83 | } |