1 package com.eviware.soapui.support.swing;
2
3 import java.awt.Frame;
4 import java.awt.event.WindowAdapter;
5 import java.awt.event.WindowEvent;
6 import java.lang.reflect.InvocationHandler;
7 import java.lang.reflect.Method;
8 import java.lang.reflect.Proxy;
9
10 import javax.swing.JFrame;
11
12
13 public class ModalFrameUtil
14 {
15 static class EventPump implements InvocationHandler
16 {
17 Frame frame;
18
19 public EventPump( Frame frame )
20 {
21 this.frame = frame;
22 }
23
24 public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
25 {
26 return frame.isShowing();
27 }
28
29
30
31 public void start() throws Exception
32 {
33 Class<?> clazz = Class.forName( "java.awt.Conditional" );
34 Object conditional = Proxy.newProxyInstance( clazz.getClassLoader(), new Class[] { clazz }, this );
35 Method pumpMethod = Class.forName( "java.awt.EventDispatchThread" ).getDeclaredMethod( "pumpEvents",
36 new Class[] { clazz } );
37 pumpMethod.setAccessible( true );
38 pumpMethod.invoke( Thread.currentThread(), new Object[] { conditional } );
39 }
40 }
41
42
43
44 public static void showAsModal( final Frame frame, final Frame owner )
45 {
46 frame.addWindowListener( new WindowAdapter()
47 {
48 public void windowOpened( WindowEvent e )
49 {
50 owner.setEnabled( false );
51 }
52
53 public void windowClosing( WindowEvent e )
54 {
55 owner.setEnabled( true );
56 frame.removeWindowListener( this );
57 }
58
59 public void windowClosed( WindowEvent e )
60 {
61 owner.setEnabled( true );
62 frame.removeWindowListener( this );
63 }
64 } );
65
66 owner.addWindowListener( new WindowAdapter()
67 {
68 public void windowActivated( WindowEvent e )
69 {
70 if( frame.isShowing() )
71 {
72 frame.setExtendedState( JFrame.NORMAL );
73 frame.toFront();
74 }
75 else
76 {
77 owner.removeWindowListener( this );
78 }
79 }
80 } );
81
82 frame.setVisible( true );
83 try
84 {
85 new EventPump( frame ).start();
86 }
87 catch( Throwable throwable )
88 {
89 throw new RuntimeException( throwable );
90 }
91 }
92 }