Package transfer.ui

Source Code of transfer.ui.TransferTableEntry

package transfer.ui;


import misc.measure.AbstractFileMeasure;
import misc.measure.FileSize;
import misc.measure.TransferSpeed;
import transfer.AbstractFileTransfer;
import transfer.TransferListenerInterface;
import transfer.ui.bar.AbstractBar;
import transfer.ui.bar.FullBar;
import transfer.ui.bar.ProgressBar;


public class TransferTableEntry implements TransferListenerInterface
{
  private AbstractFileTransfer transfer;
  private AbstractBar progressBar;

  private boolean everUpdated = false;

  private long completedSize;
  private long lastUpdateTime = 0;
  private long lastSpeed;

  public TransferTableEntry(AbstractFileTransfer transfer)
  {
    this.transfer = transfer;
    this.transfer.addListener(this);

    progressBar = new ProgressBar(transfer.getFile().getLength());

    completedSize = transfer.getCompletedSize();
    progressBar.setProgress(completedSize);
  }

  public void onProgress()
  {
    long currentTime = System.currentTimeMillis();

    if (currentTime != lastUpdateTime)
    {
      updateSpeed(currentTime);
    }

    completedSize = transfer.getCompletedSize();
    progressBar.setProgress(completedSize);
  }

  private void updateSpeed(long currentTime)
  {
    if (everUpdated)
    {
      final int ALPHA = 2;

      long currentSpeed = calcCurrentSpeed(currentTime);
      lastSpeed = (ALPHA * lastSpeed + (10 - ALPHA) * currentSpeed) / 10;
    }
    else
    {
      lastSpeed = transfer.getCompletedSize();
      everUpdated = true;
    }

    lastUpdateTime = currentTime;
  }

  private long calcCurrentSpeed(long currentTime)
  {
    long progress = transfer.getCompletedSize() - completedSize;
    return progress * 1000 / (currentTime - lastUpdateTime);
  }

  public void onComplete()
  {
    progressBar = new FullBar();
  }

  public String toString()
  {
    return transfer.getFile().getName();
  }

  public AbstractFileTransfer getTransfer()
  {
    return transfer;
  }

  public AbstractFileMeasure getSize()
  {
    return new FileSize(transfer.getFile().getLength());
  }

  public AbstractFileMeasure getCompletedSize()
  {
    return new FileSize(completedSize);
  }

  public AbstractBar getProgressBar()
  {
    return progressBar;
  }

  public String getSpeed()
  {
    if (everUpdated && !transfer.isFinished())
    {
      return getHumanReadableSpeed(lastSpeed).toString();
    }
    else
    {
      return "";
    }
  }

  private AbstractFileMeasure getHumanReadableSpeed(long value)
  {
    TransferSpeed result = new TransferSpeed(value);
    return result;
  }

}
TOP

Related Classes of transfer.ui.TransferTableEntry

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.