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