import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
public class DownloadManager extends JFrame implements Observer {
private JTextField addTextField;
private DownloadsTableModel tableModel;
private JTable table;
private JButton pauseButton, resumButton, cancelButton, clearButton;
private Download selectDownload;
private boolean clearing;
public DownloadManager() {
setTitle("Download Manager");
setSize(640, 480);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
actionExit();
}
});
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
JMenuItem fileExitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X);
fileExitMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionExit();
}
});
fileMenu.add(fileExitMenuItem);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
JPanel addPanel = new JPanel();
addTextField = new JTextField(30);
addPanel.add(addTextField);
JButton addButton = new JButton("Add Download");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionAdd();
}
});
addPanel.add(addButton);
// JFileChooser jfChooser = new JFileChooser();
// jfChooser.setDialogTitle("Save");
// addPanel.add(jfChooser);
// JButton selectDirButton = new JButton("Save Dir");
// selectDirButton.addActionListener(new ActionListener(){
// public void actionPerformed(ActionEvent e){
// actionSelect();
// }
// });
// addPanel.add(selectDirButton);
tableModel = new DownloadsTableModel();
table = new JTable(tableModel);
table.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
tableSelectionChange();
}
});
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ProgressRenderer renderer = new ProgressRenderer(0, 100);
renderer.setStringPainted(true);
table.setDefaultRenderer(ProgressRenderer.class, renderer);
table.setRowHeight((int) renderer.getPreferredSize().getHeight());
JPanel downloadsPanel = new JPanel();
downloadsPanel.setBorder(BorderFactory.createTitledBorder("Download"));
downloadsPanel.setLayout(new BorderLayout());
downloadsPanel.add(new JScrollPane(table), BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
pauseButton = new JButton("Pause");
pauseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionPause();
}
});
pauseButton.setEnabled(false);
buttonPanel.add(pauseButton);
resumButton = new JButton("Resum");
resumButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionResum();
}
});
resumButton.setEnabled(false);
buttonPanel.add(resumButton);
cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionCancel();
}
});
cancelButton.setEnabled(false);
buttonPanel.add(cancelButton);
clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionClear();
}
});
clearButton.setEnabled(false);
buttonPanel.add(clearButton);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(addPanel, BorderLayout.NORTH);
getContentPane().add(downloadsPanel, BorderLayout.CENTER);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
}
private void actionExit() {
System.exit(0);
}
private void actionAdd() {
URL verifiedUrl = verfyUrl(addTextField.getText());
if (verifiedUrl != null) {
JFileChooser fc = new JFileChooser();
fc.showSaveDialog(this);
tableModel.addDownLoad(new Download(verifiedUrl,fc.getSelectedFile()));
addTextField.setText("");
} else {
JOptionPane.showMessageDialog(this, "Invalid Download Url",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
private URL verfyUrl(String url) {
if (!url.toLowerCase().startsWith("http://"))
return null;
URL verifiedUrl = null;
try {
verifiedUrl = new URL(url);
} catch (Exception e) {
return null;
}
if (verifiedUrl.getFile().length() < 2)
return null;
return verifiedUrl;
}
private void tableSelectionChange() {
if (selectDownload != null) {
selectDownload.deleteObserver(DownloadManager.this);
}
if (!clearing) {
selectDownload = tableModel.getDownload(table.getSelectedRow());
selectDownload.addObserver(DownloadManager.this);
updateButtons();
}
}
// private void actionSelect(){
//
// }
private void actionPause() {
selectDownload.pause();
updateButtons();
}
private void actionResum() {
selectDownload.resum();
updateButtons();
}
private void actionCancel() {
selectDownload.cancel();
updateButtons();
}
private void actionClear() {
clearing = true;
tableModel.cleanDownload(table.getSelectedRow());
clearing = false;
selectDownload = null;
updateButtons();
}
private void updateButtons() {
if (selectDownload != null) {
int status = selectDownload.getStatus();
switch (status) {
case Download.DOWNLOADING:
pauseButton.setEnabled(true);
resumButton.setEnabled(false);
cancelButton.setEnabled(true);
clearButton.setEnabled(false);
break;
case Download.PAUSED:
pauseButton.setEnabled(false);
resumButton.setEnabled(true);
cancelButton.setEnabled(true);
clearButton.setEnabled(false);
break;
case Download.ERROR:
pauseButton.setEnabled(false);
resumButton.setEnabled(true);
cancelButton.setEnabled(false);
clearButton.setEnabled(true);
break;
default:
pauseButton.setEnabled(false);
resumButton.setEnabled(false);
cancelButton.setEnabled(false);
clearButton.setEnabled(true);
}
}else{
pauseButton.setEnabled(false);
resumButton.setEnabled(false);
cancelButton.setEnabled(false);
clearButton.setEnabled(false);
}
}
public void update(Observable o, Object arg) {
if(selectDownload!=null&&selectDownload.equals(o)){
updateButtons();
}
}
/**
* @param args
*/
public static void main(String[] args) {
DownloadManager manager = new DownloadManager();
manager.show();
}
}