www.pudn.com > htmlsaver.rar > DownloadProject.java
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class DownloadProject implements ActionListener
{
public Project prj;
public Object[] names;
public boolean[] down;
public boolean isOver;
protected JDialog dialog;
protected JTable table;
protected DownTableModel model;
protected JButton auto;
protected JButton user;
protected JButton over;
protected boolean inAutoDownload;
public DownloadProject(Project prj, JFrame owner)
{
this.prj = prj;
names = prj.names.values().toArray();
down = new boolean[names.length];
for (int i = 0; i < down.length; i++)
down[i] = false;
isOver = false;
inAutoDownload = false;
dialog = new JDialog(owner, "Download Project " + prj.prjName, true);
initDialog();
}
public void initDialog()
{
dialog.getContentPane().setLayout(new BorderLayout());
model = new DownTableModel();
table = new JTable(model);
table.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
TableColumn column = null;
column = table.getColumnModel().getColumn(0);
column.setPreferredWidth(300);
column = table.getColumnModel().getColumn(1);
column.setPreferredWidth(100);
column = table.getColumnModel().getColumn(2);
column.setPreferredWidth(50);
dialog.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
JPanel p = new JPanel(new GridLayout(1, 3));
auto = new JButton(/*"Auto Download"*/"自动下载");
auto.addActionListener(this);
user = new JButton(/*"User Download"*/"单独下载");
user.addActionListener(this);
over = new JButton(/*"Stop Download"*/"结束下载");
over.addActionListener(this);
p.add(auto);
p.add(user);
p.add(over);
dialog.getContentPane().add(p, BorderLayout.SOUTH);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setSize(500, 300);
dialog.show();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == auto)
autoDownload();
else if (e.getSource() == user)
userDownload();
else if (e.getSource() == over)
{
isOver = true;
refresh();
}
}
class DownTableModel extends AbstractTableModel
{
String[] columnNames = {"URL", "Name", "Status"};
public String getColumnName(int col) {
return columnNames[col].toString();
}
public int getRowCount() { return names.length; }
public int getColumnCount() { return 3; }
public Object getValueAt(int row, int col) {
if (col == 0)
return ((URLName)names[row]).url;
else if (col == 1)
return ((URLName)names[row]).name;
else
{
if (down[row] == true)
return "下载中";
else if (((URLName)names[row]).finish)
return "完成";
else
return "未下载";
}
}
public void refresh()
{
fireTableDataChanged();
}
}
protected void autoDownload()
{
if (!inAutoDownload)
{
new AutoSaveURL(this);
inAutoDownload = true;
}
}
protected void userDownload()
{
if (table.getSelectedRowCount() != 1)
return;
int row = table.getSelectedRow();
boolean can = false;
synchronized (down)
{
if (!down[row])
{
down[row] = true;
can = true;
}
}
if (can)
{
new UserSaveURL(this, row).start();
model.refresh();
}
else
{
JOptionPane.showMessageDialog(dialog, "该页面已经在下载,不能进行操作");
}
}
public void refresh()
{
model.refresh();
if (isOver)
{
for (int i = 0; i < down.length; i++)
if (down[i])
return;
dialog.hide();
}
}
public static void main(String[] args)
{
Project project;
try {
project = new Project("f:\\other\\BTB.prj");
}
catch (Exception e) {
return;
}
new DownloadProject(project, new JFrame());
}
}