Guest User

Java Panel's Problem

a guest
Sep 4th, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 51.59 KB | None | 0 0
  1. /*
  2.  * RecorderUI.java
  3.  *
  4.  * Created on __DATE__, __TIME__
  5.  */
  6.  
  7. package com.actura.app.capture;
  8.  
  9. import java.awt.Color;
  10. import java.awt.Dimension;
  11. import java.awt.Graphics;
  12. import java.awt.Graphics2D;
  13. import java.awt.LinearGradientPaint;
  14. import java.awt.event.ActionEvent;
  15. import java.awt.event.ActionListener;
  16. import java.awt.geom.Point2D;
  17.  
  18. import java.io.File;
  19.  
  20. import java.io.IOException;
  21.  
  22. import javax.swing.Timer;
  23.  
  24. import java.util.ArrayList;
  25. import java.util.HashMap;
  26. import java.util.List;
  27.  
  28. import javax.sound.sampled.AudioSystem;
  29. import javax.sound.sampled.AudioFileFormat;
  30. import javax.sound.sampled.AudioFormat;
  31. import javax.sound.sampled.AudioInputStream;
  32. import javax.sound.sampled.DataLine;
  33. import javax.sound.sampled.LineUnavailableException;
  34. import javax.sound.sampled.SourceDataLine;
  35.  
  36. import javax.sound.sampled.UnsupportedAudioFileException;
  37. import javax.swing.BorderFactory;
  38.  
  39. import javax.swing.JLabel;
  40. import javax.swing.JPanel;
  41.  
  42. import org.apache.log4j.Logger;
  43. import com.actura.app.util.ApplicationUtil;
  44.  
  45. /**
  46.  *
  47.  * @author __USER__
  48.  */
  49. public class RecorderUI extends javax.swing.JApplet implements Runnable {
  50.  
  51.     /*
  52.      * state management constants
  53.      */
  54.     private static final int NORMAL_STATE = 0;
  55.     private static final int RECORDING_STATE = 1;
  56.     private static final int LISTENING_STATE = 2;
  57.     private static final int SAVING_STATE = 3;
  58.     private static final int RE_RECORDING_STATE = 4;
  59.     private static final int STAGE1_STATE = 5;
  60.     private static final int STAGE2_STATE = 6;
  61.     private int currentCurrentState = NORMAL_STATE;
  62.  
  63.     /**
  64.      * recording related constants and variables
  65.      */
  66.     private static int MAXIMUM_RECORD_TIME = 120;
  67.     private int currentSecond = 1;
  68.  
  69.     private static Logger log = Logger.getLogger(RecorderUI.class
  70.             .getSimpleName());
  71.  
  72.     /**
  73.      *
  74.      */
  75.     private static final long serialVersionUID = 1685325421893969802L;
  76.  
  77.     private ActuraRecorder captureThread = null;
  78.  
  79.     private PCMFilePlayer playerThread = null;
  80.  
  81.     private Thread timerThread = null;
  82.  
  83.     private HashMap<String, String> parameterMap = new HashMap<String, String>();
  84.  
  85.     private File fileUnderProcessing = null;
  86.  
  87.     private int messageCount = 0;
  88.  
  89.     /** Initializes the applet RecorderUI */
  90.     public void init() {
  91.  
  92.         try {
  93.             initParameters();
  94.             java.awt.EventQueue.invokeAndWait(new Runnable() {
  95.                 public void run() {
  96.                     initDimension();
  97.                     initGraphPanel();
  98.                     initCombo();
  99.                     initComponents();
  100.                 }
  101.             });
  102.             initActuraRecorder();
  103.         } catch (Exception ex) {
  104.             ex.printStackTrace();
  105.             // TODO disable all command buttons
  106.         }
  107.     }
  108.  
  109.     public void initActuraRecorder() {
  110.  
  111.         try {
  112.             java.awt.EventQueue.invokeAndWait(new Runnable() {
  113.                 public void run() {
  114.                     currentCurrentState = NORMAL_STATE;
  115.                     reRecordingPanel.setVisible(false);
  116.                     saveImageButton.setVisible(false);
  117.                     listeningPanel.setVisible(false);
  118.                 }
  119.             });
  120.         } catch (Exception ex) {
  121.             ex.printStackTrace();
  122.         }
  123.  
  124.     }
  125.  
  126.     public void initCombo() {
  127.         try {
  128.             // do nothing
  129.         } catch (Exception ex) {
  130.             ex.printStackTrace();
  131.         }
  132.     }
  133.  
  134.     public void initGraphPanel() {
  135.         errorLabel = new JLabel("");
  136.     }
  137.  
  138.     public void initDimension() {
  139.         // TODO implement this
  140.     }
  141.  
  142.     public void initParameters() {
  143.  
  144.         try {
  145.  
  146.             // prepare server data
  147.             ApplicationUtil.prepareServerData(this, parameterMap);
  148.  
  149.             // prepare count
  150.             setMessageCount(Integer.parseInt(parameterMap
  151.                     .get(ApplicationUtil.IVR_MESSAGECOUNT_KEY)));
  152.  
  153.             // set current file to be processed
  154.             this.setFileUnderProcessing(ApplicationUtil.generateFile(
  155.                     getMessageCount(), parameterMap));
  156.  
  157.         } catch (IOException e) {
  158.             e.printStackTrace();
  159.             this.paintError(e.getMessage(), ApplicationUtil.ERROR);
  160.         } catch (ClassNotFoundException e) {
  161.             e.printStackTrace();
  162.             this.paintError(e.getMessage(), ApplicationUtil.ERROR);
  163.         } catch (Exception e) {
  164.             e.printStackTrace();
  165.             this.paintError(e.getMessage(), ApplicationUtil.ERROR);
  166.         }
  167.     }
  168.  
  169.     public void incrementMessageCount() {
  170.         this.messageCount++;
  171.     }
  172.  
  173.     private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {
  174.         stopPlayer();
  175.     }
  176.  
  177.     private void recordButtonActionPerformed(java.awt.event.ActionEvent evt) {
  178.  
  179.         int currentCurrentState = getCurrentState();
  180.  
  181.         if (currentCurrentState == RecorderUI.RECORDING_STATE) {
  182.             stopRecording(true);
  183.         } else {
  184.             startRecording();
  185.         }
  186.  
  187.     }
  188.  
  189.     public void startRecording() {
  190.  
  191.         // initialize timer
  192.         initializeTimer();
  193.         // set currentCurrentState
  194.         setCurrentState(RecorderUI.RECORDING_STATE);
  195.  
  196.         // start Recording
  197.         captureThread = new ActuraRecorder(RecorderUtility
  198.                 .getDefaultTragetDataLine(), AudioFileFormat.Type.WAVE, this
  199.                 .getFileUnderProcessing());
  200.  
  201.         captureThread.start();
  202.         // start Timer
  203.         startTimer();
  204.         // start Graph
  205.         startDrawingGraph();
  206.         // set text to stop
  207.         recordButton.setText("Stop");
  208.     }
  209.  
  210.     public void stopRecording(boolean isSwitchPanel) {
  211.         // stop recording
  212.         captureThread.stop();
  213.  
  214.         // set currentCurrentState
  215.         setCurrentState(RecorderUI.NORMAL_STATE);
  216.         // stop timer
  217.         stopTimer();
  218.         // stop Graph Drawing
  219.         stopDrawingGraph();
  220.         // set text to stop
  221.         recordButton.setText("Record");
  222.  
  223.         if (isSwitchPanel) {
  224.             recordingPanel.setVisible(false);
  225.             reRecordingPanel.setVisible(true);
  226.         }
  227.  
  228.     }
  229.  
  230.     public void stopRecordingFromTimer() {
  231.  
  232.         // stop capture thread
  233.         captureThread.stop();
  234.  
  235.         // set currentCurrentState
  236.         setCurrentState(RecorderUI.NORMAL_STATE);
  237.         // stop Graph Drawing
  238.         stopDrawingGraph();
  239.         // set text to stop
  240.         recordButton.setText("Record");
  241.  
  242.         recordingPanel.setVisible(false);
  243.         reRecordingPanel.setVisible(true);
  244.  
  245.         initializeTimer();
  246.  
  247.     }
  248.  
  249.     public void startReRecording() {
  250.  
  251.         // initialize timer
  252.         initializeTimer();
  253.         // set currentCurrentState
  254.         setCurrentState(RecorderUI.RE_RECORDING_STATE);
  255.  
  256.         // start Recording
  257.         captureThread = new ActuraRecorder(RecorderUtility
  258.                 .getDefaultTragetDataLine(), AudioFileFormat.Type.WAVE, this
  259.                 .getFileUnderProcessing());
  260.  
  261.         captureThread.start();
  262.         // start Timer
  263.         startTimer();
  264.         // start Graph
  265.         startDrawingGraph();
  266.         // set text to stop
  267.         this.reRecordingButton.setText("Stop");
  268.     }
  269.  
  270.     public void stopReRecording() {
  271.  
  272.         // stop recording
  273.         captureThread.stop();
  274.  
  275.         // set currentCurrentState
  276.         setCurrentState(RecorderUI.NORMAL_STATE);
  277.         // stop timer
  278.         stopTimer();
  279.         // stop Graph Drawing
  280.         stopDrawingGraph();
  281.         // set text to stop
  282.         reRecordingButton.setText("Record");
  283.  
  284.     }
  285.  
  286.     public void stopReRecordingFromTimer() {
  287.  
  288.         // stop capture thread
  289.         captureThread.stop();
  290.  
  291.         // set currentCurrentState
  292.         setCurrentState(RecorderUI.NORMAL_STATE);
  293.         // stop Graph Drawing
  294.         stopDrawingGraph();
  295.         // set text to stop
  296.         reRecordingButton.setText("Record");
  297.  
  298.         initializeTimer();
  299.     }
  300.  
  301.     public void stopListening() {
  302.  
  303.         if (getCurrentState() == RecorderUI.LISTENING_STATE) {
  304.  
  305.             // switch panel
  306.             this.timerPanel.setVisible(true);
  307.             this.listeningPanel.setVisible(false);
  308.  
  309.             // switch button
  310.             this.reRecordingButton.setEnabled(true);
  311.             this.saveButton.setEnabled(true);
  312.  
  313.             this.listenButton.setText("Listen");
  314.  
  315.             this.setCurrentState(NORMAL_STATE);
  316.  
  317.             // according to player's state wise
  318.             this.closePlayer();
  319.  
  320.             log.info(" player closed ");
  321.  
  322.         } else {
  323.  
  324.             // switch panel
  325.             this.timerPanel.setVisible(false);
  326.             this.listeningPanel.setVisible(true);
  327.  
  328.             log.info(" visibility of listeningPanel is"
  329.                     + this.listeningPanel.isVisible());
  330.  
  331.             // switch button
  332.             this.reRecordingButton.setEnabled(false);
  333.             this.saveButton.setEnabled(false);
  334.  
  335.             this.listenButton.setText("Close");
  336.  
  337.             this.setCurrentState(LISTENING_STATE);
  338.  
  339.             this.startPlayer();
  340.  
  341.             log.info(" now playing ");
  342.         }
  343.  
  344.     }
  345.  
  346.     public void cancelSaving() {
  347.  
  348.     }
  349.  
  350.     public void initializeTimer() {
  351.         this.currentSecond = 0;
  352.         paintTimerLabel(0);
  353.     }
  354.  
  355.     public void startTimer() {
  356.         initializeTimer();
  357.         timerThread = new Thread(this);
  358.         timerThread.start();
  359.     }
  360.  
  361.     public void stopTimer() {
  362.  
  363.         try {
  364.  
  365.             if (timerThread.isAlive()) {
  366.  
  367.                 timerThread.join();
  368.                 initializeTimer();
  369.  
  370.                 timerThread.interrupt();
  371.                 timerThread = null;
  372.             }
  373.  
  374.             if (timerThread != null) {
  375.                 timerThread.join();
  376.                 initializeTimer();
  377.                 timerThread.interrupt();
  378.                 timerThread = null;
  379.             }
  380.         } catch (InterruptedException iex) {
  381.             iex.printStackTrace();
  382.         }
  383.     }
  384.  
  385.     public void startDrawingGraph() {
  386.         ((WaveformPanel) graphPanel).setAnimation(true);
  387.     }
  388.  
  389.     public void stopDrawingGraph() {
  390.  
  391.         ((WaveformPanel) graphPanel).setAnimation(false);
  392.     }
  393.  
  394.     class WaveformPanel extends JPanel {
  395.  
  396.         Timer graphTimer = null;
  397.         AudioInfo helper = null;
  398.  
  399.         WaveformPanel() {
  400.             setPreferredSize(new Dimension(200, 80));
  401.             setBorder(BorderFactory.createLineBorder(Color.BLACK));
  402.             graphTimer = new Timer(481, new TimerDrawing());
  403.         }
  404.  
  405.         /**
  406.          *
  407.          */
  408.         private static final long serialVersionUID = 969991141812736791L;
  409.         protected final Color BACKGROUND_COLOR = Color.white;
  410.         protected final Color REFERENCE_LINE_COLOR = Color.black;
  411.         protected final Color WAVEFORM_COLOR = Color.red;
  412.  
  413.         private int offset = 0;
  414.         private int readLen = 1000;
  415.         private byte[] bytesRecorded = null; // new byte[readLen];
  416.         private int increment = 0;
  417.  
  418.         protected void paintComponent(Graphics g) {
  419.  
  420.             super.paintComponent(g);
  421.  
  422.             int lineHeight = getHeight() / 2;
  423.             g.setColor(REFERENCE_LINE_COLOR);
  424.             setBackground(Color.white);
  425.             g.drawLine(0, lineHeight, (int) getWidth(), lineHeight);
  426.  
  427.             if (helper == null) {
  428.                 return;
  429.             }
  430.  
  431.             drawWaveform(g, helper.getByteArray());
  432.  
  433.         }
  434.  
  435.         public int getIncrement(int[] samples, int width) {
  436.  
  437.             int pixelToPrint = Math.round(samples.length / 8);
  438.  
  439.             // System.out.println(" pixelToPrint " + pixelToPrint + " "+
  440.             // (samples[0].length / 8));
  441.  
  442.             double xScale = ((double) width / (double) pixelToPrint);
  443.  
  444.             // System.out.println(" xScale " + xScale + " "
  445.             // +(width/pixelToPrint));
  446.  
  447.             int increment = (int) Math.round(width / (width * xScale));
  448.  
  449.             increment = increment / 2;
  450.  
  451.             // System.out.println(" increment " + increment + " length "
  452.             // + samples.length);
  453.  
  454.             return increment;
  455.         }
  456.  
  457.         protected void drawWaveform(Graphics g, int[][] samples) {
  458.  
  459.             // Samples is a 2D int array (int[][]), where the first index is the
  460.             // channel, the second is the sample for that channel
  461.             if (samples != null) {
  462.  
  463.                 Graphics2D g2d = (Graphics2D) g;
  464.  
  465.                 int max = helper.sampleMax;
  466.                 int min = helper.sampleMin;
  467.  
  468.                 int length = samples[0].length;
  469.  
  470.                 int width = getWidth() - 1;
  471.                 int height = getHeight() - 1;
  472.  
  473.                 int frame = 0;
  474.                 int oldX = 0;
  475.                 int oldY = height / 2;
  476.  
  477.                 // min, max is the min/max range of the samples, ie the highest
  478.                 // and lowest samples
  479.                 int range = max + (min * -2);
  480.                 float scale = (float) height / (float) range;
  481.  
  482.                 int minY = Math.round(((height / 2) + (min * scale)));
  483.                 int maxY = Math.round(((height / 2) + (max * scale)));
  484.  
  485.                 LinearGradientPaint lgp = new LinearGradientPaint(
  486.                         new Point2D.Float(0, minY), new Point2D.Float(0, maxY),
  487.                         new float[] { 0f, 0.5f, 1f }, new Color[] { Color.BLUE,
  488.                                 Color.RED, Color.BLUE });
  489.                 g2d.setPaint(lgp);
  490.  
  491.                 int counter = 0;
  492.  
  493.                 if (increment == 0) {
  494.                     increment = getIncrement(samples[0], width);
  495.                 }
  496.  
  497.                 for (int i = 0; i < samples[0].length; i++) {
  498.  
  499.                     int sample = samples[0][i];
  500.  
  501.                     if (sample % 8 == 0) {
  502.  
  503.                         int x = Math.round(((float) frame / (float) length)
  504.                                 * width);
  505.                         int y = Math.round((height / 2) + (sample * scale));
  506.  
  507.                         g2d.drawLine(oldX, oldY, x, y);
  508.  
  509.                         if (x >= width) {
  510.                             x = 0;
  511.                             frame = 0;
  512.  
  513.                         }
  514.  
  515.                         oldX = x;
  516.                         oldY = y;
  517.  
  518.                     }
  519.  
  520.                     frame++;
  521.  
  522.                 }
  523.  
  524.             }
  525.  
  526.         }
  527.  
  528.         public void setAnimation(boolean turnon) {
  529.             if (turnon) {
  530.                 graphTimer.start();
  531.             } else {
  532.                 graphTimer.stop();
  533.                 helper = null;
  534.                 repaint();
  535.             }
  536.         }
  537.  
  538.         class TimerDrawing implements ActionListener {
  539.  
  540.             @Override
  541.             public void actionPerformed(ActionEvent e) {
  542.  
  543.                 // captureThread.readRecorderByte(bytesRecorded, offset,
  544.                 // readLen);
  545.                 bytesRecorded = captureThread.getTempBuffer();
  546.  
  547.                 if (helper != null) {
  548.                     helper.setBytes(bytesRecorded);
  549.                 } else {
  550.                     helper = new AudioInfo(bytesRecorded);
  551.                 }
  552.                 repaint();
  553.             }
  554.         }
  555.  
  556.     }
  557.  
  558.     private void listenButtonActionPerformed(java.awt.event.ActionEvent evt) {
  559.  
  560.         stopListening();
  561.     }
  562.  
  563.     public int getCurrentState() {
  564.         return currentCurrentState;
  565.     }
  566.  
  567.     public void setCurrentState(int currentCurrentState) {
  568.         this.currentCurrentState = currentCurrentState;
  569.     }
  570.  
  571.     public int getMessageCount() {
  572.         return messageCount;
  573.     }
  574.  
  575.     public void setMessageCount(int messageCount) {
  576.         this.messageCount = messageCount;
  577.     }
  578.  
  579.     public File getFileUnderProcessing() {
  580.         return fileUnderProcessing;
  581.     }
  582.  
  583.     public void setFileUnderProcessing(File fileUnderProcessing) {
  584.         this.fileUnderProcessing = fileUnderProcessing;
  585.     }
  586.  
  587.     @Override
  588.     public void run() {
  589.  
  590.         int recordingLimitInSeconds = RecorderUI.MAXIMUM_RECORD_TIME;
  591.  
  592.         while (getCurrentState() == RecorderUI.RECORDING_STATE
  593.                 || getCurrentState() == RecorderUI.RE_RECORDING_STATE) {
  594.  
  595.             recordingLimitInSeconds--;
  596.             currentSecond++;
  597.             // paint label
  598.             paintTimerLabel(currentSecond);
  599.  
  600.             try {
  601.                 Thread.sleep(1000);
  602.             } catch (InterruptedException iex) {
  603.                 iex.printStackTrace();
  604.             }
  605.  
  606.             if (recordingLimitInSeconds == 0) {
  607.                 // stop recording
  608.                 if (getCurrentState() == RecorderUI.RECORDING_STATE) {
  609.                     stopRecordingFromTimer();
  610.                     // paintTimerLabel(0);
  611.                     break;
  612.                 } else if (getCurrentState() == RecorderUI.RE_RECORDING_STATE) {
  613.                     stopReRecordingFromTimer();
  614.                     // paintTimerLabel(0);
  615.                     break;
  616.                 } else {
  617.                     // do nothing
  618.                 }
  619.             }
  620.  
  621.         }
  622.     }
  623.  
  624.     public void paintTimerLabel(int seconds) {
  625.  
  626.         if (seconds == 0) {
  627.             // secondsLabel.setText("   ");
  628.             timerLabel.setText("000");
  629.         }
  630.  
  631.         if (seconds == 1) {
  632.             secondsLabel.setText("Sec ");
  633.         } else {
  634.             secondsLabel.setText("Secs");
  635.         }
  636.  
  637.         if (seconds > 0 && seconds < 10) {
  638.             timerLabel.setText(("00" + seconds));
  639.         } else if (seconds > 9 && seconds < 100) {
  640.             timerLabel.setText(("0" + seconds));
  641.         } else if (seconds > 99 && seconds < 121) {
  642.             timerLabel.setText(("" + seconds));
  643.         } else {
  644.             // do nothing
  645.         }
  646.     }
  647.  
  648.     /***************************************************************/
  649.     /* Player */
  650.     /**************************************************************/
  651.  
  652.     class PCMFilePlayer implements Runnable {
  653.  
  654.         private File fileToPlay = null;
  655.         private AudioInputStream in = null;
  656.         private SourceDataLine line;
  657.         private int frameSize = 0;
  658.         private byte[] buffer = new byte[32 * 1024]; // 32k is arbitrary
  659.         private Thread playThread = null;
  660.         private boolean playing = false;
  661.         private boolean notYetEOF = true;
  662.         private boolean isPaused = false;
  663.         private PlayerTimer playerTimerThread = null;
  664.  
  665.         PCMFilePlayer(File fileToPlay) throws IOException,
  666.                 UnsupportedAudioFileException, LineUnavailableException {
  667.  
  668.             this.fileToPlay = fileToPlay;
  669.  
  670.             // log.info(" PCM file " + this.fileToPlay.getAbsolutePath());
  671.  
  672.             in = javax.sound.sampled.AudioSystem
  673.                     .getAudioInputStream(this.fileToPlay);
  674.  
  675.             // validate audio file format
  676.             AudioFormat format = in.getFormat();
  677.             AudioFormat.Encoding formatEncoding = format.getEncoding();
  678.  
  679.             if (!(formatEncoding.equals(AudioFormat.Encoding.PCM_SIGNED) || formatEncoding
  680.                     .equals(AudioFormat.Encoding.PCM_SIGNED))) {
  681.                 throw new UnsupportedAudioFileException(this.fileToPlay
  682.                         + " is not a PCM Audio !,so can not play.");
  683.             }
  684.  
  685.             log.info(" PCM Format");
  686.  
  687.             frameSize = format.getFrameSize();
  688.  
  689.             DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
  690.  
  691.             log.info(" got info ");
  692.  
  693.             line = (SourceDataLine) AudioSystem.getLine(info);
  694.  
  695.             log.info(" got source line");
  696.  
  697.             line.open();
  698.  
  699.             log.info(" line opened ...");
  700.  
  701.             playerTimerThread = new PlayerTimer(this.fileToPlay);
  702.  
  703.         }
  704.  
  705.         public void startPCMPlayer() {
  706.  
  707.             if (isPaused) {
  708.                 resumePCMPlayer();
  709.                 log.info(" resumed....");
  710.             } else {
  711.                 playing = true;
  712.                 notYetEOF = true;
  713.                 playThread = new Thread(this);
  714.                 if (!playThread.isAlive()) {
  715.                     playThread.start();
  716.                     playerTimerThread.start();
  717.                 }
  718.                 line.start();
  719.             }
  720.  
  721.         }
  722.  
  723.         public void resumePCMPlayer() {
  724.             isPaused = false;
  725.             playing = true;
  726.         }
  727.  
  728.         public void stopPCMPlayer() throws InterruptedException {
  729.  
  730.             playing = false;
  731.             isPaused = false;
  732.             notYetEOF = false;
  733.  
  734.             if (!playThread.isAlive()) {
  735.                 playThread.interrupt();
  736.             } else {
  737.                 playThread.join();
  738.                 playThread.interrupt();
  739.             }
  740.  
  741.             log.info(" playThread is stopped......");
  742.  
  743.             if (!playerTimerThread.isAlive()) {
  744.                 playerTimerThread.interrupt();
  745.             } else {
  746.                 playerTimerThread.join();
  747.                 playerTimerThread.interrupt();
  748.             }
  749.  
  750.             log.info(" playTimerThread is stopped......");
  751.  
  752.             playerTimerThread = null;
  753.  
  754.             line.stop();
  755.         }
  756.  
  757.         public void pausePCMPlayer() throws InterruptedException {
  758.             isPaused = true;
  759.             playing = false;
  760.         }
  761.  
  762.         public boolean isPaused() {
  763.             return isPaused;
  764.         }
  765.  
  766.         @Override
  767.         public void run() {
  768.  
  769.             int readPoint = 0;
  770.             int bytesRead = 0;
  771.  
  772.             try {
  773.  
  774.                 while (notYetEOF) {
  775.                     if (playing) {
  776.                         bytesRead = in.read(buffer, readPoint, buffer.length
  777.                                 - readPoint);
  778.  
  779.                         if (bytesRead == -1) {
  780.                             notYetEOF = false;
  781.                             log.info(" reached end of file");
  782.                             break;
  783.                         }
  784.  
  785.                         // how many frames did we get ,
  786.                         // how many are left over
  787.                         int frames = bytesRead / frameSize;
  788.                         int leftOver = bytesRead % frameSize;
  789.                         // send to line
  790.                         line.write(buffer, readPoint, bytesRead - leftOver);
  791.                         // save the left over bytes
  792.                         System
  793.                                 .arraycopy(buffer, bytesRead, buffer, 0,
  794.                                         leftOver);
  795.  
  796.                         readPoint = leftOver;
  797.                     }// ends if playing
  798.                     else {
  799.                         // if not playing
  800.                         try {
  801.                             Thread.sleep(10);
  802.                         } catch (InterruptedException iex) {
  803.                             iex.printStackTrace();
  804.                         }
  805.                     }
  806.                 }// ends while
  807.                 log.info(" reached of ");
  808.                 line.drain();
  809.                 line.stop();
  810.                 playing = false;
  811.             } catch (IOException ioe) {
  812.                 ioe.printStackTrace();
  813.             } finally {
  814.                 line.close();
  815.             }
  816.         }
  817.  
  818.         public File getFileToPlay() {
  819.             return fileToPlay;
  820.         }
  821.  
  822.         public void setFileToPlay(File fileToPlay) {
  823.             this.fileToPlay = fileToPlay;
  824.         }
  825.  
  826.         public SourceDataLine getLine() {
  827.             return line;
  828.         }
  829.  
  830.         public void setLine(SourceDataLine line) {
  831.             this.line = line;
  832.         }
  833.  
  834.         class PlayerTimer extends Thread {
  835.  
  836.             private File file = null;
  837.             private int counter = 0;
  838.  
  839.             public PlayerTimer(File file) {
  840.                 this.file = file;
  841.             }
  842.  
  843.             public void initializePlayerTimer() {
  844.                 playButton.setEnabled(true);
  845.                 stopButton.setEnabled(false);
  846.                 pauseButton.setEnabled(false);
  847.                 playerProgressbar.setValue(0);
  848.                 playerTimerLabel.setText("00:00");
  849.             }
  850.  
  851.             public void run() {
  852.  
  853.                 paintTotalTimeLabel();
  854.  
  855.                 try {
  856.  
  857.                     while (playing || isPaused) {
  858.  
  859.                         if (isPaused) {
  860.                             Thread.sleep(10);
  861.                         } else {
  862.                             playerProgressbar.setValue(counter);
  863.                             paintPlayerLabel(counter, false);
  864.                             Thread.sleep(1000);
  865.  
  866.                             counter++;
  867.  
  868.                         }
  869.  
  870.                     }
  871.  
  872.                     log.info(" Finished Playing ");
  873.  
  874.                     initializePlayerTimer();
  875.  
  876.                 } catch (InterruptedException iex) {
  877.                     iex.printStackTrace();
  878.                 }
  879.             } // ends run
  880.  
  881.             public void paintPlayerLabel(int second, boolean paintTotal) {
  882.  
  883.                 String labelString = null;
  884.  
  885.                 if (second == 0) {
  886.                     return;
  887.                 }
  888.  
  889.                 // only minute
  890.                 if (second % 60 == 0) {
  891.  
  892.                     int minute = second / 60;
  893.  
  894.                     if (minute < 10) {
  895.                         labelString = "0" + minute + ":00";
  896.                     } else {
  897.                         labelString = minute + ":00";
  898.                     }
  899.                 } else if (second > 60) {
  900.  
  901.                     int minute = (int) Math.floor(second / 60);
  902.  
  903.                     if (minute < 10) {
  904.                         labelString = "0" + minute;
  905.                     } else {
  906.                         labelString = minute + "";
  907.                     }
  908.  
  909.                     int remainingSecond = Math.abs(second - (minute * 60));
  910.  
  911.                     if (remainingSecond < 10) {
  912.                         labelString += (":0" + remainingSecond);
  913.                     } else {
  914.                         labelString += (":" + remainingSecond);
  915.                     }
  916.  
  917.                 } else {
  918.                     labelString = "00:"
  919.                             + ((second < 10) ? ("0" + second) : second);
  920.                 }
  921.  
  922.                 if (paintTotal) {
  923.                     totalTimeLabel.setText(labelString);
  924.                 } else {
  925.                     playerTimerLabel.setText(labelString);
  926.                 }
  927.  
  928.             }// ends label painting
  929.  
  930.             public double getDurationOfWavInSeconds(File file) {
  931.                 AudioInputStream stream = null;
  932.  
  933.                 try {
  934.                     stream = AudioSystem.getAudioInputStream(file);
  935.  
  936.                     AudioFormat format = stream.getFormat();
  937.  
  938.                     return file.length() / format.getSampleRate()
  939.                             / (format.getSampleSizeInBits() / 8.0)
  940.                             / format.getChannels();
  941.                 } catch (Exception e) {
  942.                     // log an error
  943.                     return -1;
  944.                 } finally {
  945.                     try {
  946.                         stream.close();
  947.                     } catch (Exception ex) {
  948.                     }
  949.                 }
  950.             }
  951.  
  952.             public void paintTotalTimeLabel() {
  953.  
  954.                 double fileInSeconds = getDurationOfWavInSeconds(this.file);
  955.  
  956.                 int seconds = (int) Math.floor(fileInSeconds);
  957.  
  958.                 paintPlayerLabel(seconds, true);
  959.  
  960.                 playerProgressbar.setMinimum(0);
  961.                 playerProgressbar.setMaximum(seconds);
  962.             }
  963.  
  964.         } // ends inner class
  965.  
  966.     } // ends player
  967.  
  968.     public void startPlayer() {
  969.  
  970.         try {
  971.  
  972.             if (playerThread != null && playerThread.isPaused()) {
  973.                 // keep this object
  974.             } else {
  975.                 // create new object
  976.                 playerThread = new PCMFilePlayer(this.getFileUnderProcessing());
  977.                 log.info(" new instance of player is created : ");
  978.             }
  979.  
  980.             playerThread.startPCMPlayer();
  981.             playButton.setEnabled(false);
  982.             stopButton.setEnabled(true);
  983.             pauseButton.setEnabled(true);
  984.             log.info(" playing ... ");
  985.         } catch (IOException e) {
  986.             e.printStackTrace();
  987.         } catch (UnsupportedAudioFileException e) {
  988.             e.printStackTrace();
  989.         } catch (LineUnavailableException e) {
  990.             e.printStackTrace();
  991.         }
  992.     }
  993.  
  994.     public void stopPlayer() {
  995.         try {
  996.             playerThread.stopPCMPlayer();
  997.             playerThread = null;
  998.             playButton.setEnabled(true);
  999.             stopButton.setEnabled(false);
  1000.             pauseButton.setEnabled(false);
  1001.             log.info(" stopped ... ");
  1002.         } catch (InterruptedException e) {
  1003.             e.printStackTrace();
  1004.         }
  1005.     }
  1006.  
  1007.     public void pausePlayer() {
  1008.         try {
  1009.             playerThread.pausePCMPlayer();
  1010.             playButton.setEnabled(true);
  1011.             stopButton.setEnabled(false);
  1012.             pauseButton.setEnabled(false);
  1013.             log.info(" paused ... ");
  1014.         } catch (InterruptedException e) {
  1015.             e.printStackTrace();
  1016.         }
  1017.     }
  1018.  
  1019.     public void initializePlayer() {
  1020.         playButton.setEnabled(true);
  1021.         stopButton.setEnabled(true);
  1022.         pauseButton.setEnabled(true);
  1023.     }
  1024.  
  1025.     public void closePlayer() {
  1026.  
  1027.         if (playerThread.isPaused()) {
  1028.             playerThread.startPCMPlayer();
  1029.         }
  1030.  
  1031.         if (playerThread.playing) {
  1032.             stopPlayer();
  1033.         }
  1034.  
  1035.         initializePlayer();
  1036.     }
  1037.  
  1038.     public void paintError(String message, String type) {
  1039.  
  1040.         errorLabel.setText("");
  1041.  
  1042.         if (type.equals(ApplicationUtil.INFO)) {
  1043.             // blue color
  1044.             errorLabel.setForeground(Color.blue);
  1045.         } else {
  1046.             // red color
  1047.             errorLabel.setForeground(Color.blue);
  1048.         }
  1049.  
  1050.         errorLabel.setText(message);
  1051.     }
  1052.  
  1053.     //GEN-BEGIN:initComponents
  1054.     // <editor-fold defaultstate="collapsed" desc="Generated Code">
  1055.     private void initComponents() {
  1056.  
  1057.         containerPanel = new javax.swing.JPanel();
  1058.         containerFrame = new javax.swing.JPanel();
  1059.         messagePanel = new javax.swing.JPanel();
  1060.         errorLabel = new javax.swing.JLabel();
  1061.         saveImageButton = new javax.swing.JButton();
  1062.         jLayeredPane1 = new javax.swing.JLayeredPane();
  1063.         recordingPanel = new javax.swing.JPanel();
  1064.         recordButton = new javax.swing.JButton();
  1065.         cancelButton = new javax.swing.JButton();
  1066.         reRecordingPanel = new javax.swing.JPanel();
  1067.         reRecordingButton = new javax.swing.JButton();
  1068.         listenButton = new javax.swing.JButton();
  1069.         saveButton = new javax.swing.JButton();
  1070.         reCancelButton = new javax.swing.JButton();
  1071.         jLayeredPane2 = new javax.swing.JLayeredPane();
  1072.         listeningPanel = new javax.swing.JPanel();
  1073.         listeningButtonPanel1 = new javax.swing.JPanel();
  1074.         jLabel3 = new javax.swing.JLabel();
  1075.         playButton = new javax.swing.JButton();
  1076.         pauseButton = new javax.swing.JButton();
  1077.         stopButton = new javax.swing.JButton();
  1078.         totalTimeLabel = new javax.swing.JLabel();
  1079.         playerTimerLabel = new javax.swing.JLabel();
  1080.         playerProgressbar = new javax.swing.JProgressBar();
  1081.         timerPanel = new javax.swing.JPanel();
  1082.         timerLabel = new javax.swing.JLabel();
  1083.         secondsLabel = new javax.swing.JLabel();
  1084.         graphPanel = new WaveformPanel();
  1085.  
  1086.         containerFrame.setBorder(javax.swing.BorderFactory.createTitledBorder(
  1087.                 javax.swing.BorderFactory.createTitledBorder(""),
  1088.                 "Record Message",
  1089.                 javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
  1090.                 javax.swing.border.TitledBorder.DEFAULT_POSITION,
  1091.                 new java.awt.Font("Tahoma", 1, 14)));
  1092.  
  1093.         messagePanel.setBorder(javax.swing.BorderFactory
  1094.                 .createLineBorder(new java.awt.Color(0, 0, 0)));
  1095.  
  1096.         errorLabel.setFont(new java.awt.Font("Verdana", 0, 12));
  1097.         errorLabel.setForeground(new java.awt.Color(255, 0, 51));
  1098.  
  1099.         saveImageButton.setIcon(new javax.swing.ImageIcon(getClass()
  1100.                 .getResource("/com/actura/app/capture/bigrotation2.gif"))); // NOI18N
  1101.         saveImageButton.setBorder(null);
  1102.  
  1103.         javax.swing.GroupLayout messagePanelLayout = new javax.swing.GroupLayout(
  1104.                 messagePanel);
  1105.         messagePanel.setLayout(messagePanelLayout);
  1106.         messagePanelLayout
  1107.                 .setHorizontalGroup(messagePanelLayout
  1108.                         .createParallelGroup(
  1109.                                 javax.swing.GroupLayout.Alignment.LEADING)
  1110.                         .addGroup(
  1111.                                 messagePanelLayout
  1112.                                         .createSequentialGroup()
  1113.                                         .addContainerGap()
  1114.                                         .addComponent(saveImageButton)
  1115.                                         .addPreferredGap(
  1116.                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED,
  1117.                                                 23, Short.MAX_VALUE)
  1118.                                         .addComponent(
  1119.                                                 errorLabel,
  1120.                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
  1121.                                                 277,
  1122.                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
  1123.                                         .addContainerGap()));
  1124.         messagePanelLayout
  1125.                 .setVerticalGroup(messagePanelLayout
  1126.                         .createParallelGroup(
  1127.                                 javax.swing.GroupLayout.Alignment.LEADING)
  1128.                         .addGroup(
  1129.                                 javax.swing.GroupLayout.Alignment.TRAILING,
  1130.                                 messagePanelLayout
  1131.                                         .createSequentialGroup()
  1132.                                         .addContainerGap()
  1133.                                         .addGroup(
  1134.                                                 messagePanelLayout
  1135.                                                         .createParallelGroup(
  1136.                                                                 javax.swing.GroupLayout.Alignment.TRAILING)
  1137.                                                         .addComponent(
  1138.                                                                 errorLabel,
  1139.                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
  1140.                                                                 130,
  1141.                                                                 Short.MAX_VALUE)
  1142.                                                         .addComponent(
  1143.                                                                 saveImageButton))
  1144.                                         .addContainerGap()));
  1145.  
  1146.         jLayeredPane1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
  1147.  
  1148.         recordButton.setText("Record");
  1149.         recordButton.addActionListener(new java.awt.event.ActionListener() {
  1150.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  1151.                 recordButtonActionPerformed(evt);
  1152.             }
  1153.         });
  1154.  
  1155.         cancelButton.setText("Cancel");
  1156.         cancelButton.addActionListener(new java.awt.event.ActionListener() {
  1157.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  1158.                 cancelButtonActionPerformed(evt);
  1159.             }
  1160.         });
  1161.  
  1162.         javax.swing.GroupLayout recordingPanelLayout = new javax.swing.GroupLayout(
  1163.                 recordingPanel);
  1164.         recordingPanel.setLayout(recordingPanelLayout);
  1165.         recordingPanelLayout
  1166.                 .setHorizontalGroup(recordingPanelLayout
  1167.                         .createParallelGroup(
  1168.                                 javax.swing.GroupLayout.Alignment.LEADING)
  1169.                         .addGroup(
  1170.                                 recordingPanelLayout
  1171.                                         .createSequentialGroup()
  1172.                                         .addGap(21, 21, 21)
  1173.                                         .addGroup(
  1174.                                                 recordingPanelLayout
  1175.                                                         .createParallelGroup(
  1176.                                                                 javax.swing.GroupLayout.Alignment.LEADING)
  1177.                                                         .addComponent(
  1178.                                                                 cancelButton)
  1179.                                                         .addComponent(
  1180.                                                                 recordButton))
  1181.                                         .addContainerGap(22, Short.MAX_VALUE)));
  1182.         recordingPanelLayout
  1183.                 .setVerticalGroup(recordingPanelLayout
  1184.                         .createParallelGroup(
  1185.                                 javax.swing.GroupLayout.Alignment.LEADING)
  1186.                         .addGroup(
  1187.                                 recordingPanelLayout
  1188.                                         .createSequentialGroup()
  1189.                                         .addGap(23, 23, 23)
  1190.                                         .addComponent(recordButton)
  1191.                                         .addPreferredGap(
  1192.                                                 javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  1193.                                         .addComponent(cancelButton)
  1194.                                         .addContainerGap(20, Short.MAX_VALUE)));
  1195.  
  1196.         recordingPanel.setBounds(10, 20, 110, 100);
  1197.         jLayeredPane1.add(recordingPanel,
  1198.                 javax.swing.JLayeredPane.DEFAULT_LAYER);
  1199.  
  1200.         reRecordingButton.setText("Re Record");
  1201.         reRecordingButton
  1202.                 .addActionListener(new java.awt.event.ActionListener() {
  1203.                     public void actionPerformed(java.awt.event.ActionEvent evt) {
  1204.                         reRecordingButtonActionPerformed(evt);
  1205.                     }
  1206.                 });
  1207.  
  1208.         listenButton.setText("Listen");
  1209.         listenButton.addActionListener(new java.awt.event.ActionListener() {
  1210.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  1211.                 listenButtonActionPerformed(evt);
  1212.             }
  1213.         });
  1214.  
  1215.         saveButton.setText("Save");
  1216.         saveButton.addActionListener(new java.awt.event.ActionListener() {
  1217.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  1218.                 saveButtonActionPerformed(evt);
  1219.             }
  1220.         });
  1221.  
  1222.         reCancelButton.setText("Cancel");
  1223.         reCancelButton.addActionListener(new java.awt.event.ActionListener() {
  1224.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  1225.                 reCancelButtonActionPerformed(evt);
  1226.             }
  1227.         });
  1228.  
  1229.         javax.swing.GroupLayout reRecordingPanelLayout = new javax.swing.GroupLayout(
  1230.                 reRecordingPanel);
  1231.         reRecordingPanel.setLayout(reRecordingPanelLayout);
  1232.         reRecordingPanelLayout
  1233.                 .setHorizontalGroup(reRecordingPanelLayout
  1234.                         .createParallelGroup(
  1235.                                 javax.swing.GroupLayout.Alignment.LEADING)
  1236.                         .addGroup(
  1237.                                 reRecordingPanelLayout
  1238.                                         .createSequentialGroup()
  1239.                                         .addGap(24, 24, 24)
  1240.                                         .addGroup(
  1241.                                                 reRecordingPanelLayout
  1242.                                                         .createParallelGroup(
  1243.                                                                 javax.swing.GroupLayout.Alignment.LEADING)
  1244.                                                         .addComponent(
  1245.                                                                 listenButton)
  1246.                                                         .addComponent(
  1247.                                                                 reRecordingButton)
  1248.                                                         .addGroup(
  1249.                                                                 reRecordingPanelLayout
  1250.                                                                         .createParallelGroup(
  1251.                                                                                 javax.swing.GroupLayout.Alignment.TRAILING)
  1252.                                                                         .addComponent(
  1253.                                                                                 reCancelButton)
  1254.                                                                         .addComponent(
  1255.                                                                                 saveButton)))
  1256.                                         .addContainerGap(13, Short.MAX_VALUE)));
  1257.         reRecordingPanelLayout
  1258.                 .setVerticalGroup(reRecordingPanelLayout
  1259.                         .createParallelGroup(
  1260.                                 javax.swing.GroupLayout.Alignment.LEADING)
  1261.                         .addGroup(
  1262.                                 reRecordingPanelLayout
  1263.                                         .createSequentialGroup()
  1264.                                         .addContainerGap()
  1265.                                         .addComponent(reRecordingButton)
  1266.                                         .addPreferredGap(
  1267.                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  1268.                                         .addComponent(listenButton)
  1269.                                         .addPreferredGap(
  1270.                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  1271.                                         .addComponent(saveButton)
  1272.                                         .addPreferredGap(
  1273.                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  1274.                                         .addComponent(reCancelButton)
  1275.                                         .addContainerGap(19, Short.MAX_VALUE)));
  1276.  
  1277.         reRecordingPanel.setBounds(10, 10, 120, 140);
  1278.         jLayeredPane1.add(reRecordingPanel,
  1279.                 javax.swing.JLayeredPane.DEFAULT_LAYER);
  1280.  
  1281.         jLayeredPane2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
  1282.  
  1283.         listeningPanel.setBackground(new java.awt.Color(255, 255, 255));
  1284.         listeningPanel.setBorder(javax.swing.BorderFactory
  1285.                 .createBevelBorder(javax.swing.border.BevelBorder.RAISED));
  1286.  
  1287.         listeningButtonPanel1.setBackground(new java.awt.Color(255, 255, 255));
  1288.  
  1289.         jLabel3.setFont(new java.awt.Font("Verdana", 1, 14));
  1290.         jLabel3.setForeground(java.awt.Color.orange);
  1291.         jLabel3.setText("Canary Voice");
  1292.  
  1293.         playButton.setIcon(new javax.swing.ImageIcon(getClass().getResource(
  1294.                 "/com/actura/app/capture/playbutton.PNG"))); // NOI18N
  1295.         playButton.setActionCommand("play");
  1296.         playButton.setBorder(javax.swing.BorderFactory
  1297.                 .createLineBorder(new java.awt.Color(255, 153, 0)));
  1298.         playButton.addActionListener(new java.awt.event.ActionListener() {
  1299.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  1300.                 playButtonActionPerformed(evt);
  1301.             }
  1302.         });
  1303.  
  1304.         pauseButton.setIcon(new javax.swing.ImageIcon(getClass().getResource(
  1305.                 "/com/actura/app/capture/pausebutton.PNG"))); // NOI18N
  1306.         pauseButton.setActionCommand("pause");
  1307.         pauseButton.setBorder(javax.swing.BorderFactory
  1308.                 .createLineBorder(new java.awt.Color(255, 204, 51)));
  1309.         pauseButton.addActionListener(new java.awt.event.ActionListener() {
  1310.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  1311.                 pauseButtonActionPerformed(evt);
  1312.             }
  1313.         });
  1314.  
  1315.         stopButton.setIcon(new javax.swing.ImageIcon(getClass().getResource(
  1316.                 "/com/actura/app/capture/stopbutton.PNG"))); // NOI18N
  1317.         stopButton.setActionCommand("stop");
  1318.         stopButton.setBorder(javax.swing.BorderFactory
  1319.                 .createLineBorder(new java.awt.Color(255, 204, 51)));
  1320.         stopButton.addActionListener(new java.awt.event.ActionListener() {
  1321.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  1322.                 stopButtonActionPerformed(evt);
  1323.             }
  1324.         });
  1325.  
  1326.         totalTimeLabel.setFont(new java.awt.Font("Verdana", 1, 14));
  1327.         totalTimeLabel.setForeground(new java.awt.Color(255, 204, 51));
  1328.         totalTimeLabel.setText("00:00");
  1329.  
  1330.         javax.swing.GroupLayout listeningButtonPanel1Layout = new javax.swing.GroupLayout(
  1331.                 listeningButtonPanel1);
  1332.         listeningButtonPanel1.setLayout(listeningButtonPanel1Layout);
  1333.         listeningButtonPanel1Layout
  1334.                 .setHorizontalGroup(listeningButtonPanel1Layout
  1335.                         .createParallelGroup(
  1336.                                 javax.swing.GroupLayout.Alignment.LEADING)
  1337.                         .addGroup(
  1338.                                 listeningButtonPanel1Layout
  1339.                                         .createSequentialGroup()
  1340.                                         .addGroup(
  1341.                                                 listeningButtonPanel1Layout
  1342.                                                         .createParallelGroup(
  1343.                                                                 javax.swing.GroupLayout.Alignment.LEADING)
  1344.                                                         .addGroup(
  1345.                                                                 listeningButtonPanel1Layout
  1346.                                                                         .createSequentialGroup()
  1347.                                                                         .addGap(
  1348.                                                                                 56,
  1349.                                                                                 56,
  1350.                                                                                 56)
  1351.                                                                         .addComponent(
  1352.                                                                                 jLabel3,
  1353.                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
  1354.                                                                                 116,
  1355.                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
  1356.                                                                         .addPreferredGap(
  1357.                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  1358.                                                                         .addComponent(
  1359.                                                                                 playButton,
  1360.                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
  1361.                                                                                 44,
  1362.                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
  1363.                                                                         .addPreferredGap(
  1364.                                                                                 javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  1365.                                                                         .addComponent(
  1366.                                                                                 pauseButton,
  1367.                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
  1368.                                                                                 40,
  1369.                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
  1370.                                                                         .addPreferredGap(
  1371.                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  1372.                                                                         .addComponent(
  1373.                                                                                 stopButton,
  1374.                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
  1375.                                                                                 32,
  1376.                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE))
  1377.                                                         .addComponent(
  1378.                                                                 totalTimeLabel,
  1379.                                                                 javax.swing.GroupLayout.Alignment.TRAILING,
  1380.                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
  1381.                                                                 67,
  1382.                                                                 javax.swing.GroupLayout.PREFERRED_SIZE))
  1383.                                         .addContainerGap()));
  1384.         listeningButtonPanel1Layout
  1385.                 .setVerticalGroup(listeningButtonPanel1Layout
  1386.                         .createParallelGroup(
  1387.                                 javax.swing.GroupLayout.Alignment.LEADING)
  1388.                         .addGroup(
  1389.                                 listeningButtonPanel1Layout
  1390.                                         .createSequentialGroup()
  1391.                                         .addContainerGap()
  1392.                                         .addGroup(
  1393.                                                 listeningButtonPanel1Layout
  1394.                                                         .createParallelGroup(
  1395.                                                                 javax.swing.GroupLayout.Alignment.LEADING)
  1396.                                                         .addGroup(
  1397.                                                                 listeningButtonPanel1Layout
  1398.                                                                         .createParallelGroup(
  1399.                                                                                 javax.swing.GroupLayout.Alignment.BASELINE)
  1400.                                                                         .addComponent(
  1401.                                                                                 jLabel3)
  1402.                                                                         .addComponent(
  1403.                                                                                 playButton,
  1404.                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
  1405.                                                                                 31,
  1406.                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE))
  1407.                                                         .addGroup(
  1408.                                                                 listeningButtonPanel1Layout
  1409.                                                                         .createParallelGroup(
  1410.                                                                                 javax.swing.GroupLayout.Alignment.TRAILING,
  1411.                                                                                 false)
  1412.                                                                         .addComponent(
  1413.                                                                                 pauseButton,
  1414.                                                                                 javax.swing.GroupLayout.Alignment.LEADING,
  1415.                                                                                 0,
  1416.                                                                                 0,
  1417.                                                                                 Short.MAX_VALUE)
  1418.                                                                         .addComponent(
  1419.                                                                                 stopButton,
  1420.                                                                                 javax.swing.GroupLayout.Alignment.LEADING,
  1421.                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
  1422.                                                                                 31,
  1423.                                                                                 Short.MAX_VALUE)))
  1424.                                         .addPreferredGap(
  1425.                                                 javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  1426.                                         .addComponent(totalTimeLabel)
  1427.                                         .addContainerGap(
  1428.                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
  1429.                                                 Short.MAX_VALUE)));
  1430.  
  1431.         playerTimerLabel.setFont(new java.awt.Font("Arial", 1, 14));
  1432.         playerTimerLabel.setForeground(new java.awt.Color(51, 0, 255));
  1433.         playerTimerLabel.setText("00:00");
  1434.  
  1435.         javax.swing.GroupLayout listeningPanelLayout = new javax.swing.GroupLayout(
  1436.                 listeningPanel);
  1437.         listeningPanel.setLayout(listeningPanelLayout);
  1438.         listeningPanelLayout
  1439.                 .setHorizontalGroup(listeningPanelLayout
  1440.                         .createParallelGroup(
  1441.                                 javax.swing.GroupLayout.Alignment.LEADING)
  1442.                         .addGroup(
  1443.                                 listeningPanelLayout
  1444.                                         .createSequentialGroup()
  1445.                                         .addContainerGap()
  1446.                                         .addGroup(
  1447.                                                 listeningPanelLayout
  1448.                                                         .createParallelGroup(
  1449.                                                                 javax.swing.GroupLayout.Alignment.LEADING)
  1450.                                                         .addGroup(
  1451.                                                                 javax.swing.GroupLayout.Alignment.TRAILING,
  1452.                                                                 listeningPanelLayout
  1453.                                                                         .createSequentialGroup()
  1454.                                                                         .addComponent(
  1455.                                                                                 playerProgressbar,
  1456.                                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
  1457.                                                                                 226,
  1458.                                                                                 Short.MAX_VALUE)
  1459.                                                                         .addGap(
  1460.                                                                                 18,
  1461.                                                                                 18,
  1462.                                                                                 18)
  1463.                                                                         .addComponent(
  1464.                                                                                 playerTimerLabel,
  1465.                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
  1466.                                                                                 50,
  1467.                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
  1468.                                                                         .addGap(
  1469.                                                                                 34,
  1470.                                                                                 34,
  1471.                                                                                 34))
  1472.                                                         .addGroup(
  1473.                                                                 listeningPanelLayout
  1474.                                                                         .createSequentialGroup()
  1475.                                                                         .addComponent(
  1476.                                                                                 listeningButtonPanel1,
  1477.                                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
  1478.                                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
  1479.                                                                                 Short.MAX_VALUE)
  1480.                                                                         .addContainerGap()))));
  1481.         listeningPanelLayout
  1482.                 .setVerticalGroup(listeningPanelLayout
  1483.                         .createParallelGroup(
  1484.                                 javax.swing.GroupLayout.Alignment.LEADING)
  1485.                         .addGroup(
  1486.                                 listeningPanelLayout
  1487.                                         .createSequentialGroup()
  1488.                                         .addContainerGap()
  1489.                                         .addComponent(
  1490.                                                 listeningButtonPanel1,
  1491.                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
  1492.                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
  1493.                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
  1494.                                         .addPreferredGap(
  1495.                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  1496.                                         .addGroup(
  1497.                                                 listeningPanelLayout
  1498.                                                         .createParallelGroup(
  1499.                                                                 javax.swing.GroupLayout.Alignment.LEADING)
  1500.                                                         .addComponent(
  1501.                                                                 playerTimerLabel)
  1502.                                                         .addComponent(
  1503.                                                                 playerProgressbar,
  1504.                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
  1505.                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
  1506.                                                                 javax.swing.GroupLayout.PREFERRED_SIZE))
  1507.                                         .addContainerGap(
  1508.                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
  1509.                                                 Short.MAX_VALUE)));
  1510.  
  1511.         listeningPanel.setBounds(20, 40, 0, 0);
  1512.         jLayeredPane2.add(listeningPanel,
  1513.                 javax.swing.JLayeredPane.DEFAULT_LAYER);
  1514.  
  1515.         timerLabel.setFont(new java.awt.Font("Arial", 1, 18));
  1516.         timerLabel.setText("000");
  1517.  
  1518.         secondsLabel.setFont(new java.awt.Font("Verdana", 1, 11));
  1519.         secondsLabel.setForeground(java.awt.Color.orange);
  1520.         secondsLabel.setText("secs");
  1521.  
  1522.         graphPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
  1523.  
  1524.         javax.swing.GroupLayout graphPanelLayout = new javax.swing.GroupLayout(
  1525.                 graphPanel);
  1526.         graphPanel.setLayout(graphPanelLayout);
  1527.         graphPanelLayout.setHorizontalGroup(graphPanelLayout
  1528.                 .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  1529.                 .addGap(0, 369, Short.MAX_VALUE));
  1530.         graphPanelLayout.setVerticalGroup(graphPanelLayout.createParallelGroup(
  1531.                 javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 85,
  1532.                 Short.MAX_VALUE));
  1533.  
  1534.         javax.swing.GroupLayout timerPanelLayout = new javax.swing.GroupLayout(
  1535.                 timerPanel);
  1536.         timerPanel.setLayout(timerPanelLayout);
  1537.         timerPanelLayout
  1538.                 .setHorizontalGroup(timerPanelLayout
  1539.                         .createParallelGroup(
  1540.                                 javax.swing.GroupLayout.Alignment.LEADING)
  1541.                         .addGroup(
  1542.                                 timerPanelLayout
  1543.                                         .createSequentialGroup()
  1544.                                         .addContainerGap()
  1545.                                         .addGroup(
  1546.                                                 timerPanelLayout
  1547.                                                         .createParallelGroup(
  1548.                                                                 javax.swing.GroupLayout.Alignment.LEADING)
  1549.                                                         .addComponent(
  1550.                                                                 graphPanel,
  1551.                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
  1552.                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
  1553.                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
  1554.                                                         .addGroup(
  1555.                                                                 timerPanelLayout
  1556.                                                                         .createSequentialGroup()
  1557.                                                                         .addComponent(
  1558.                                                                                 timerLabel)
  1559.                                                                         .addPreferredGap(
  1560.                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  1561.                                                                         .addComponent(
  1562.                                                                                 secondsLabel)))
  1563.                                         .addGap(97, 97, 97)));
  1564.         timerPanelLayout.setVerticalGroup(timerPanelLayout.createParallelGroup(
  1565.                 javax.swing.GroupLayout.Alignment.LEADING).addGroup(
  1566.                 timerPanelLayout.createSequentialGroup().addGroup(
  1567.                         timerPanelLayout.createParallelGroup(
  1568.                                 javax.swing.GroupLayout.Alignment.BASELINE)
  1569.                                 .addComponent(timerLabel,
  1570.                                         javax.swing.GroupLayout.PREFERRED_SIZE,
  1571.                                         28,
  1572.                                         javax.swing.GroupLayout.PREFERRED_SIZE)
  1573.                                 .addComponent(secondsLabel)).addPreferredGap(
  1574.                         javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  1575.                         .addComponent(graphPanel,
  1576.                                 javax.swing.GroupLayout.PREFERRED_SIZE,
  1577.                                 javax.swing.GroupLayout.DEFAULT_SIZE,
  1578.                                 javax.swing.GroupLayout.PREFERRED_SIZE)
  1579.                         .addContainerGap(21, Short.MAX_VALUE)));
  1580.  
  1581.         timerPanel.setBounds(10, 10, 390, 140);
  1582.         jLayeredPane2.add(timerPanel, javax.swing.JLayeredPane.DEFAULT_LAYER);
  1583.  
  1584.         javax.swing.GroupLayout containerFrameLayout = new javax.swing.GroupLayout(
  1585.                 containerFrame);
  1586.         containerFrame.setLayout(containerFrameLayout);
  1587.         containerFrameLayout
  1588.                 .setHorizontalGroup(containerFrameLayout
  1589.                         .createParallelGroup(
  1590.                                 javax.swing.GroupLayout.Alignment.LEADING)
  1591.                         .addGroup(
  1592.                                 containerFrameLayout
  1593.                                         .createSequentialGroup()
  1594.                                         .addGroup(
  1595.                                                 containerFrameLayout
  1596.                                                         .createParallelGroup(
  1597.                                                                 javax.swing.GroupLayout.Alignment.LEADING)
  1598.                                                         .addGroup(
  1599.                                                                 containerFrameLayout
  1600.                                                                         .createSequentialGroup()
  1601.                                                                         .addComponent(
  1602.                                                                                 messagePanel,
  1603.                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
  1604.                                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
  1605.                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
  1606.                                                                         .addPreferredGap(
  1607.                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  1608.                                                                         .addComponent(
  1609.                                                                                 jLayeredPane1,
  1610.                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
  1611.                                                                                 137,
  1612.                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE))
  1613.                                                         .addComponent(
  1614.                                                                 jLayeredPane2,
  1615.                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
  1616.                                                                 448,
  1617.                                                                 javax.swing.GroupLayout.PREFERRED_SIZE))
  1618.                                         .addContainerGap()));
  1619.         containerFrameLayout
  1620.                 .setVerticalGroup(containerFrameLayout
  1621.                         .createParallelGroup(
  1622.                                 javax.swing.GroupLayout.Alignment.LEADING)
  1623.                         .addGroup(
  1624.                                 containerFrameLayout
  1625.                                         .createSequentialGroup()
  1626.                                         .addGroup(
  1627.                                                 containerFrameLayout
  1628.                                                         .createParallelGroup(
  1629.                                                                 javax.swing.GroupLayout.Alignment.LEADING,
  1630.                                                                 false)
  1631.                                                         .addComponent(
  1632.                                                                 jLayeredPane1)
  1633.                                                         .addComponent(
  1634.                                                                 messagePanel,
  1635.                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
  1636.                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
  1637.                                                                 Short.MAX_VALUE))
  1638.                                         .addGap(18, 18, 18)
  1639.                                         .addComponent(
  1640.                                                 jLayeredPane2,
  1641.                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
  1642.                                                 177,
  1643.                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
  1644.                                         .addContainerGap()));
  1645.  
  1646.         javax.swing.GroupLayout containerPanelLayout = new javax.swing.GroupLayout(
  1647.                 containerPanel);
  1648.         containerPanel.setLayout(containerPanelLayout);
  1649.         containerPanelLayout.setHorizontalGroup(containerPanelLayout
  1650.                 .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  1651.                 .addComponent(containerFrame,
  1652.                         javax.swing.GroupLayout.PREFERRED_SIZE, 512,
  1653.                         javax.swing.GroupLayout.PREFERRED_SIZE));
  1654.         containerPanelLayout.setVerticalGroup(containerPanelLayout
  1655.                 .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  1656.                 .addComponent(containerFrame,
  1657.                         javax.swing.GroupLayout.PREFERRED_SIZE,
  1658.                         javax.swing.GroupLayout.DEFAULT_SIZE,
  1659.                         javax.swing.GroupLayout.PREFERRED_SIZE));
  1660.  
  1661.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
  1662.                 getContentPane());
  1663.         getContentPane().setLayout(layout);
  1664.         layout.setHorizontalGroup(layout.createParallelGroup(
  1665.                 javax.swing.GroupLayout.Alignment.LEADING).addComponent(
  1666.                 containerPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
  1667.                 javax.swing.GroupLayout.DEFAULT_SIZE,
  1668.                 javax.swing.GroupLayout.PREFERRED_SIZE));
  1669.         layout.setVerticalGroup(layout.createParallelGroup(
  1670.                 javax.swing.GroupLayout.Alignment.LEADING).addComponent(
  1671.                 containerPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
  1672.                 javax.swing.GroupLayout.DEFAULT_SIZE,
  1673.                 javax.swing.GroupLayout.PREFERRED_SIZE));
  1674.     }// </editor-fold>
  1675.     //GEN-END:initComponents
  1676.  
  1677.     private void reCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
  1678.  
  1679.         if (getCurrentState() == RE_RECORDING_STATE) {
  1680.             stopReRecording();
  1681.         } else if (getCurrentState() == LISTENING_STATE) {
  1682.             stopListening();
  1683.         } else if (getCurrentState() == SAVING_STATE) {
  1684.             // cancel saving
  1685.             cancelSaving();
  1686.         } else if (getCurrentState() == NORMAL_STATE) {
  1687.             // do nothing
  1688.         } else {
  1689.             throw new IllegalArgumentException(getCurrentState()
  1690.                     + " is invalid state");
  1691.         }
  1692.  
  1693.         // delete file if exist
  1694.         if (this.getFileUnderProcessing() != null) {
  1695.             if (this.getFileUnderProcessing().exists()) {
  1696.                 this.getFileUnderProcessing().delete();
  1697.             }
  1698.         }
  1699.  
  1700.         // switch between panels
  1701.         this.recordingPanel.setVisible(true);
  1702.         this.reRecordingPanel.setVisible(false);
  1703.  
  1704.         // set state to Normal
  1705.         this.setCurrentState(NORMAL_STATE);
  1706.  
  1707.     }
  1708.  
  1709.     private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
  1710.  
  1711.         try {
  1712.             // disable button
  1713.             this.reRecordingButton.setEnabled(false);
  1714.             this.listenButton.setEnabled(false);
  1715.  
  1716.             // set state
  1717.             this.setCurrentState(SAVING_STATE);
  1718.  
  1719.             // visible image button
  1720.             this.saveImageButton.setVisible(true);
  1721.  
  1722.             // prepare file to save
  1723.             List<File> lists = new ArrayList<File>();
  1724.             lists.add(this.getFileUnderProcessing());
  1725.  
  1726.             // paint message to screen
  1727.             this.paintError(" Saving file....", ApplicationUtil.INFO);
  1728.  
  1729.             // preform save
  1730.             ApplicationUtil.saveWaveToServer(lists, parameterMap);
  1731.  
  1732.             // increment message counter
  1733.             this.incrementMessageCount();
  1734.  
  1735.             // set current file to new file
  1736.             this.setFileUnderProcessing(ApplicationUtil.generateFile(
  1737.                     messageCount, parameterMap));
  1738.  
  1739.             // set state
  1740.             this.setCurrentState(NORMAL_STATE);
  1741.  
  1742.             // enable button
  1743.             this.reRecordingButton.setEnabled(false);
  1744.             this.listenButton.setEnabled(false);
  1745.  
  1746.             // paint message to screen
  1747.             this.paintError(" Saved Succesfully ", ApplicationUtil.INFO);
  1748.  
  1749.             this.saveImageButton.setVisible(false);
  1750.  
  1751.             // switch panels
  1752.             // switch between panels
  1753.             this.recordingPanel.setVisible(true);
  1754.             this.reRecordingPanel.setVisible(false);
  1755.  
  1756.         } catch (Exception ex) {
  1757.             ex.printStackTrace();
  1758.         }
  1759.  
  1760.     }
  1761.  
  1762.     private void reRecordingButtonActionPerformed(java.awt.event.ActionEvent evt) {
  1763.  
  1764.         int currentCurrentState = getCurrentState();
  1765.  
  1766.         if (currentCurrentState == RecorderUI.RE_RECORDING_STATE) {
  1767.             stopReRecording();
  1768.             this.listenButton.setEnabled(true);
  1769.             this.saveButton.setEnabled(true);
  1770.         } else {
  1771.             startReRecording();
  1772.             this.listenButton.setEnabled(false);
  1773.             this.saveButton.setEnabled(false);
  1774.         }
  1775.  
  1776.     }
  1777.  
  1778.     void pauseButtonActionPerformed(java.awt.event.ActionEvent evt) {
  1779.         pausePlayer();
  1780.     }
  1781.  
  1782.     void playButtonActionPerformed(java.awt.event.ActionEvent evt) {
  1783.         startPlayer();
  1784.     }
  1785.  
  1786.     private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
  1787.  
  1788.         if (getCurrentState() == RecorderUI.RECORDING_STATE) {
  1789.  
  1790.             // stop recording without switching panel
  1791.             this.stopRecording(false);
  1792.  
  1793.             // delete any recorded file
  1794.             if (this.getFileUnderProcessing() != null) {
  1795.                 if (this.getFileUnderProcessing().exists()) {
  1796.                     this.getFileUnderProcessing().delete();
  1797.                 }
  1798.             }
  1799.         } // ends recording if
  1800.     }
  1801.  
  1802.     //GEN-BEGIN:variables
  1803.     // Variables declaration - do not modify
  1804.     private javax.swing.JButton cancelButton;
  1805.     private javax.swing.JPanel containerFrame;
  1806.     private javax.swing.JPanel containerPanel;
  1807.     private javax.swing.JLabel errorLabel;
  1808.     private javax.swing.JPanel graphPanel;
  1809.     private javax.swing.JLabel jLabel3;
  1810.     private javax.swing.JLayeredPane jLayeredPane1;
  1811.     private javax.swing.JLayeredPane jLayeredPane2;
  1812.     private javax.swing.JButton listenButton;
  1813.     private javax.swing.JPanel listeningButtonPanel1;
  1814.     private javax.swing.JPanel listeningPanel;
  1815.     private javax.swing.JPanel messagePanel;
  1816.     private javax.swing.JButton pauseButton;
  1817.     private javax.swing.JButton playButton;
  1818.     private javax.swing.JProgressBar playerProgressbar;
  1819.     private javax.swing.JLabel playerTimerLabel;
  1820.     private javax.swing.JButton reCancelButton;
  1821.     private javax.swing.JButton reRecordingButton;
  1822.     private javax.swing.JPanel reRecordingPanel;
  1823.     private javax.swing.JButton recordButton;
  1824.     private javax.swing.JPanel recordingPanel;
  1825.     private javax.swing.JButton saveButton;
  1826.     private javax.swing.JButton saveImageButton;
  1827.     private javax.swing.JLabel secondsLabel;
  1828.     private javax.swing.JButton stopButton;
  1829.     private javax.swing.JLabel timerLabel;
  1830.     private javax.swing.JPanel timerPanel;
  1831.     private javax.swing.JLabel totalTimeLabel;
  1832.     // End of variables declaration//GEN-END:variables
  1833.  
  1834. }
Advertisement
Add Comment
Please, Sign In to add comment