/*
     * Executed in event dispatching thread
     */
    @Override
    public void done() {

      try {

        String value = get();
        myMessage.messageChanged(value);

      } catch (InterruptedException e) {
        e.printStackTrace();
      } catch (ExecutionException e) {
        e.printStackTrace();
      } catch (CancellationException e) {
        myMessage.messageChanged("Operation Cancelled");
      } catch (Exception e) {
        e.printStackTrace();
      }

      Toolkit.getDefaultToolkit().beep();
      // startButton.setEnabled(true);
      // cancelButton.setEnabled(false);
      setCursor(null); // turn off the wait cursor
      StartButton.setEnabled(true);
      cancelButton.setEnabled(false);
      taskOutput.append("Done!\n");
    }
    public void getFileInfo() {
      myMessage.stateChanged("DOWNLOADFILEINFO");
      URL url;
      try {
        url = new URL(myProperties.getProperty("DOWNLOADURL"));
        System.out.println(myProperties.getProperty("DOWNLOADURL"));
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        int contentLength = httpConn.getContentLength();
        Map<String, List<String>> theMap = httpConn.getHeaderFields();
        Set keys = theMap.keySet();
        for (Iterator i = keys.iterator(); i.hasNext(); ) {
          String key = (String) i.next();
          String value = theMap.get(key).toString();
          myMessage.messageChanged(key + " = " + value);
        }

        if (contentLength == -1) {
          myMessage.messageChanged("unknown content length");
        } else {
          myMessage.messageChanged("content length: " + contentLength + " bytes");
        }
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    private void downloadFile(String theFilename) {
      OutputStream outStream = null;
      InputStream is = null;
      try {
        myMessage.stateChanged("DOWNLOADLOCALBASE");
        String theFile =
            myProperties
                .getProperty("BASEURL")
                .substring(0, myProperties.getProperty("BASEURL").lastIndexOf("/"));

        theFile = theFile + "/" + (new URI((theFilename).replaceAll(" ", "%20")));
        URLConnection uCon = null;
        int size = 1024;
        URL Url;
        byte[] buf;
        int ByteRead, ByteWritten = 0;
        Url = new URL(theFile);
        String theLocalFilename = (String) myFileList.get(theFilename).get(2);
        long theFileSize = Long.parseLong((String) myFileList.get(theFilename).get(5));
        outStream =
            new BufferedOutputStream(
                new FileOutputStream(
                    myProperties.getProperty("LOCALBASEDIR")
                        + "\\"
                        + myFileList.get(theFilename).get(2)));

        uCon = Url.openConnection();
        is = uCon.getInputStream();
        buf = new byte[size];
        while ((ByteRead = is.read(buf)) != -1) {
          outStream.write(buf, 0, ByteRead);
          ByteWritten += ByteRead;
          myMessage.setProgress((int) getPercentage(ByteWritten, theFileSize));
          // System.out.println(ByteWritten);
        }
        myMessage.messageChanged("Downloaded Successfully.");
        myMessage.messageChanged(
            "File name:\""
                + theLocalFilename
                + "\"\nNo ofbytes :"
                + ByteWritten
                + "Filesize ="
                + theFileSize);
      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        try {
          is.close();
          outStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    private void CleanRemoteBaseDir() {
      myMessage.stateChanged("CLEANBASEDIR");
      URL theURL = null;
      ;
      try {
        theURL = new URL(myProperties.getProperty("BASEURL"));
      } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
      ;

      String theRemoteTempDir = myProperties.getProperty("REMOTEBASEDIR");
      String data = "";
      try {
        data =
            URLEncoder.encode("filelocation", "UTF-8")
                + "="
                + URLEncoder.encode(theRemoteTempDir, "UTF-8");
        data +=
            "&"
                + URLEncoder.encode("fileprocess", "UTF-8")
                + "="
                + URLEncoder.encode("CLEANBASEDIR", "UTF-8");

        URLConnection conn = theURL.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();
        myMessage.messageChanged(0, "Cleaning Remote Base Dir");
        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line2;
        while (((line2 = rd.readLine()) != null) && !isCancelled()) {
          myMessage.messageChanged(0, line2);
        }
        rd.close();
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    private void getFileList() {
      myMessage.stateChanged("REMOTEFILELIST");
      URL theURL = null;
      ;
      try {
        theURL = new URL(myProperties.getProperty("BASEURL"));
      } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
      ;

      String theRemoteBaseDir = myProperties.getProperty("REMOTEBASEDIR");
      Properties props = System.getProperties();
      props.put("http.proxyHost", myProperties.getProperty("PROXYHOST"));
      props.put("http.proxyPort", myProperties.getProperty("PROXYPORT"));
      String data = "";
      try {
        data =
            URLEncoder.encode("filelocation", "UTF-8")
                + "="
                + URLEncoder.encode(theRemoteBaseDir, "UTF-8");
        data +=
            "&"
                + URLEncoder.encode("fileprocess", "UTF-8")
                + "="
                + URLEncoder.encode("REMOTEFILELIST", "UTF-8");

        URLConnection conn = theURL.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();
        myMessage.messageChanged(0, "Get the File List");
        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line2;
        myFileList = new HashMap<String, List>();
        while (((line2 = rd.readLine()) != null) && !isCancelled()) {
          CSV parser = new CSV('|');
          List theFileList = parser.parse(line2);
          // myFileList.put((String)theFileList.get(1), theFileList);
          for (int i = 0; i < theFileList.size(); i++) {
            if (i == 1) {
              myFileList.put((String) theFileList.get(i), theFileList);
            }
          }
        }
        rd.close();
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    public long getFileSize() {
      URL url;
      long contentLength = -1;
      try {
        url = new URL(myProperties.getProperty("DOWNLOADURL"));
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        contentLength = httpConn.getContentLength();

        if (contentLength == -1) {
          myMessage.messageChanged("unknown content length");
        } else {
          myMessage.messageChanged("content length: " + contentLength + " bytes");
        }
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

      return contentLength;
    }
    private void emailFile(String theFilename) {
      myMessage.stateChanged("EMAILFILEFROMBASE");
      URL theURL = null;
      String theLocalFilename = (String) myFileList.get(theFilename).get(2);
      try {
        theURL = new URL(myProperties.getProperty("BASEURL"));
      } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
      ;

      String data = "";
      try {
        data =
            URLEncoder.encode("filelocation", "UTF-8")
                + "="
                + URLEncoder.encode(myProperties.getProperty("REMOTEBASEDIR"), "UTF-8");
        data +=
            "&"
                + URLEncoder.encode("fileprocess", "UTF-8")
                + "="
                + URLEncoder.encode("FILEEMAIL", "UTF-8");
        data +=
            "&"
                + URLEncoder.encode("filename", "UTF-8")
                + "="
                + URLEncoder.encode(theLocalFilename, "UTF-8");
        data +=
            "&"
                + URLEncoder.encode("mailtoaddress", "UTF-8")
                + "="
                + URLEncoder.encode(myProperties.getProperty("REMOTEDOWNLOADEMAIL"), "UTF-8");

        URLConnection conn = theURL.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();
        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line2;
        while (((line2 = rd.readLine()) != null) && !isCancelled()) {
          myMessage.messageChanged(line2);
        }
        rd.close();

      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    private void sendDownloadMessage() {
      myMessage.stateChanged("DOWNLOADREMOTE");
      URL theURL = null;
      ;
      try {
        theURL = new URL(myProperties.getProperty("BASEURL"));
      } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
      ;

      String data = "";
      try {
        data =
            URLEncoder.encode("filelocation", "UTF-8")
                + "="
                + URLEncoder.encode(myProperties.getProperty("REMOTEBASEDIR"), "UTF-8");
        data +=
            "&"
                + URLEncoder.encode("filename", "UTF-8")
                + "="
                + URLEncoder.encode(myProperties.getProperty("REMOTEDOWNLOADFILENAME"), "UTF-8");
        data +=
            "&"
                + URLEncoder.encode("fileprocess", "UTF-8")
                + "="
                + URLEncoder.encode("DOWNLOAD", "UTF-8");
        data +=
            "&"
                + URLEncoder.encode("url", "UTF-8")
                + "="
                + URLEncoder.encode(myProperties.getProperty("DOWNLOADURL"), "UTF-8");

        URLConnection conn = theURL.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();
        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line2;
        while (((line2 = rd.readLine()) != null)) {
          myMessage.messageChanged(line2);
        }
        rd.close();
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    private void join(File aFile) throws Exception {

      Properties theProperties = myProperties;
      String theLocalBaseDir = theProperties.getProperty("LOCALBASEDIR");

      try {

        myMessage.stateChanged("JOIN");
        myMessage.messageChanged(0, "In join");
        myMessage.messageChanged(0, "My file name is " + aFile.getName());
        myMessage.messageChanged(0, "My file full path  is " + aFile.getPath());
        // myMessage.messageChanged(0, "My MD5 value is "+MD5.getMD5(aFile));
        /**
         * myMessage.messageChanged(0, "Delete files from "+theLocalTempDir);
         *
         * <p>File directory = new File(theLocalTempDir); // Get all files in directory File[] files
         * = directory.listFiles(); for (File file : files) { // Delete each file if
         * (!file.delete()) { // Failed to delete file System.out.println("Failed to delete "+file);
         * } }*
         */
        myMessage.messageChanged(0, "Joining files into " + theLocalBaseDir);
        File theBaseDir = new File(theLocalBaseDir);
        Joiner theJoiner = new Joiner(aFile.getAbsolutePath(), theLocalBaseDir);
        /**
         * int theFileSplitSize = Integer.parseInt(theProperties.getProperty("LOCALFILESPLITSIZE"));
         * int theFileSplitBufferSize =
         * Integer.parseInt(theProperties.getProperty("LOCALFILESPLITBUFSIZE")); long
         * theNumIterations = (long)Math.ceil((float)aFile.length()/(float)theFileSplitSize); long
         * theCurrentIteration = 0; Splitter theSplitter = new
         * Splitter(aFile.getPath(),theLocalTempDir,theFileSplitSize,theFileSplitBufferSize);
         * myMessage.messageChanged(0, "Splitting FileName"+theSplitter.getNextTargetName());*
         */
        long theNumIterations = theJoiner.getNumSplitFiles();
        long theCurrentIteration = 0;
        while (theJoiner.moreToJoin() && !isCancelled()) {
          theJoiner.go();
          theCurrentIteration++;
          if (theJoiner.moreToJoin()) {
            myMessage.messageChanged(
                (int) getPercentage(theCurrentIteration, theNumIterations),
                "Joining FileName" + theJoiner.getNextSourceName());
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      if (!isCancelled()) {
        myMessage.messageChanged(100, "done");
      } else {
        myMessage.messageChanged("Canceled in split");
      }
    }
    private boolean getStatus(long theFileSize) {
      URL theURL = null;
      ;
      try {
        theURL = new URL(myProperties.getProperty("BASEURL"));
      } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
      ;

      String data = "";
      boolean theStatus = false;
      try {
        data =
            URLEncoder.encode("filelocation", "UTF-8")
                + "="
                + URLEncoder.encode(myProperties.getProperty("REMOTEBASEDIR"), "UTF-8");
        data +=
            "&"
                + URLEncoder.encode("fileprocess", "UTF-8")
                + "="
                + URLEncoder.encode("STATUS", "UTF-8");
        data +=
            "&"
                + URLEncoder.encode("filename", "UTF-8")
                + "="
                + URLEncoder.encode(myProperties.getProperty("REMOTEDOWNLOADFILENAME"), "UTF-8");

        URLConnection conn = theURL.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();
        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line2;
        CSV theParser = new CSV('|');
        while (((line2 = rd.readLine()) != null) && !isCancelled()) {
          List list = theParser.parse(line2);
          System.out.println((String) list.get(0));
          if (((String) list.get(0)).equals(" Process running")) {
            theStatus = true;
            myMessage.messageChanged(
                (int) getPercentage(Long.parseLong((String) list.get(1)), theFileSize),
                "Process Running downloaded " + (String) list.get(1) + " bytes");
          }
          if (((String) list.get(0)).equals(" Process stopped")) {
            theStatus = false;
            myMessage.messageChanged(
                (int) getPercentage(Long.parseLong((String) list.get(1)), theFileSize),
                "Process Stopped File size =  " + (String) list.get(1) + " bytes");
          }
        }
        rd.close();

      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      System.out.println("theStatus =" + theStatus);
      return theStatus;
    }