OW2 Consortium jaspte

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
6 duanzhiquan 1
/**
2
 * =========================================================================
3
 * 					Bench4Q version 1.2.1
4
 * =========================================================================
5
 *
6
 * Bench4Q is available on the Internet at http://forge.ow2.org/projects/jaspte
7
 * You can find latest version there.
8
 *
9
 * Distributed according to the GNU Lesser General Public Licence.
10
 * This library is free software; you can redistribute it and/or modify it
11
 * under the terms of the GNU Lesser General Public License as published by
12
 * the Free Software Foundation; either version 2.1 of the License, or any
13
 * later version.
14
 *
15
 * SEE Copyright.txt FOR FULL COPYRIGHT INFORMATION.
16
 *
17
 * This source code is distributed "as is" in the hope that it will be
18
 * useful.  It comes with no warranty, and no author or distributor
19
 * accepts any responsibility for the consequences of its use.
20
 *
21
 *
22
 * This version is a based on the implementation of TPC-W from University of Wisconsin.
23
 * This version used some source code of The Grinder.
24
 *
25
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
26
 *  * Initial developer(s): Zhiquan Duan.
27
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
28
 *
29
 */
30
 
31
package org.bench4Q.console.ui;
32
 
33
import java.awt.BorderLayout;
34
import java.awt.Container;
35
import java.awt.Cursor;
36
import java.awt.event.WindowAdapter;
37
import java.awt.event.WindowEvent;
38
import java.beans.PropertyChangeEvent;
39
import java.beans.PropertyChangeListener;
40
 
41
import javax.swing.JDialog;
42
import javax.swing.JFrame;
43
import javax.swing.JOptionPane;
44
 
45
/**
46
 * <code>JDialog</code> that is more useful than that returned by
47
 * <code>JOptionPane.createDialog()</code>.
48
 */
49
public class JOptionPaneDialog extends JDialog {
50
 
51
	/**
52
	 * Constructor.
53
	 *
54
	 * @param frame
55
	 *            Parent frame.
56
	 * @param title
57
	 *            The title.
58
	 * @param modal
59
	 *            <code>true</code> => dialog should be modal.
60
	 * @param optionPane
61
	 *            JOptionPane to wrap.
62
	 */
63
	public JOptionPaneDialog(JFrame frame, String title, boolean modal,
64
			JOptionPane optionPane) {
65
 
66
		super(frame, title, modal);
67
		setOptionPane(optionPane);
68
	}
69
 
70
	/**
71
	 * Constructor.
72
	 *
73
	 * @param dialog
74
	 *            Parent dialog.
75
	 * @param title
76
	 *            The title.
77
	 * @param modal
78
	 *            <code>true</code> => dialog should be modal.
79
	 * @param optionPane
80
	 *            JOptionPane to wrap.
81
	 */
82
	public JOptionPaneDialog(JDialog dialog, String title, boolean modal,
83
			JOptionPane optionPane) {
84
 
85
		super(dialog, title, modal);
86
		setOptionPane(optionPane);
87
	}
88
 
89
	/**
90
	 * Common initialistion. We need separate constructors because JDialog does
91
	 * not allow us to treat parentComponent polymorphically.
92
	 *
93
	 * @param optionPane
94
	 *            JOptionPane to wrap.
95
	 */
96
	private void setOptionPane(final JOptionPane optionPane) {
97
 
98
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
99
 
100
		final Container contentPane = getContentPane();
101
		contentPane.setLayout(new BorderLayout());
102
		contentPane.add(optionPane, BorderLayout.CENTER);
103
		pack();
104
		setLocationRelativeTo(getOwner());
105
 
106
		addWindowListener(new WindowAdapter() {
107
			private boolean m_gotFocus = false;
108
 
109
			public void windowClosing(WindowEvent e) {
110
				optionPane.setValue(null);
111
			}
112
 
113
			public void windowActivated(WindowEvent e) {
114
				// Once window gets focus, set initial focus
115
				if (!m_gotFocus) {
116
					optionPane.selectInitialValue();
117
					m_gotFocus = true;
118
				}
119
			}
120
		});
121
 
122
		optionPane.addPropertyChangeListener(new PropertyChangeListener() {
123
 
124
			private boolean m_disable = false;
125
 
126
			public void propertyChange(PropertyChangeEvent e) {
127
				if (isVisible()
128
						&& e.getSource() == optionPane
129
						&& !m_disable
130
						&& (e.getPropertyName().equals(
131
								JOptionPane.VALUE_PROPERTY) || e
132
								.getPropertyName().equals(
133
										JOptionPane.INPUT_VALUE_PROPERTY))) {
134
 
135
					final Cursor oldCursor = getCursor();
136
					setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
137
 
138
					try {
139
						if (shouldClose()) {
140
							setVisible(false);
141
							dispose();
142
						}
143
					} finally {
144
						m_disable = true;
145
						optionPane.setValue(null);
146
						m_disable = false;
147
						setCursor(oldCursor);
148
					}
149
				}
150
			}
151
		});
152
 
153
		optionPane.setValue(null);
154
	}
155
 
156
	/**
157
	 * Whether dialog should be closed on a property change.
158
	 *
159
	 * @return <code>true</code> => it should.
160
	 */
161
	protected boolean shouldClose() {
162
		return true;
163
	}
164
}