001package horstmann.ch05_mailgui;
002import java.awt.BorderLayout;
003import java.awt.GridLayout;
004import javax.swing.JButton;
005import javax.swing.JFrame;
006import javax.swing.JLabel;
007import javax.swing.JPanel;
008import javax.swing.JTextArea;
009
010/**
011   Presents a phone GUI for the voice mail system.
012 */
013public class Telephone
014{
015        /**
016      Constructs a telephone with a speaker, keypad,
017      and microphone.
018         */
019        public Telephone()
020        {
021                JPanel speakerPanel = new JPanel();
022                speakerPanel.setLayout(new BorderLayout());
023                speakerPanel.add(new JLabel("Speaker:"),
024                                BorderLayout.NORTH);
025                speakerField = new JTextArea(10, 25);
026                speakerPanel.add(speakerField,
027                                BorderLayout.CENTER);
028
029                String keyLabels = "123456789*0#";
030                JPanel keyPanel = new JPanel();
031                keyPanel.setLayout(new GridLayout(4, 3));
032                for (int i = 0; i < keyLabels.length(); i++)
033                {
034                        final String label = keyLabels.substring(i, i + 1);
035                        JButton keyButton = new JButton(label);
036                        keyPanel.add(keyButton);
037                        keyButton.addActionListener(event -> connect.dial(label));
038                }
039
040                final JTextArea microphoneField = new JTextArea(10,25);
041
042                JButton speechButton = new JButton("Send speech");
043                speechButton.addActionListener(event -> {
044                        connect.record(microphoneField.getText());
045                        microphoneField.setText("");
046                });
047
048                JButton hangupButton = new JButton("Hangup");
049                hangupButton.addActionListener(event -> connect.hangup());
050
051                JPanel buttonPanel = new JPanel();
052                buttonPanel.add(speechButton);
053                buttonPanel.add(hangupButton);
054
055                JPanel microphonePanel = new JPanel();
056                microphonePanel.setLayout(new BorderLayout());
057                microphonePanel.add(new JLabel("Microphone:"),
058                                BorderLayout.NORTH);
059                microphonePanel.add(microphoneField, BorderLayout.CENTER);
060                microphonePanel.add(buttonPanel, BorderLayout.SOUTH);
061
062                JFrame frame = new JFrame();
063                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
064                frame.add(speakerPanel, BorderLayout.NORTH);
065                frame.add(keyPanel, BorderLayout.CENTER);
066                frame.add(microphonePanel, BorderLayout.SOUTH);
067
068                frame.pack();
069                frame.setVisible(true);
070        }
071
072        /**
073      Give instructions to the mail system user.
074         */
075        public void speak(String output)
076        {
077                speakerField.setText(output);
078        }
079
080        public void run(Connection c)
081        {
082                connect = c;
083        }
084
085        private JTextArea speakerField;
086        private Connection connect;
087}