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