package gui;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import gui.*;
import gui.run.*;
import audio.*;
public class JCapture extends JFrame implements AudioPlayerListener{
    AudioPlayer player = new AudioPlayer();
    JTextField audioFormatField = new JTextField();
    JPanel buttonBar = new JPanel();
    JTextField stateField = new JTextField(10);
    File currentDirectory;
    
    
    public JCapture() {
        super("Audio Capture Tool");
        player.addAudioPlayerListener(this);
        buttonBar.setLayout(new FlowLayout());
        buttonBar.add(new RunButton("Capture") {
            public void run() {
                try{
                    player.captureAudio();
                    updateUIStrings();
                }catch (AudioPlayerException a){
                    messageBox(a.getMessage());
                }
            }
        });
        buttonBar.add(new RunButton("Stop") {
            public void run() {
                player.stop();
            }
        });
        buttonBar.add(new RunButton("Play") {
            public void run() {
                try{
                    player.playAudio();
                }catch (AudioPlayerException a){
                    messageBox(a.getMessage());
                }
            }
        });
        buttonBar.add(new RunButton("Save") {
            public void run() {
                try{
                    AudioFileChooser afc = new AudioFileChooser(currentDirectory);
                    afc.setDialogType(AudioFileChooser.SAVE_DIALOG);
                    afc.setSourceFormat(player.getFormat());
                    afc.show();
                    if (afc.getSelectedFile() != null){
                        player.saveAudioFile(afc.getSelectedFile(), 
                                             afc.getSelectedEncoding());
                        currentDirectory = afc.getCurrentDirectory();
                        updateUIStrings();
                    }
                }catch (AudioPlayerException a){
                    messageBox(a.getMessage());
                }
            }
        });
        buttonBar.add(new RunButton("Open") {
            public void run() {
                try{
                    AudioFileChooser afc = new AudioFileChooser(currentDirectory);
                    afc.setDialogType(AudioFileChooser.OPEN_DIALOG);
                    afc.show();
                    if (afc.getSelectedFile() != null){
                        player.openAudioFile(afc.getSelectedFile());
                        currentDirectory = afc.getCurrentDirectory();
                        updateUIStrings();
                    }
                }catch (AudioPlayerException a){
                    messageBox(a.getMessage());
                }
            }
        });
        buttonBar.add(stateField);
        stateField.setEditable(false);
        audioFormatField.setEditable(false);
        Container contentPane = getContentPane();
        
        contentPane.setLayout(new BorderLayout());
        contentPane.add(buttonBar, BorderLayout.NORTH);
        contentPane.add(audioFormatField, BorderLayout.SOUTH);
         
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    public void audioStateChanged(AudioPlayer.State s){
        stateField.setText(s.toString());
    }
    
    
    private void messageBox(String s){
        JOptionPane.showMessageDialog(this, s, "Audio Capture Tool", JOptionPane.WARNING_MESSAGE);
    }
    
    
    private void updateUIStrings(){
        setTitle("Audio Capture Tool - " + player.getAudioDescription());
        audioFormatField.setText(player.getFormat().toString());
    }
    
    
    
    public static void main(String args[]) {
        JCapture c = new JCapture();
        c.setSize(500, 200);
        c.show();
    }
}