KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > key > passwd > swing > WindowUtils


1 /*
2  * WindowUtils.java
3  *
4  * Created on 15. Dezember 2006, 18:42
5  */

6 /*
7  * Copyright 2006 Schlichtherle IT Services
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package de.schlichtherle.key.passwd.swing;
23
24 import java.awt.*;
25 import java.beans.*;
26 import java.lang.ref.*;
27
28 import javax.swing.*;
29
30 /**
31  * A utility class for window management.
32  *
33  * @author Christian Schlichtherle
34  * @version @version@
35  * @since TrueZIP 6.4
36  */

37 final class WindowUtils {
38     private static final String JavaDoc PROPERTY_FOCUSED_WINDOW = "focusedWindow";
39
40     private static Reference lastFocusManager = new WeakReference(null);
41     private static Reference lastFocusedWindow = lastFocusManager;
42     private static Reference parent;
43
44     private static PropertyChangeListener focusListener
45             = new PropertyChangeListener() {
46         public void propertyChange(PropertyChangeEvent evt) {
47             Window w = (Window) evt.getNewValue();
48             if (w != null)
49                 lastFocusedWindow = new WeakReference(w);
50         }
51     };
52
53     /**
54      * You can't instantiate this class.
55      * It's a holder for static methods only.
56      */

57     private WindowUtils() {
58     }
59
60     /**
61      * @see PromptingKeyManager#getParentWindow
62      */

63     public static synchronized Window getParentWindow() {
64         Window w = parent != null ? (Window) parent.get() : null;
65         if (w == null) {
66             w = WindowUtils.getLastFocusedWindow();
67             if (w == null)
68                 w = getAnyShowingWindow();
69         }
70
71         // Search the containment hierarchy updwards for the first showing
72
// window.
73
for (Container c = w; c != null; c = c.getParent())
74             if (c instanceof Window && c.isShowing())
75                 return (Window) c;
76
77         // No window is showing, use JOptionPane's default.
78
return JOptionPane.getRootFrame();
79     }
80
81     /**
82      * @see PromptingKeyManager#setParentWindow
83      */

84     public static synchronized void setParentWindow(Window w) {
85         parent = w != null ? new WeakReference(w) : null;
86     }
87
88     /**
89      * Returns the last window which received the focus.
90      * If no window received the focus yet or is already made eligible for
91      * finalization, <code>null</code> is returned instead.
92      * Note that this is <em>not</em> the same as
93      * <code>WindowUtils.getCurrentKeyboardFocusManager().getFocusedWindow()</code>:
94      * The latter may return <code>null</code> if no window in this JVM has
95      * the focus, while this method will return the last window in this JVM
96      * which had the focus (unless this is also the first call to this method).
97      */

98     public static Window getLastFocusedWindow() {
99         observeFocusedWindow();
100         return (Window) lastFocusedWindow.get();
101     }
102
103     /**
104      * Ensures that the focused window managed by the current keyboard focus
105      * manager is observed.
106      */

107     private static synchronized void observeFocusedWindow() {
108         final KeyboardFocusManager lfm
109                 = (KeyboardFocusManager) lastFocusManager.get();
110         final KeyboardFocusManager fm
111                 = KeyboardFocusManager.getCurrentKeyboardFocusManager();
112         if (fm == lfm)
113             return;
114
115         if (lfm != null)
116             lfm.removePropertyChangeListener(
117                     PROPERTY_FOCUSED_WINDOW, focusListener);
118         fm.addPropertyChangeListener(
119                 PROPERTY_FOCUSED_WINDOW, focusListener);
120         lastFocusManager = new WeakReference(fm);
121         lastFocusedWindow = new WeakReference(fm.getFocusedWindow());
122     }
123
124     private static final Window getAnyShowingWindow() {
125         return getAnyShowingWindow(Frame.getFrames());
126     }
127
128     private static Window getAnyShowingWindow(final Window[] windows) {
129         for (int i = 0, l = windows.length; i < l; i++) {
130             Window window = windows[i];
131             if (window.isShowing())
132                 return window;
133
134             window = getAnyShowingWindow(window.getOwnedWindows());
135             if (window != null)
136                 return window;
137         }
138
139         return null;
140     }
141 }
142
Popular Tags