gt:         downloader = new DownloaderThread(url, outfile, progress);
context: //File:		MainDialog.java
  //Author:	Gary Bezet
  //Date:		2016-07-14
  //Desc:		This program was created to help Nick download videos from the Louisiana House or Representatives video on demand site at 
  //HORWebsite:   http://house.louisiana.gov/H_Video/Hse_Video_OnDemand.aspx
  
  
  package net.garyscorner.lahordownloader;
  
  
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.net.MalformedURLException;
  import java.net.URL;
  import javax.swing.JFileChooser;
  import javax.swing.JOptionPane;
  import javax.swing.filechooser.FileNameExtensionFilter;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  import javax.swing.JDialog;
  import javax.swing.Timer;
  
  
  
  
  
  public class MainDialog extends javax.swing.JDialog {
      
      //my variables
      FileNameExtensionFilter filter;
      String path = null;
      
      //should be true when downloading
      private boolean downloading = false;
      
  
      private File savefile;
      
      
      private DownloaderThread downloader;
      FileOutputStream outfile;
      
      long filesize = 0;
      
      private DownloadProgress progress;
      
      private Timer progressTimer;
      
      
      //my functions
      
      //attempted close
      public boolean attemptwindowclose() {
          
          if(downloading) {
              
              if(JOptionPane.showConfirmDialog(this, "Cancel download?", "Cancel Download", JOptionPane.YES_NO_CANCEL_OPTION) == JOptionPane.YES_OPTION) {
                  
                  jLabel_status.setText("Canceling download...");
                  
                  System.err.println("Interrupting download thread...");
                  downloader.interrupt();
                  
                  this.downloadFinished(DownloadProgress.STATUS_CANCELED);
                  
                  return false;
                  
              } else {
                  return false;
              }
              
          } else {
              return true;
          }
          
          
          
      }
      
      //initialize my stuff
      public void myinit() {
          
          filter = new FileNameExtensionFilter("MP4 Video", "mp4");
         
          this.setTitle("La HOR Video Downloader");
          
          
      }
      
      //validate the URL
      private URL validateURL(String url) {
          
          
          URL returnvalue;
          try {
              returnvalue = new URL(url);
          } catch (MalformedURLException ex) {
              System.err.printf("Bad URL provided:  %1$s", url);
              JOptionPane.showMessageDialog(this, "Bad URL!", "Error", JOptionPane.ERROR_MESSAGE);
              returnvalue = null;
          }
          
          return returnvalue;
      }
      
      //called when download stops or is finished
      private void downloadFinished(int finalstatus) {
          
          //remove the timer and let the garbage collector take it
          progressTimer.stop();
          progressTimer = null;
          
          //join the downloaded thread
          try {
              downloader.join();  //join the downloader
              System.err.println("Joined download thread.");
          } catch (InterruptedException ex) {
              System.err.println("Error:  Could not join download thread!!!");
          }
          
          if(finalstatus == DownloadProgress.STATUS_CANCELED || finalstatus == DownloadProgress.STATUS_ERROR || finalstatus == DownloadProgress.STATUS_PERROR) {
              System.err.println("Downloaded thread ended with error, deleting the file:  " + this.savefile);
              this.savefile.delete();
          }
          
          downloader = null;  //let the garbage collector take it away
          
          jButton_download.setEnabled(true);
          textfield_url.setEnabled(true);
          jButton_close.setText("Close");
          this.downloading = false;
          
      }
      
      //setup the download process
      private void startdownload(URL url, File savefile)  {
          
          this.progress = new DownloadProgress();
          
          this.downloading = true;
          jButton_close.setText("Cancel");
          jButton_download.setEnabled(false);
          textfield_url.setEnabled(false);
          
          this.savefile = savefile;
          
          ActionListener checkProgress = new ActionListener() {
              
              long updatecount = 0;
              
              long filesize;
              int status = 0;
              
              @Override
              public void actionPerformed(ActionEvent e) {
                  
                  long newupdatecount = progress.getUpdateCount();
                  
                  if(updatecount < newupdatecount) {
                  
                      
                      updatecount = newupdatecount;
                      
                      if(this.status != progress.getStatus()) {
                          this.status = progress.getStatus();  //ugly
  
                          switch(this.status) {
                              case DownloadProgress.STATUS_CONNECTED:
                                  jLabel_status.setText("Connected");
                                  break;
  
                              case DownloadProgress.STATUS_CONNECTING:
                                  jLabel_status.setText("Connecting...");
                                  break;
  
                              case DownloadProgress.STATUS_ERROR:
                                  jLabel_status.setText("ERROR!");
                                  downloadFinished(this.status);
                                  break;
  
                              case DownloadProgress.STATUS_FINISHED:
                                  jLabel_status.setText("Finished!");
                                  downloadFinished(this.status);
                                  break;
  
                              case DownloadProgress.STATUS_PERROR:
                                  jLabel_status.setText("Error parsing video URL!");
                                  downloadFinished(this.status);
                                  break;
  
                              case DownloadProgress.STATUS_STARTED:
                                  jLabel_status.setText("Downloading...");
                                  break;
  
                              case DownloadProgress.STATUS_RETREIVING:
                                  jLabel_status.setText("Retrieving download URL from website...");
                                  break;
                                  
                              case DownloadProgress.STATUS_CANCELED:
                                  jLabel_status.setText("Download canceled!");
                                  break;
                              default:
  
  
                          }
  
                      }
  
                      //check progress and update progress bar here
                      if(this.filesize > 0) {
                           jProgressBar_download.setValue((int) (progress.getCount() / 1024));
                      } else if(this.filesize == -1) {
  
                      } else {
                          if((this.filesize = progress.getFileSize()) > 0 ) {
                              jProgressBar_download.setMaximum((int)(this.filesize / 1024));
                          }
                      }
                  }
              }
              
          };
          
          progressTimer = new Timer(1000, checkProgress);
          progressTimer.start();
          
          try {
              outfile = new FileOutputStream(this.savefile);
          } catch (FileNotFoundException ex) {
              System.err.printf("An error occurred while trying to open \"%1%s\":  %2$s", this.savefile, ex);
          }
          
  