package com.nyohh.flashget.ui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;
import javax.swing.Timer;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import com.nyohh.flashget.ContextHolder;
import com.nyohh.flashget.DownloadContext;
import com.nyohh.flashget.info.Info;
import com.nyohh.flashget.navigation.DownloadNode;
import com.nyohh.flashget.navigation.DownloadingNode;
import com.nyohh.flashget.navigation.FailNode;
import com.nyohh.flashget.navigation.FinishNode;
import com.nyohh.flashget.navigation.TaskNode;
import com.nyohh.flashget.object.Resource;
import com.nyohh.flashget.state.Downloading;
import com.nyohh.flashget.state.Failed;
import com.nyohh.flashget.state.Finished;
import com.nyohh.flashget.state.Pause;
import com.nyohh.flashget.util.DateUtil;
import com.nyohh.flashget.util.FileUtil;
import com.nyohh.flashget.util.ImageUtil;
/**
* 主界面
*
* @author yulei
* @Date: 2013年11月13日
* @Copyright: 2013 www.nyohh.com Inc. All rights reserved.
*/
public class MainFrame extends JFrame
{
private static final long serialVersionUID = -7385308668156588694L;
private Dimension screen; // 得到屏幕大小
private static final String FILE_SIZE_TEXT = "文件大小: ";
private static final String FILE_PATH_TEXT = "文件路径: ";
private static final String DOWNLOAD_DATE_TEXT = "下载时间: ";
private static final String RESOURCE_INFO_TEXT = "资源信息: ";
private ArrayList<Info> infoList; // 信息对象数据
// 信息列表信息
private Info fileSize, info, downloadDate, filePath;
private TaskNode taskNode; // 所有任务节点
private NavigationTree navTree; // 导航树
private DownloadTable downloadTable; // 下载列表
private JList<Object> infoJList; // 信息列表
private JToolBar toolBar; // 工具栏
private TrayIcon trayIcon; // 任务栏图标
// 任务栏菜单
private MenuItem quitItem;
private MenuItem removeItem;
private MenuItem pauseItem;
private MenuItem startItem;
private MenuItem newItem;
private MenuItem openItem;
private NewTaskFrame taskFrame; // 新建任务界面
private DownloadNode currentNode = taskNode; // 当前用户浏览的节点
private Action newTask = new AbstractAction("新任务", new ImageIcon("images/tool/new-download.gif"))
{
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e)
{
newTask();
}
};
private void newTask()
{
taskFrame.setVisible(true);
}
private Action start = new AbstractAction("开始", new ImageIcon("images/tool/do-download.gif"))
{
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e)
{
start();
}
};
private void start()
{
Resource r = getResource();
if (r == null)
{
return;
}
if (r.getState() instanceof Pause || r.getState() instanceof Failed)
{
ContextHolder.dh.resumeDownload(r);
}
}
/**
* 得到用户在列表中所选择的资源
*/
private Resource getResource()
{
int row = this.downloadTable.getSelectedRow();
int column = this.downloadTable.getColumn(DownloadTableModel.ID_COLUMN).getModelIndex();
if (row == -1)
{
return null;
}
String id = (String) this.downloadTable.getValueAt(row, column);
return ContextHolder.ctx.getResource(id);
}
private Action pause = new AbstractAction("暂停", new ImageIcon("images/tool/pause.gif"))
{
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e)
{
pause();
}
};
private void pause()
{
Resource r = getResource();
if (r == null)
{
return;
}
// 判断状态
if (!(r.getState() instanceof Downloading))
{
return;
}
r.setState(DownloadContext.PAUSE);
}
private Action delete = new AbstractAction("删除任务", new ImageIcon("images/tool/delete.gif"))
{
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e)
{
delete();
}
};
/**
* 删除资源
*/
private void delete()
{
Resource r = getResource();
if (r == null)
{
return;
}
// 先将任务停止
r.setState(DownloadContext.PAUSE);
// 删除所有的.part文件
FileUtil.deletePartFiles(r);
// 从上下文集合中删除资源
ContextHolder.ctx.resources.remove(r);
}
private Action deleteFinished = new AbstractAction("删除任务", new ImageIcon("images/tool/remove-finished.gif"))
{
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e)
{
deleteFinished();
}
};
/**
* 删除已下载完成的资源
*/
public void deleteFinished()
{
for (Iterator<Resource> it = ContextHolder.ctx.resources.iterator(); it.hasNext();)
{
Resource r = it.next();
if (r.getState() instanceof Finished)
{
it.remove();
}
}
}
public MainFrame()
{
screen = Toolkit.getDefaultToolkit().getScreenSize(); // 得到屏幕大小
taskFrame = new NewTaskFrame(); // 新建任务界面
initialization();
// 创建任务栏图标
createTrayIcon();
// 创建悬浮窗口
new SuspendWindow(this);
initlisteners();
// 创建定时器
Timer timer = new Timer(1000, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
downloadTable.updateUI(); // 刷新列表
}
});
timer.start();
// 读取序列化文件
reverseSer();
}
/**
* 反序列化
*/
private void reverseSer()
{
File serFile = FileUtil.SERIALIZABLE_FILE;
if (!serFile.exists())
{
return;
}
try
{
// 得到文件输入流
FileInputStream fis = new FileInputStream(serFile);
ObjectInputStream ois = new ObjectInputStream(fis);
// 设置ContextHolder的DownloadContext
ContextHolder.ctx = (DownloadContext) ois.readObject();
ois.close();
fis.close();
}
catch (Exception e)
{
e.printStackTrace();
}
// 设置列表
addTableData();
}
private void addTableData()
{
DownloadTableModel model = (DownloadTableModel) this.downloadTable.getModel();
// 将保存的资源设置到列表中
model.setResources(ContextHolder.ctx.resources);
// 刷新列表
this.downloadTable.refresh();
}
private void initlisteners()
{
// 任务栏图标监听器
this.trayIcon.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2)
{
setVisible(true);
}
}
});
this.openItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
if (isVisible())
{
setVisible(false);
}
else
{
setVisible(true);
}
}
});
this.newItem.addActionListener(new
没有合适的资源?快使用搜索试试~ 我知道了~
多线程下载工具(练手项目十一)
共124个文件
class:65个
java:35个
gif:22个
3星 · 超过75%的资源 需积分: 10 18 下载量 127 浏览量
2013-11-15
23:55:02
上传
评论 1
收藏 116KB ZIP 举报
温馨提示
Java SE学习完成后,自己练手项目十一 这个项目里面多出了:任务栏图标功能实现,和浮动图标的实现(SystemTray 、JWindow) 巩固了线程、序列化等概念,再次使用树形、表格 留心主界面的关闭按钮,我改成隐藏界面,并不是前面的缺省关闭了。
资源推荐
资源详情
资源评论
收起资源包目录
多线程下载工具(练手项目十一) (124个子文件)
MainFrame.class 15KB
NewTaskFrame.class 6KB
DownloadThread.class 5KB
Resource.class 5KB
SuspendWindow.class 4KB
DownloadHandler.class 4KB
DownloadTableModel.class 3KB
DownloadContext.class 3KB
MainFrame$15.class 2KB
ImageUtil.class 2KB
FileUtil.class 2KB
DownloadTable.class 2KB
Part.class 2KB
SuspendWindow$2.class 2KB
DownloadProgressBar.class 1KB
NavigationTreeCellRender.class 1KB
Failed.class 1KB
DownloadTableCellRenderer.class 1KB
NavigationTree.class 1KB
NewTaskFrame$FolderChooser.class 1KB
Finished.class 1KB
SuspendWindow$4.class 1KB
SuspendWindow$3.class 1017B
SuspendWindow$1.class 1013B
Pause.class 1012B
SuspendWindow$8.class 972B
MainFrame$3.class 970B
MainFrame$4.class 970B
MainFrame$2.class 970B
MainFrame$1.class 970B
NewTaskFrame$2.class 969B
MainFrame$5.class 943B
DownloadThreadTest.class 936B
SuspendWindow$7.class 920B
SuspendWindow$5.class 918B
SuspendWindow$6.class 918B
MainFrame$9.class 902B
DownloadHandler$CountTimeTask.class 882B
MainFrame$6.class 870B
MainFrame$8.class 851B
MainFrame$7.class 830B
MainFrame$13.class 814B
NewTaskFrame$4.class 780B
NewTaskFrame$1.class 770B
NewTaskFrame$3.class 770B
MainFrame$12.class 762B
MainFrame$11.class 760B
MainFrame$10.class 760B
MainFrame$14.class 736B
MainFrame$16.class 723B
Downloading.class 697B
DownloadingNode.class 693B
Connecting.class 692B
FinishNode.class 673B
AbstractState.class 673B
FailNode.class 665B
DateUtil.class 665B
TaskNode.class 659B
Info.class 633B
NewTaskFrameTest.class 625B
ContextHolder.class 613B
URLException.class 451B
Main.class 445B
TaskState.class 315B
DownloadNode.class 227B
.classpath 275B
flashget.gif 2KB
remove-finished.gif 2KB
pause.gif 1KB
do-download.gif 1KB
new-download.gif 1KB
delete.gif 1KB
flashget.gif 1KB
downloading.gif 1KB
flashget-trayicon.gif 1KB
connecting.gif 968B
fail.gif 949B
finish.gif 932B
open.gif 923B
downloading.gif 923B
start.gif 923B
pause.gif 884B
pause.gif 884B
finished.gif 630B
remove.gif 610B
failed.gif 203B
quit.gif 102B
add.gif 87B
MainFrame.java 16KB
NewTaskFrame.java 8KB
Resource.java 5KB
DownloadThread.java 4KB
SuspendWindow.java 4KB
DownloadHandler.java 3KB
DownloadTableModel.java 3KB
ImageUtil.java 3KB
DownloadTable.java 2KB
DownloadContext.java 2KB
FileUtil.java 1KB
Part.java 1KB
共 124 条
- 1
- 2
资源评论
- fate09282014-09-04程序代码比较实用
小行星0906
- 粉丝: 7
- 资源: 29
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功