1 package com.eviware.soapui.support.swing;
2
3 import java.awt.BorderLayout;
4 import java.awt.FlowLayout;
5 import java.awt.Frame;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8
9 import javax.swing.JButton;
10 import javax.swing.JFrame;
11 import javax.swing.JLabel;
12 import javax.swing.JPanel;
13 import javax.swing.JTextField;
14 import javax.swing.SwingConstants;
15 import javax.swing.UIManager;
16 import javax.swing.border.EmptyBorder;
17
18 import com.eviware.soapui.support.UISupport;
19
20 /***
21 * Workaround because JOptionPane doesn't work well with ModalFrameUtil; if a
22 * JOptionPane is displayed, the modal frame is temporarily hidden.
23 *
24 * @author lars
25 */
26 public class ModalFrameDialog
27 {
28 private JFrame frame;
29 private JTextField textField;
30 private JButton okButton;
31 private JButton cancelButton;
32
33 private String retValue = null;
34
35 public static String showInputDialog( Frame parent, String question, String title, String initialValue )
36 {
37 ModalFrameDialog dialog = new ModalFrameDialog( question, title, initialValue );
38 dialog.show( parent );
39 return dialog.retValue;
40 }
41
42 private ModalFrameDialog( String question, String title, String initialValue )
43 {
44 textField = new JTextField( initialValue, 20 );
45 if( initialValue != null )
46 {
47 textField.setSelectionStart( 0 );
48 textField.setSelectionEnd( initialValue.length() );
49 }
50
51 JPanel buttonPanel = new JPanel( new FlowLayout() );
52 okButton = new JButton( "OK" );
53 cancelButton = new JButton( "Cancel" );
54 buttonPanel.add( okButton );
55 buttonPanel.add( cancelButton );
56
57 JLabel iconLabel = new JLabel( UIManager.getIcon( "OptionPane.questionIcon" ) );
58 iconLabel.setVerticalAlignment( SwingConstants.TOP );
59 iconLabel.setBorder( new EmptyBorder( 0, 0, 0, 10 ) );
60
61 JPanel outerPanel = new JPanel( new BorderLayout() );
62 JPanel innerPanel = new JPanel( new BorderLayout() );
63 outerPanel.add( iconLabel, BorderLayout.WEST );
64 outerPanel.add( innerPanel, BorderLayout.CENTER );
65 outerPanel.setBorder( new EmptyBorder( 10, 10, 10, 10 ) );
66
67 innerPanel.add( new JLabel( question ), BorderLayout.NORTH );
68 innerPanel.add( textField, BorderLayout.CENTER );
69 innerPanel.add( buttonPanel, BorderLayout.SOUTH );
70
71 frame = new JFrame( title );
72 frame.getContentPane().add( outerPanel );
73 frame.getRootPane().setDefaultButton( okButton );
74 frame.pack();
75
76 okButton.addActionListener( new ActionListener()
77 {
78 public void actionPerformed( ActionEvent e )
79 {
80 close( textField.getText() );
81 }
82 } );
83
84 cancelButton.addActionListener( new ActionListener()
85 {
86 public void actionPerformed( ActionEvent e )
87 {
88 close( null );
89 }
90 } );
91 }
92
93 private void show( Frame parent )
94 {
95 UISupport.centerDialog( frame, parent );
96 ModalFrameUtil.showAsModal( frame, parent );
97 }
98
99 private void close( String retValue )
100 {
101 this.retValue = retValue;
102 frame.setVisible( false );
103 frame.dispose();
104 }
105 }