Skip to content

Commit

Permalink
Stable
Browse files Browse the repository at this point in the history
  • Loading branch information
Puyodead1 committed Nov 13, 2019
1 parent 50336f1 commit 70364bc
Show file tree
Hide file tree
Showing 4 changed files with 572 additions and 99 deletions.
51 changes: 33 additions & 18 deletions src/me/puyodead1/filedownloader/DownloadDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class DownloadDialog extends Dialog {

protected Object result;
protected Shell shell;
private String fileName;
private String savePath;
private URL url;
private Downloader downloader;

protected Shell shell, parent;
protected String fileName, urlString, savePath;
protected URL url;
protected ProgressBar progressBar;
protected Label lblProgress;
protected Downloader downloader;

/**
* Create the dialog.
* @param parent
Expand All @@ -30,14 +33,9 @@ public DownloadDialog(Shell parent, int style, String url, String fileName, Stri
super(parent, style);
this.fileName = fileName;
this.savePath = savePath;
this.urlString = url;
this.parent = parent;
setText("Downloading...");
try {
this.url = new URL(url);
this.downloader = new Downloader(parent, this.url, savePath);
} catch(MalformedURLException e) {
final MessageDialog dialog = new MessageDialog(shell, getStyle(), "Error", e.getMessage());
dialog.open();
}
}

/**
Expand All @@ -46,6 +44,13 @@ public DownloadDialog(Shell parent, int style, String url, String fileName, Stri
*/
public Object open() {
createContents();
try {
this.url = new URL(urlString);
downloader = new Downloader(this, parent, shell, this.url, savePath);
} catch(MalformedURLException e) {
final MessageDialog dialog = new MessageDialog(shell, getStyle(), "Error", e.getMessage());
dialog.open();
}
shell.open();
shell.layout();
Display display = getParent().getDisplay();
Expand All @@ -62,20 +67,30 @@ public Object open() {
*/
private void createContents() {
shell = new Shell(getParent(), getStyle());
shell.setSize(450, 148);
shell.setSize(450, 168);
shell.setText(getText());

ProgressBar progressBar = new ProgressBar(shell, SWT.NONE);
progressBar.setBounds(10, 52, 424, 25);
progressBar = new ProgressBar(shell, SWT.NONE);
progressBar.setBounds(10, 70, 424, 25);

Button btnCancel = new Button(shell, SWT.NONE);
btnCancel.setBounds(10, 83, 424, 25);
btnCancel.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
downloader.setStatus(Downloader.CANCELLED);
System.out.println("Download cancelled: " + downloader.getStatus());
}
});
btnCancel.setBounds(10, 101, 424, 25);
btnCancel.setText("Cancel");

Label lblFile = new Label(shell, SWT.WRAP | SWT.CENTER);
lblFile.setAlignment(SWT.CENTER);
lblFile.setText("Downloading file: \n" + fileName);
lblFile.setBounds(10, 10, 424, 36);


lblProgress = new Label(shell, SWT.NONE);
lblProgress.setAlignment(SWT.CENTER);
lblProgress.setBounds(10, 52, 424, 15);
}
}
123 changes: 63 additions & 60 deletions src/me/puyodead1/filedownloader/Downloader.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.puyodead1.filedownloader;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
Expand All @@ -14,25 +13,31 @@
public class Downloader implements Runnable {
private static final int MAX_BUFFER_SIZE = 1024;

public static final String STATUSES[] = { "Downloading", "Paused", "Complete", "Cancelled", "Error" };
public static final String STATUSES[] = {"Downloading", "Paused",
"Complete", "Cancelled", "Error"};

public static final int DOWNLOADING = 0;
public static final int PAUSED = 1;
public static final int COMPLETE = 2;
public static final int CANCELLED = 3;
public static final int ERROR = 4;

private Shell parentShell;
private Shell downloadDialogShell, parentShell;
private URL url;
private int size;
private int downloaded;
private int status;
private String output;
private DownloadDialog downloadDialog;

public Downloader(Shell parent, URL url, String output) {
this.parentShell = parent;
public Downloader(DownloadDialog downloadDialog, Shell parentShell,
Shell downloadDialogShell, URL url, String output) {
this.downloadDialogShell = downloadDialogShell;
this.parentShell = parentShell;
this.url = url;
this.output = output;
this.downloadDialog = downloadDialog;

size = -1;
downloaded = 0;
status = DOWNLOADING;
Expand All @@ -56,6 +61,10 @@ public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

private void error() {
status = ERROR;
stateChanged();
Expand All @@ -72,7 +81,8 @@ public void run() {

try {
// Open connection to URL.
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();

// Specify what portion of file to download.
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
Expand Down Expand Up @@ -102,58 +112,46 @@ public void run() {
}

// Open file and seek to the end of it.
try {
file = new RandomAccessFile(output, "rw");
file.seek(downloaded);

stream = connection.getInputStream();
while (status == DOWNLOADING) {
/*
* Size buffer according to how much of the file is left to download.
*/
byte buffer[];
if (size - downloaded > MAX_BUFFER_SIZE) {
buffer = new byte[MAX_BUFFER_SIZE];
} else {
buffer = new byte[size - downloaded];
}

// Read from server into buffer.
int read = stream.read(buffer);
if (read == -1)
break;

// Write buffer to file.
file.write(buffer, 0, read);
downloaded += read;
stateChanged();
}
file = new RandomAccessFile(output, "rw");
file.seek(downloaded);

stream = connection.getInputStream();
while (status == DOWNLOADING) {
/*
* Change status to complete if this point was reached because downloading has
* finished.
* Size buffer according to how much of the file is left to
* download.
*/
if (status == DOWNLOADING) {
status = COMPLETE;
stateChanged();
byte buffer[];
if (size - downloaded > MAX_BUFFER_SIZE) {
buffer = new byte[MAX_BUFFER_SIZE];
} else {
buffer = new byte[size - downloaded];
}
} catch (Throwable t) {
Display.getDefault().asyncExec(new Runnable() {

@Override
public void run() {
final MessageDialog dialog = new MessageDialog(parentShell, parentShell.getStyle(), "Error",
t.getMessage());
dialog.open();
}
});
error();

// Read from server into buffer.
int read = stream.read(buffer);
if (read == -1)
break;

// Write buffer to file.
file.write(buffer, 0, read);
downloaded += read;
stateChanged();
}

/*
* Change status to complete if this point was reached because
* downloading has finished.
*/
if (status == DOWNLOADING) {
status = COMPLETE;
stateChanged();
}
} catch (Exception e) {
error();
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
System.out.println(writer.toString());
error();
} finally {
// Close file.
if (file != null) {
Expand Down Expand Up @@ -182,34 +180,39 @@ public void run() {
private void stateChanged() {
if (status == ERROR) {
Display.getDefault().asyncExec(new Runnable() {

@Override
public void run() {
final MessageDialog dialog = new MessageDialog(parentShell, parentShell.getStyle(), "Error",
System.out.println("error downloading");
final MessageDialog dialog = new MessageDialog(parentShell,
parentShell.getStyle(), "Error",
"An error occured and the download could not complete!");
dialog.open();

}
});
} else if (status == DOWNLOADING) {
// update progess bar
int progress = Math.round(this.getProgress());

Display.getDefault().asyncExec(new Runnable() {
final int progress = Math.round(this.getProgress());

downloadDialogShell.getDisplay().asyncExec(new Runnable() {
public void run() {
// DownloadDialog.progressBar.setText(progress + "%");
System.out.println(progress + "%");
downloadDialog.progressBar.setSelection(progress);
downloadDialog.lblProgress.setText(progress + "%");
}
});
} else if (status == COMPLETE) {
downloadDialogShell.getDisplay().asyncExec(new Runnable() {
public void run() {
downloadDialog.progressBar.setSelection(100);
}
});
Display.getDefault().asyncExec(new Runnable() {

@Override
public void run() {
final MessageDialog dialog = new MessageDialog(parentShell, parentShell.getStyle(), "Complete",
downloadDialogShell.close();
final MessageDialog dialog = new MessageDialog(parentShell,
parentShell.getStyle(), "Complete",
"File download complete!");
dialog.open();

}
});
}
Expand Down
Loading

0 comments on commit 70364bc

Please sign in to comment.