
package org.avmdti.sound.example;

import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.JFileChooser;

import org.avmdti.sound.AudioFormatFactory;
import org.avmdti.sound.SoundRecorder;

/*
 * GUIDemoSoundRecorder.java
 *
 * Created on 27 janvier 2004, 09:04
 */

/**
 *
 * @author  mercier
 */
public class GUIDemoSoundRecorder extends javax.swing.JFrame {
    /**
	 * 
	 */
	private static final long serialVersionUID = 3175588711372297398L;
	SoundRecorder SR=null;
    /** Creates new form GUIDemoSoundRecorder */
    public GUIDemoSoundRecorder() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldFileName = new javax.swing.JTextField();
        jButtonFileSelect = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jButtonStart = new javax.swing.JButton();
        jButtonStop = new javax.swing.JButton();
        jButtonExit = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        helpMenu = new javax.swing.JMenu();
        contentsMenuItem = new javax.swing.JMenuItem();
        aboutMenuItem = new javax.swing.JMenuItem();

        setTitle("JOSAST Demo Sound Recorder");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel3.setToolTipText("null");
        jLabel1.setText("Filename (.wav)");
        jPanel3.add(jLabel1);

        jTextFieldFileName.setText("demo.wav");
        jTextFieldFileName.setMinimumSize(new java.awt.Dimension(4, 50));
        jTextFieldFileName.setPreferredSize(new java.awt.Dimension(200, 20));
        jPanel3.add(jTextFieldFileName);

        jButtonFileSelect.setText("...");
        jButtonFileSelect.setToolTipText("File selection");
  
  		jButtonFileSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonFileSelectActionPerformed(evt);
            }
        });
        jPanel3.add(jButtonFileSelect);

        jPanel2.add(jPanel3, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel2, java.awt.BorderLayout.NORTH);

        jButtonStart.setText("Start");
        jButtonStart.setToolTipText("Begin to record sound");
        jButtonStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonStartActionPerformed(evt);
            }
        });

        jPanel4.add(jButtonStart);

        jButtonStop.setText("Stop");
        jButtonStop.setToolTipText("Stop record sound");
        jButtonStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonStopActionPerformed(evt);
            }
        });

        jPanel4.add(jButtonStop);

        jButtonExit.setText("Exit");
        jButtonExit.setToolTipText("exit demo");
        jButtonExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonExitActionPerformed(evt);
            }
        });

        jPanel4.add(jButtonExit);

        jPanel1.add(jPanel4, java.awt.BorderLayout.SOUTH);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        helpMenu.setText("Help");
        contentsMenuItem.setText("Contents");
        helpMenu.add(contentsMenuItem);

        aboutMenuItem.setText("About");
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setJMenuBar(menuBar);
 		jButtonStop.setEnabled(false);
 		
        pack();
    }//GEN-END:initComponents


	/**
	 * Method jButtonFileSelectActionPerformed.
	 * @param evt
	 */
	private void jButtonFileSelectActionPerformed(ActionEvent evt) {

		// Add your handling code here:
		JFileChooser jFileChooserkeps = new JFileChooser();
		// Filtre pour le FileChooser
		// JFileChooser jFileChooserkeps = null;
		MyFileFilter filter = new MyFileFilter();
		//    filter.addExtension("jpg");
		filter.addExtension("wav");
		filter.setDescription("WAV File ");
		jFileChooserkeps.setFileFilter(filter);

		if (JFileChooser.APPROVE_OPTION
			== jFileChooserkeps.showOpenDialog(this)) {
			File f = jFileChooserkeps.getSelectedFile();
			jTextFieldFileName.setText(f.getAbsolutePath());

		} //GEN-LAST:event_jButtonLocalFileSelectionActionPerformed
	
	}

    private void jButtonExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExitActionPerformed
        // Add your handling code here:
          	if (SR!= null)
      		SR.close();
           System.exit(0);
    }//GEN-LAST:event_jButtonExitActionPerformed

    private void jButtonStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStopActionPerformed
        // Add your handling code here:
        jButtonStop.setEnabled(false);
        jButtonStart.setEnabled(true);
        SR.endRecord();
        
    }//GEN-LAST:event_jButtonStopActionPerformed

    private void jButtonStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStartActionPerformed
        // Add your handling code here:
               jButtonStop.setEnabled(true);
        jButtonStart.setEnabled(false);
        SR = new SoundRecorder(jTextFieldFileName.getText(),AudioFormatFactory.getAudioFormat(AudioFormatFactory.MONO8BITPCM));
 		SR.startRecord();
    }//GEN-LAST:event_jButtonStartActionPerformed
        
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
      	if (SR!= null)
      		SR.close();
        System.exit(0);
    }//GEN-LAST:event_exitForm
    
    /**
     * @param args the command line arguments
     */

	@SuppressWarnings("deprecation")
	public static void main(String args[]) {
        new GUIDemoSoundRecorder().show();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JButton jButtonExit;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jButtonFileSelect;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JButton jButtonStop;
    private javax.swing.JMenuItem contentsMenuItem;
    private javax.swing.JTextField jTextFieldFileName;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton jButtonStart;
    private javax.swing.JMenu helpMenu;
    // End of variables declaration//GEN-END:variables
    
}
