]>
Commit | Line | Data |
---|---|---|
878ddf1f | 1 | /** @file\r |
2 | Java class ModalFrameUtil is used to show modal frame.\r | |
3 | \r | |
4 | Copyright (c) 2006, Intel Corporation\r | |
5 | All rights reserved. This program and the accompanying materials\r | |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | **/\r | |
13 | package org.tianocore.packaging;\r | |
14 | \r | |
15 | import javax.swing.*;\r | |
16 | import java.awt.*;\r | |
17 | import java.awt.event.WindowAdapter;\r | |
18 | import java.awt.event.WindowEvent;\r | |
19 | import java.lang.reflect.InvocationHandler;\r | |
20 | import java.lang.reflect.Method;\r | |
21 | import java.lang.reflect.Proxy;\r | |
22 | \r | |
23 | /**\r | |
24 | This class is used to show modal frame.\r | |
25 | \r | |
26 | @since PackageEditor 1.0\r | |
27 | **/\r | |
28 | public class ModalFrameUtil {\r | |
29 | /**\r | |
30 | Invocation handler for event threads\r | |
31 | \r | |
32 | @since PackageEditor 1.0\r | |
33 | **/\r | |
34 | static class EventPump implements InvocationHandler {\r | |
35 | Frame frame;\r | |
36 | \r | |
37 | public EventPump(Frame frame) {\r | |
38 | this.frame = frame;\r | |
39 | }\r | |
40 | \r | |
41 | /**\r | |
42 | Invocation handler invoked by Method.invoke\r | |
43 | **/\r | |
44 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {\r | |
45 | //\r | |
46 | // return frame showing status for Conditional.evaluate()\r | |
47 | //\r | |
48 | return frame.isShowing() ? Boolean.TRUE : Boolean.FALSE;\r | |
49 | }\r | |
50 | \r | |
51 | public void start() throws Exception {\r | |
52 | Class clazz = Class.forName("java.awt.Conditional");\r | |
53 | //\r | |
54 | // Conditional proxy instance will invoke "this" InvocationHandler.invoke when calling its methods\r | |
55 | //\r | |
56 | Object conditional = Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz }, this);\r | |
57 | //\r | |
58 | // EventDisaptchThread.pumpEvents will be called under Conditional "conditional"\r | |
59 | //\r | |
60 | Method pumpMethod = Class.forName("java.awt.EventDispatchThread").getDeclaredMethod("pumpEvents",\r | |
61 | new Class[] { clazz });\r | |
62 | pumpMethod.setAccessible(true);\r | |
63 | //\r | |
64 | // pumpEvents when conditional.evaluate() == true (frame.isShowing() in EventPump.invoke)\r | |
65 | //\r | |
66 | pumpMethod.invoke(Thread.currentThread(), new Object[] { conditional });\r | |
67 | }\r | |
68 | }\r | |
69 | \r | |
70 | /**\r | |
71 | Show modal frame, return only when frame closed.\r | |
72 | \r | |
73 | @param frame Frame to be modal\r | |
74 | @param owner Parent Frame\r | |
75 | **/\r | |
76 | public static void showAsModal(final Frame frame, final Frame owner) {\r | |
77 | frame.addWindowListener(new WindowAdapter() {\r | |
78 | public void windowOpened(WindowEvent e) {\r | |
79 | owner.setEnabled(false);\r | |
80 | }\r | |
81 | \r | |
82 | public void windowClosed(WindowEvent e) {\r | |
83 | owner.setEnabled(true);\r | |
84 | owner.setVisible(true);\r | |
85 | owner.removeWindowListener(this);\r | |
86 | }\r | |
87 | });\r | |
88 | \r | |
89 | owner.addWindowListener(new WindowAdapter() {\r | |
90 | public void windowActivated(WindowEvent e) {\r | |
91 | if (frame.isShowing()) {\r | |
92 | frame.setExtendedState(JFrame.NORMAL);\r | |
93 | frame.toFront();\r | |
94 | } else {\r | |
95 | owner.removeWindowListener(this);\r | |
96 | }\r | |
97 | }\r | |
98 | });\r | |
99 | \r | |
100 | frame.setVisible(true);\r | |
101 | try {\r | |
102 | new EventPump(frame).start();\r | |
103 | } catch (Throwable throwable) {\r | |
104 | throw new RuntimeException(throwable);\r | |
105 | }\r | |
106 | }\r | |
107 | }\r |