Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Just some improvements in the code, using lamba, logging and some other stuff #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ A download manager application written in Java.

##Usage
Use eclipse to open the project.
Also it's possible to use intellij idea, just do "import from csv"

##Contact
[@luugiathuy](http://twitter.com/luugiathuy)

[email protected]

17 changes: 12 additions & 5 deletions src/com/luugiathuy/apps/downloadmanager/DownloadManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.net.URL;
import java.util.ArrayList;
import java.util.logging.Logger;

public class DownloadManager {

Expand All @@ -35,16 +36,21 @@ public class DownloadManager {

// Constant variables
private static final int DEFAULT_NUM_CONN_PER_DOWNLOAD = 8;
public static final String DEFAULT_OUTPUT_FOLDER = "";
// public static final String DEFAULT_OUTPUT_FOLDER = "";
public static final String DEFAULT_OUTPUT_FOLDER = System.getProperty("user.home");

// Member variables
private int mNumConnPerDownload;
private ArrayList<Downloader> mDownloadList;


private static String HTTP_PREFIX = "http://";
private static Logger logger = Logger.getLogger(DownloadManager.class.getName());

/** Protected constructor */
protected DownloadManager() {
mNumConnPerDownload = DEFAULT_NUM_CONN_PER_DOWNLOAD;
mDownloadList = new ArrayList<Downloader>();
mDownloadList = new ArrayList<>();
}

/**
Expand All @@ -57,7 +63,7 @@ public int getNumConnPerDownload() {
/**
* Set the max number of connections per download
*/
public void SetNumConnPerDownload(int value) {
public void setNumConnPerDownload(int value) {
mNumConnPerDownload = value;
}

Expand Down Expand Up @@ -108,14 +114,15 @@ public static DownloadManager getInstance() {
*/
public static URL verifyURL(String fileURL) {
// Only allow HTTP URLs.
if (!fileURL.toLowerCase().startsWith("http://"))
if (!fileURL.toLowerCase().startsWith(HTTP_PREFIX))
return null;

// Verify format of URL.
URL verifiedUrl = null;
URL verifiedUrl;
try {
verifiedUrl = new URL(fileURL);
} catch (Exception e) {
logger.finest("url is not valid");
return null;
}

Expand Down
91 changes: 40 additions & 51 deletions src/com/luugiathuy/apps/downloadmanager/DownloadManagerGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,12 @@

package com.luugiathuy.apps.downloadmanager;

import javax.swing.*;
import java.net.URL;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.*;

import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class DownloadManagerGUI extends javax.swing.JFrame implements Observer{

Expand All @@ -45,22 +41,38 @@ public class DownloadManagerGUI extends javax.swing.JFrame implements Observer{
private Downloader mSelectedDownloader;

private boolean mIsClearing;


private static Logger logger = Logger.getLogger(DownloadManagerGUI.class.getName());

static {
Logger log = LogManager.getLogManager().getLogger("");
for (Handler h : log.getHandlers()) {
h.setLevel(Level.ALL);
h.setFormatter(new SimpleFormatter());
}

Logger logger = Logger.getLogger("com.luugiathuy");
for(Handler handler : logger.getHandlers()) {
handler.setLevel(Level.ALL);
handler.setFormatter(new SimpleFormatter());
}
logger.setLevel(Level.ALL);
}

/** Creates new form DownloadManagerGUI */
public DownloadManagerGUI() {
logger.finest("DownloadManagerGUI started");
mTableModel = new DownloadTableModel();
initComponents();
initialize();
}

private void initialize() {

logger.finest("default download location: " + System.getProperty("user.home"));

// Set up table
jtbDownload.getSelectionModel().addListSelectionListener(new
ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
tableSelectionChanged();
}
});
jtbDownload.getSelectionModel().addListSelectionListener(e -> tableSelectionChanged());

// Allow only one row at a time to be selected.
jtbDownload.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Expand Down Expand Up @@ -99,53 +111,29 @@ private void initComponents() {
setResizable(false);

jbnAdd.setText("Add Download");
jbnAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbnAddActionPerformed(evt);
}
});
jbnAdd.addActionListener(this::jbnAddActionPerformed);

jtbDownload.setModel(mTableModel);
jScrollPane1.setViewportView(jtbDownload);

jbnPause.setText("Pause");
jbnPause.setEnabled(false);
jbnPause.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbnPauseActionPerformed(evt);
}
});
jbnPause.addActionListener(this::jbnPauseActionPerformed);

jbnRemove.setText("Remove");
jbnRemove.setEnabled(false);
jbnRemove.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbnRemoveActionPerformed(evt);
}
});
jbnRemove.addActionListener(this::jbnRemoveActionPerformed);

jbnCancel.setText("Cancel");
jbnCancel.setEnabled(false);
jbnCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbnCancelActionPerformed(evt);
}
});
jbnCancel.addActionListener(this::jbnCancelActionPerformed);

jbnExit.setText("Exit");
jbnExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbnExitActionPerformed(evt);
}
});
jbnExit.addActionListener(this::jbnExitActionPerformed);

jbnResume.setText("Resume");
jbnResume.setEnabled(false);
jbnResume.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbnResumeActionPerformed(evt);
}
});
jbnResume.addActionListener(this::jbnResumeActionPerformed);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
Expand All @@ -172,7 +160,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addContainerGap())
);

layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jbnCancel, jbnExit, jbnPause, jbnRemove, jbnResume});
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, jbnCancel, jbnExit, jbnPause, jbnRemove, jbnResume);

layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
Expand Down Expand Up @@ -222,7 +210,8 @@ private void jbnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIR
}//GEN-LAST:event_jbnRemoveActionPerformed

private void jbnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbnExitActionPerformed
setVisible(false);
logger.finest("Exiting");
System.exit(1);
}//GEN-LAST:event_jbnExitActionPerformed

private void jbnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbnAddActionPerformed
Expand Down Expand Up @@ -313,13 +302,13 @@ public static void main(String args[]) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
logger.severe(e.getMessage());
}

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DownloadManagerGUI().setVisible(true);
}
});

java.awt.EventQueue.invokeLater(
() -> new DownloadManagerGUI().setVisible(true)
);
}

// Variables declaration - do not modify//GEN-BEGIN:variables
Expand Down
11 changes: 9 additions & 2 deletions src/com/luugiathuy/apps/downloadmanager/DownloadTableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public class DownloadTableModel extends AbstractTableModel implements Observer {

// These are the names for the table's columns.
private static final String[] columnNames = {"URL", "Size (KB)",
"Progress", "Status"};
"Progress", "Status", "Destination"};

// These are the classes for each column's values.
@SuppressWarnings("rawtypes")
private static final Class[] columnClasses = {String.class,
String.class, JProgressBar.class, String.class};
String.class, JProgressBar.class, String.class, String.class};

/**
* Add a new download to the table.
Expand Down Expand Up @@ -114,6 +114,13 @@ public Object getValueAt(int row, int col) {
return new Float(download.getProgress());
case 3: // Status
return Downloader.STATUSES[download.getState()];
case 4 : //Destination
{
int state = download.getState();
if(state == 2 ) return download.getOutputFolder() + download.getFileName();
else return "-";
}

}
return "";
}
Expand Down
25 changes: 19 additions & 6 deletions src/com/luugiathuy/apps/downloadmanager/Downloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@

package com.luugiathuy.apps.downloadmanager;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Observable;
import java.util.logging.Logger;

public abstract class Downloader extends Observable implements Runnable{


private static Logger logger = Logger.getLogger(Downloader.class.getName());


// Member variables
/** The URL to download the file */
protected URL mURL;
Expand Down Expand Up @@ -74,7 +79,7 @@ public abstract class Downloader extends Observable implements Runnable{

/**
* Constructor
* @param fileURL
* @param url
* @param outputFolder
* @param numConnections
*/
Expand All @@ -85,13 +90,13 @@ protected Downloader(URL url, String outputFolder, int numConnections) {

// Get the file name from url path
String fileURL = url.getFile();
mFileName = fileURL.substring(fileURL.lastIndexOf('/') + 1);
System.out.println("File name: " + mFileName);
mFileName = File.separator + fileURL.substring(fileURL.lastIndexOf('/') + 1);
logger.finest("File name:" + mFileName);
mFileSize = -1;
mState = DOWNLOADING;
mDownloaded = 0;

mListDownloadThread = new ArrayList<DownloadThread>();
mListDownloadThread = new ArrayList<>();
}

/**
Expand Down Expand Up @@ -122,7 +127,10 @@ public void cancel() {
public String getURL() {
return mURL.toString();
}



public String getFileName(){return mFileName;}

/**
* Get the downloaded file's size
*/
Expand All @@ -143,6 +151,11 @@ public float getProgress() {
public int getState() {
return mState;
}

/**
*
*/
public String getOutputFolder(){return mOutputFolder;}

/**
* Set the state of the downloader
Expand Down
Loading