001: package jimm.datavision.gui;
002:
003: import jimm.util.I18N;
004: import java.awt.BorderLayout;
005: import java.awt.event.ActionListener;
006: import java.awt.event.ActionEvent;
007: import java.awt.event.WindowAdapter;
008: import java.awt.event.WindowEvent;
009: import javax.swing.*;
010:
011: /**
012: * A modal dialog used to ask the user for a simple string like a
013: * formula or parameter name.
014: *
015: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
016: */
017: public class AskStringDialog extends JDialog implements ActionListener {
018:
019: protected static final int TEXT_FIELD_COLUMNS = 24;
020:
021: protected String string;
022: protected JTextField stringField;
023:
024: /**
025: * Constructor.
026: *
027: * @param frame parent frame
028: * @param title window title
029: * @param label string field label
030: */
031: public AskStringDialog(java.awt.Frame frame, String title,
032: String label) {
033: this (frame, title, label, "");
034: }
035:
036: /**
037: * Constructor.
038: *
039: * @param frame parent frame
040: * @param title window title
041: * @param label string field label
042: * @param initialString initial string value
043: */
044: public AskStringDialog(java.awt.Frame frame, String title,
045: String label, String initialString) {
046: super ((java.awt.Frame) null, title, true); // Modal
047: buildWindow(title, label, initialString);
048: pack();
049: setVisible(true);
050: }
051:
052: /**
053: * Returns string (or <code>null</code> if user hit Cancel).
054: *
055: * @return string (or <code>null</code> if user cancelled)
056: */
057: public String getString() {
058: return string;
059: }
060:
061: /**
062: * Builds window GUI.
063: *
064: * @param title window title
065: * @param labelString string field label
066: * @param initialString initial value of string
067: */
068: protected void buildWindow(String title, String labelString,
069: String initialString) {
070: getContentPane().setLayout(new BorderLayout());
071:
072: EditFieldLayout efl = new EditFieldLayout();
073: stringField = efl.addTextField(labelString, initialString,
074: TEXT_FIELD_COLUMNS);
075:
076: // OK and Cancel buttons
077: JPanel buttonPanel = new JPanel();
078: JButton button;
079:
080: buttonPanel.add(button = new JButton(I18N.get("GUI.ok")));
081: button.addActionListener(this );
082: button.setDefaultCapable(true);
083: getRootPane().setDefaultButton(button);
084:
085: buttonPanel.add(button = new JButton(I18N.get("GUI.cancel")));
086: button.addActionListener(this );
087:
088: getContentPane().add(efl.getPanel(), BorderLayout.CENTER);
089: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
090:
091: addWindowListener(new WindowAdapter() {
092: public void windowClosing(WindowEvent e) {
093: dispose();
094: }
095: });
096:
097: new FocusSetter(stringField);
098: }
099:
100: /**
101: * Handles the buttons.
102: *
103: * @param e action event
104: */
105: public void actionPerformed(ActionEvent e) {
106: String cmd = e.getActionCommand();
107: if (I18N.get("GUI.ok").equals(cmd)) {
108: string = new String(stringField.getText());
109: dispose();
110: } else if (I18N.get("GUI.cancel").equals(cmd)) {
111: dispose();
112: }
113: }
114:
115: }
|