From 50336f1ec2c4fb791cfbf0a7c3320adc2c2c3f32 Mon Sep 17 00:00:00 2001 From: Puyodead1 <23562356riley@gmail.com> Date: Wed, 13 Nov 2019 10:06:24 -0500 Subject: [PATCH] init --- .gitignore | 4 + .../filedownloader/DownloadDialog.java | 81 +++++++ .../puyodead1/filedownloader/Downloader.java | 217 ++++++++++++++++++ .../filedownloader/FileDownloader.java | 113 +++++++++ .../filedownloader/MessageDialog.java | 73 ++++++ 5 files changed, 488 insertions(+) create mode 100644 .gitignore create mode 100644 src/me/puyodead1/filedownloader/DownloadDialog.java create mode 100644 src/me/puyodead1/filedownloader/Downloader.java create mode 100644 src/me/puyodead1/filedownloader/FileDownloader.java create mode 100644 src/me/puyodead1/filedownloader/MessageDialog.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..844bdf4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.classpath +.project +bin/ +.settings/ \ No newline at end of file diff --git a/src/me/puyodead1/filedownloader/DownloadDialog.java b/src/me/puyodead1/filedownloader/DownloadDialog.java new file mode 100644 index 0000000..94557d7 --- /dev/null +++ b/src/me/puyodead1/filedownloader/DownloadDialog.java @@ -0,0 +1,81 @@ +package me.puyodead1.filedownloader; + +import org.eclipse.swt.widgets.Dialog; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.ProgressBar; + +import java.net.MalformedURLException; +import java.net.URL; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Label; + +public class DownloadDialog extends Dialog { + + protected Object result; + protected Shell shell; + private String fileName; + private String savePath; + private URL url; + private Downloader downloader; + + /** + * Create the dialog. + * @param parent + * @param style + */ + public DownloadDialog(Shell parent, int style, String url, String fileName, String savePath) { + super(parent, style); + this.fileName = fileName; + this.savePath = savePath; + 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(); + } + } + + /** + * Open the dialog. + * @return the result + */ + public Object open() { + createContents(); + shell.open(); + shell.layout(); + Display display = getParent().getDisplay(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) { + display.sleep(); + } + } + return result; + } + + /** + * Create contents of the dialog. + */ + private void createContents() { + shell = new Shell(getParent(), getStyle()); + shell.setSize(450, 148); + shell.setText(getText()); + + ProgressBar progressBar = new ProgressBar(shell, SWT.NONE); + progressBar.setBounds(10, 52, 424, 25); + + Button btnCancel = new Button(shell, SWT.NONE); + btnCancel.setBounds(10, 83, 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); + + } +} diff --git a/src/me/puyodead1/filedownloader/Downloader.java b/src/me/puyodead1/filedownloader/Downloader.java new file mode 100644 index 0000000..7ffb0e7 --- /dev/null +++ b/src/me/puyodead1/filedownloader/Downloader.java @@ -0,0 +1,217 @@ +package me.puyodead1.filedownloader; + +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.io.PrintWriter; +import java.io.RandomAccessFile; +import java.io.StringWriter; +import java.net.HttpURLConnection; +import java.net.URL; + +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; + +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 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 URL url; + private int size; + private int downloaded; + private int status; + private String output; + + public Downloader(Shell parent, URL url, String output) { + this.parentShell = parent; + this.url = url; + this.output = output; + size = -1; + downloaded = 0; + status = DOWNLOADING; + + download(); + } + + public String getURL() { + return url.toString(); + } + + public int getSize() { + return size; + } + + public float getProgress() { + return ((float) downloaded / size) * 100; + } + + public int getStatus() { + return status; + } + + private void error() { + status = ERROR; + stateChanged(); + } + + private void download() { + Thread thread = new Thread(this); + thread.start(); + } + + public void run() { + RandomAccessFile file = null; + InputStream stream = null; + + try { + // Open connection to URL. + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + + // Specify what portion of file to download. + connection.setRequestProperty("Range", "bytes=" + downloaded + "-"); + + // Connect to server. + connection.connect(); + + // Make sure response code is in the 200 range. + if (connection.getResponseCode() / 100 != 2) { + error(); + System.out.println("Invalid response code"); + } + + // Check for valid content length. + int contentLength = connection.getContentLength(); + if (contentLength < 1) { + error(); + System.out.println("Invalid content length"); + } + + /* + * Set the size for this download if it hasn't been already set. + */ + if (size == -1) { + size = contentLength; + stateChanged(); + } + + // 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(); + } + + /* + * Change status to complete if this point was reached because downloading has + * finished. + */ + if (status == DOWNLOADING) { + status = COMPLETE; + stateChanged(); + } + } 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(); + } + } catch (Exception e) { + error(); + StringWriter writer = new StringWriter(); + e.printStackTrace(new PrintWriter(writer)); + System.out.println(writer.toString()); + } finally { + // Close file. + if (file != null) { + try { + file.close(); + } catch (Exception e) { + StringWriter writer = new StringWriter(); + e.printStackTrace(new PrintWriter(writer)); + System.out.println(writer.toString()); + } + } + + // Close connection to server. + if (stream != null) { + try { + stream.close(); + } catch (Exception e) { + StringWriter writer = new StringWriter(); + e.printStackTrace(new PrintWriter(writer)); + System.out.println(writer.toString()); + } + } + } + } + + private void stateChanged() { + if (status == ERROR) { + Display.getDefault().asyncExec(new Runnable() { + + @Override + public void run() { + 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() { + public void run() { + // DownloadDialog.progressBar.setText(progress + "%"); + System.out.println(progress + "%"); + } + }); + } else if (status == COMPLETE) { + Display.getDefault().asyncExec(new Runnable() { + + @Override + public void run() { + final MessageDialog dialog = new MessageDialog(parentShell, parentShell.getStyle(), "Complete", + "File download complete!"); + dialog.open(); + + } + }); + } + } +} diff --git a/src/me/puyodead1/filedownloader/FileDownloader.java b/src/me/puyodead1/filedownloader/FileDownloader.java new file mode 100644 index 0000000..3124419 --- /dev/null +++ b/src/me/puyodead1/filedownloader/FileDownloader.java @@ -0,0 +1,113 @@ +package me.puyodead1.filedownloader; + +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; + +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.DirectoryDialog; +import org.eclipse.swt.events.MouseAdapter; +import org.eclipse.swt.events.MouseEvent; + +public class FileDownloader { + private static final List allowedExts = Arrays.asList("mp4", "webm", "mp3", "wav", "iso", "rar", "zip", "java"); + + private static Text txtUrl; + private static Text txtSavePath; + + /** + * Launch the application. + * + * @param args + */ + public static void main(String[] args) { + Display display = Display.getDefault(); + Shell shell = new Shell(); + shell.setSize(755, 434); + shell.setText("SWT Application"); + + txtUrl = new Text(shell, SWT.BORDER); + txtUrl.setBounds(10, 106, 719, 21); + + Label lblUrl = new Label(shell, SWT.NONE); + lblUrl.setAlignment(SWT.CENTER); + lblUrl.setBounds(10, 85, 719, 15); + lblUrl.setText("URL"); + + Button btnDownload = new Button(shell, SWT.NONE); + btnDownload.addMouseListener(new MouseAdapter() { + @Override + public void mouseDown(MouseEvent e) { + if (!Objects.isNull(txtSavePath.getText()) && !txtSavePath.getText().isEmpty()) { + final String url = txtUrl.getText(); + if (!Objects.isNull(url) && !url.isEmpty()) { + final String[] fileNameSplit = url.split("/"); + if (fileNameSplit.length > 0) { + final String fileName = fileNameSplit[fileNameSplit.length - 1]; + final String[] fileExtSplit = fileName.split("\\."); + if (fileExtSplit.length > 0) { + final String fileExt = fileExtSplit[fileExtSplit.length - 1]; + if (allowedExts.contains(fileExt)) { + final String savePath = txtSavePath.getText(); + new DownloadDialog(shell, shell.getStyle(), url, fileName, savePath); + } else { + final MessageDialog dialog = new MessageDialog(shell, shell.getStyle(), "Error", + "File extension not allowed!"); + dialog.open(); + } + } else { + System.out.println("no file ext"); + } + } else { + System.out.println("no file paths"); + } + } else { + System.out.println("url is empty or null"); + } + } else { + System.out.println("save path empty or null"); + } + } + }); + btnDownload.setBounds(10, 202, 719, 25); + btnDownload.setText("Download"); + + txtSavePath = new Text(shell, SWT.BORDER | SWT.READ_ONLY | SWT.H_SCROLL | SWT.CANCEL); + txtSavePath.setEditable(false); + txtSavePath.setBounds(128, 175, 601, 21); + + Button btnBrowse = new Button(shell, SWT.NONE); + btnBrowse.addMouseListener(new MouseAdapter() { + @Override + public void mouseDown(MouseEvent e) { + DirectoryDialog dialog = new DirectoryDialog(shell); + dialog.setMessage("Choose Save Location"); + String dir = dialog.open(); + if (dir != null) { + txtSavePath.setText(dir); + } + } + }); + btnBrowse.setBounds(10, 175, 112, 21); + btnBrowse.setText("Browse"); + + Label lblSaveTo = new Label(shell, SWT.NONE); + lblSaveTo.setAlignment(SWT.CENTER); + lblSaveTo.setBounds(10, 154, 719, 15); + lblSaveTo.setText("Save To"); + + shell.open(); + shell.layout(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) { + display.sleep(); + } + } + } +} diff --git a/src/me/puyodead1/filedownloader/MessageDialog.java b/src/me/puyodead1/filedownloader/MessageDialog.java new file mode 100644 index 0000000..0bd4831 --- /dev/null +++ b/src/me/puyodead1/filedownloader/MessageDialog.java @@ -0,0 +1,73 @@ +package me.puyodead1.filedownloader; + +import org.eclipse.swt.widgets.Dialog; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.events.MouseAdapter; +import org.eclipse.swt.events.MouseEvent; + +public class MessageDialog extends Dialog { + + protected Object result; + protected Shell shlMessage; + private String message; + private String title; + + /** + * Create the dialog. + * + * @param parent + * @param style + */ + public MessageDialog(Shell parent, int style, String title, String message) { + super(parent, style); + this.title = title; + this.message = message; + } + + /** + * Open the dialog. + * + * @return the result + */ + public Object open() { + createContents(); + shlMessage.open(); + shlMessage.layout(); + Display display = getParent().getDisplay(); + while (!shlMessage.isDisposed()) { + if (!display.readAndDispatch()) { + display.sleep(); + } + } + return result; + } + + /** + * Create contents of the dialog. + */ + private void createContents() { + shlMessage = new Shell(getParent(), getStyle()); + shlMessage.setSize(450, 165); + shlMessage.setText(title); + + Label lblMessage = new Label(shlMessage, SWT.WRAP | SWT.CENTER); + lblMessage.setAlignment(SWT.CENTER); + lblMessage.setBounds(10, 10, 424, 85); + lblMessage.setText(message); + + Button btnClose = new Button(shlMessage, SWT.NONE); + btnClose.addMouseListener(new MouseAdapter() { + @Override + public void mouseDown(MouseEvent e) { + shlMessage.close(); + } + }); + btnClose.setBounds(10, 101, 424, 25); + btnClose.setText("Close"); + + } +}