001    package view;
002    
003    import javax.swing.*;
004    import java.awt.*;
005    import java.awt.event.ActionEvent;
006    import java.awt.event.ActionListener;
007    
008    /**
009     * Dialog to enter a string.
010     *
011     * @author Mathias Ricken
012     */
013    public class InputStringDialog {
014        private JDialog _dialog;
015        private JButton[] _optButtons;
016        private JTextField _textField;
017        private boolean _cancelled;
018    
019        /**
020         * Creates a new input string dialog and all its controls.
021         *
022         * @param parent parent dialog for the dialog
023         * @param title  window title
024         */
025        public InputStringDialog(JDialog parent, String title) {
026            _dialog = new JDialog(parent, title, true);
027            _dialog.setContentPane(makeOptionsPanel());
028            _dialog.pack();
029            _dialog.setResizable(false);
030        }
031    
032        /**
033         * Creates a new input string dialog and all its controls.
034         *
035         * @param parent parent frame for the dialog
036         * @param title  window title
037         */
038        public InputStringDialog(Frame parent, String title) {
039            _dialog = new JDialog(parent, title, true);
040            _dialog.setContentPane(makeOptionsPanel());
041            _dialog.pack();
042            _dialog.setResizable(false);
043        }
044    
045        /**
046         * Show the modal dialog.
047         *
048         * @return contents of text field
049         */
050        public String showDialog() {
051            // center dialog in middle of parent frame
052            _cancelled = false;
053            Component parent = _dialog.getParent();
054            _dialog.setLocation(parent.getX() + parent.getWidth() / 2 - _dialog.getSize().width / 2,
055                parent.getY() + parent.getHeight() / 2 - _dialog.getSize().height / 2);
056            _dialog.setVisible(true); // Modal dialog will block until setVisible(false), see ok/cancel methods
057    
058            return (_cancelled)?null:_textField.getText();
059        }
060    
061        // callback when user clicks cancel button
062        private void cancelClicked() {
063            _textField.setText("");
064            _cancelled = true;
065            _dialog.setVisible(false);
066        }
067    
068        // callback when user clicks ok button
069        private void okClicked() {
070            _dialog.setVisible(false);
071        }
072    
073        /**
074         * Build options dialog.
075         *
076         * @return options pane
077         */
078        private JOptionPane makeOptionsPanel() {
079            JPanel myControls = new JPanel();
080            myControls.setLayout(new BoxLayout(myControls, BoxLayout.Y_AXIS));
081    
082            _textField = new JTextField();
083            myControls.add(_textField);
084    
085            JOptionPane optPane = new JOptionPane(myControls, JOptionPane.QUESTION_MESSAGE);
086            _optButtons = new JButton[]{new JButton("Create"), new JButton("Cancel")};
087            optPane.setOptions(_optButtons);
088            optPane.setInitialValue(_optButtons[0]);
089            _optButtons[1].addActionListener(new ActionListener() {
090                public void actionPerformed(ActionEvent e) {
091                    cancelClicked();
092                }
093            });
094            _optButtons[0].addActionListener(new ActionListener() {
095                public void actionPerformed(ActionEvent e) {
096                    okClicked();
097                }
098            });
099    
100            return optPane;
101        }
102    }
103