Source code: com/simscomputing/testbed/gui/ClassNameDialog.java
1 package com.simscomputing.testbed.gui;
2
3 import com.simscomputing.swing.SwingUtilities;
4
5 import java.awt.Container;
6 import java.awt.GridBagConstraints;
7 import java.awt.GridBagLayout;
8 import java.awt.Insets;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.ActionListener;
11 import java.awt.event.KeyEvent;
12
13 import javax.swing.JButton;
14 import javax.swing.JDialog;
15 import javax.swing.JFrame;
16 import javax.swing.JLabel;
17 import javax.swing.JPanel;
18 import javax.swing.JTextField;
19
20 /**
21 Gathers a class name from the user.
22
23 @author David Sims
24 @version $Revision: 1.1.1.1 $ $Date: 2000/02/21 21:22:35 $
25 */
26 public class ClassNameDialog extends JDialog {
27 private String className;
28 private final JTextField classNameField = new JTextField("");
29
30 public ClassNameDialog(JFrame parent) {
31 super(parent, true);
32
33 setTitle("Enter class name");
34
35 final Container root = getContentPane();
36 root.setLayout(new GridBagLayout());
37
38 final JLabel contents = new JLabel("Enter a class name to test (for example, com.foo.BarTest)");
39 classNameField.addActionListener(new ActionListener() {
40 public void actionPerformed(final ActionEvent e) {
41 doOkAction();
42 } // actionPerformed()
43 });
44 classNameField.addKeyListener(new java.awt.event.KeyAdapter() {
45 public void keyPressed(final KeyEvent e) {
46 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
47 doCancelAction();
48 } // if
49 } // keyPressed()
50 });
51
52 JButton okButton = new JButton("OK");
53 okButton.addActionListener(new ActionListener() {
54 public void actionPerformed(ActionEvent e) {
55 doOkAction();
56 }
57 });
58 okButton.addKeyListener(new java.awt.event.KeyAdapter() {
59 public void keyPressed(final KeyEvent e) {
60 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
61 doCancelAction();
62 } // if
63 } // keyPressed()
64 });
65
66 JButton cancelButton = new JButton("Cancel");
67 cancelButton.addActionListener(new ActionListener() {
68 public void actionPerformed(final ActionEvent e) {
69 doCancelAction();
70 } // actionPerformed()
71 });
72 cancelButton.addKeyListener(new java.awt.event.KeyAdapter() {
73 public void keyPressed(final KeyEvent e) {
74 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
75 doCancelAction();
76 } // if
77 } // keyPressed()
78 });
79
80 final int INSET = Resources.INSET;
81 final Insets INSETS = Resources.INSETS;
82
83 root.add(classNameField,
84 new GridBagConstraints(0, 1, 2, 1, 1.0, 0.0,
85 GridBagConstraints.CENTER,
86 GridBagConstraints.HORIZONTAL,
87 INSETS,
88 0, 0));
89
90 root.add(contents,
91 new GridBagConstraints(0, 0, 2, 1, 1.0, 0.0,
92 GridBagConstraints.CENTER,
93 GridBagConstraints.HORIZONTAL,
94 INSETS,
95 0, 0));
96
97 JPanel okCancelPanel = new JPanel();
98 okCancelPanel.setLayout(new GridBagLayout());
99
100 root.add(okCancelPanel,
101 new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0,
102 GridBagConstraints.CENTER,
103 GridBagConstraints.HORIZONTAL,
104 INSETS,
105 0, 0));
106
107 okCancelPanel.add(okButton,
108 new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
109 GridBagConstraints.CENTER,
110 GridBagConstraints.HORIZONTAL,
111 INSETS,
112 0, 0));
113 okCancelPanel.add(cancelButton,
114 new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
115 GridBagConstraints.CENTER,
116 GridBagConstraints.HORIZONTAL,
117 INSETS,
118 0, 0));
119
120 pack();
121
122 SwingUtilities.centerOnParent(this, parent);
123 } // constructor
124
125 private void doOkAction() {
126 className = classNameField.getText();
127 if (className.length() > 0) {
128 dispose();
129 } // if
130 } // doOkAction()
131
132 private void doCancelAction() {
133 dispose();
134 } // doOkAction()
135
136 public String getClassName() {
137 return className;
138 } // getClassName()
139 } // class ClassNameDialog