📄 downloadpanel.java
字号:
/* * @(#) DownloadPanel.java * Copyright 2004 HWStudio. All rights reserved. */package hws.item.smart.panel.function.share;//导入核心Java类库import java.io.IOException;import java.io.StringReader;import java.awt.Insets;import java.awt.Component;import java.awt.FlowLayout;import java.awt.GridBagLayout;import java.awt.GridBagConstraints;import java.net.MalformedURLException;import java.util.List;import java.util.Vector;import java.util.ArrayList;import javax.swing.JTree;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTable;import javax.swing.JButton;import javax.swing.JSplitPane;import javax.swing.JOptionPane;import javax.swing.JScrollPane;import javax.swing.ListSelectionModel;import javax.swing.tree.TreePath;import javax.swing.tree.DefaultTreeModel;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.DefaultTreeCellRenderer;import javax.swing.tree.DefaultTreeSelectionModel;import javax.swing.event.TreeSelectionEvent;import javax.swing.event.TreeSelectionListener;import javax.swing.table.DefaultTableModel;//导入自定义Java类库import hws.item.smart.Smart;import hws.item.smart.misc.ImageShop;import hws.item.smart.misc.SBChanger;import hws.item.smart.misc.XMLConfig;import hws.item.smart.misc.StringShop;import hws.item.smart.panel.function.chat.ViewPanel;import hws.item.smart.action.share.download.DownloadAction;import hws.item.smart.utility.chat.UserInfo;import hws.item.smart.utility.share.RequestAgent;import hws.item.smart.utility.share.TransferAgent;import hws.item.smart.utility.share.ProgressRenderer;//导入第三方Java类库import org.jdom.Element;import org.jdom.Document;import org.jdom.JDOMException;import org.jdom.input.SAXBuilder;import org.jdom.output.XMLOutputter;import org.apache.xmlrpc.XmlRpc;import org.apache.xmlrpc.XmlRpcClient;import org.apache.xmlrpc.XmlRpcRequest;import org.apache.xmlrpc.XmlRpcException;/** * 下载文件面板 * * @version 0.1 2005-08-30 * @author Hwerz */public class DownloadPanel extends JPanel { /*------------------------------------------------------------------------* * 属性定义 * *------------------------------------------------------------------------*/ /** * 好友树根节点 */ private static final DefaultMutableTreeNode FRIEND_ROOT = new DefaultMutableTreeNode("我的好友(未登录)"); /** * 文件树根节点 */ private static final DefaultMutableTreeNode FILE_ROOT = new DefaultMutableTreeNode("下载文件"); /** * 该类自身的一个静态引用 */ private static DownloadPanel panel; /** * 好友视图面板 */ private FriendViewPanel friendViewPanel; /** * 文件视图面板 */ private FileViewPanel fileViewPanel; /** * 任务列表面板 */ private TaskListPanel taskListPanel; /*------------------------------------------------------------------------* * 构造函数 * *------------------------------------------------------------------------*/ /** * 构造函数为私有,这样在整个运行过程中该类就只能有一个实例 */ private DownloadPanel() { super(new GridBagLayout()); //工具栏面板 GridBagConstraints constraints = new GridBagConstraints( //gridx, gridy 0, 0, //gridwidth, gridheight 1, 1, //weightx, weighty 1.0, 0.0, //anchor GridBagConstraints.NORTH, //fill GridBagConstraints.HORIZONTAL, //insets new Insets(5, 0, 0, 0), //ipadx, ipady 0, 0); add(new Toolbar(), constraints); //分割条面板 taskListPanel = new TaskListPanel(); fileViewPanel = new FileViewPanel(); friendViewPanel = new FriendViewPanel(); JSplitPane spliter1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false, fileViewPanel, taskListPanel); spliter1.setBorder(null); spliter1.setOneTouchExpandable(true); spliter1.setDividerLocation(180); JSplitPane spliter2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false, friendViewPanel, spliter1); spliter2.setOneTouchExpandable(true); spliter2.setDividerLocation(180); constraints.gridy = 1; constraints.weighty = 1.0; constraints.fill = GridBagConstraints.BOTH; constraints.insets = new Insets(5, 5, 5, 5); add(spliter2, constraints); } /*------------------------------------------------------------------------* * 公共方法 * *------------------------------------------------------------------------*/ /** * 对该类提供的一个全局访问点,用来实例化该对象 * * @return 该类唯一的一个实例 */ public static DownloadPanel getInstance() { if (panel == null) { panel = new DownloadPanel(); } return panel; } /** * 设置好友信息 * * @param info 待设置的好友信息 */ public void setValue(UserInfo info) { friendViewPanel.setValue(info); String id = info.getBasicInfo().getID(); RequestAgent.getInstance(id).start(); TransferAgent.getInstance(id).start(); } /** * 添加好友 * * @param id 待添加好友的ID */ public void addFriend(String id) { friendViewPanel.addFriend(id); } /** * 删除好友 * * @param id 待删除好友的ID */ public void deleteFriend(String id) { friendViewPanel.deleteFriend(id); } /** * 返回选中好友的ID * * @return 选中好友的ID */ public String getSelectedFriendID() { return friendViewPanel.getSelectedFriendID(); } /** * 返回选中的下载文件 * * @return 选中的下载文件 */ public String getSelectedFile() { return fileViewPanel.getSelectedFile(); } /** * 添加下载 * * @param file 文件名 * @param progress 下载进度 */ public void addDownload(String file, int progress) { taskListPanel.addRow(file, progress); } /** * 删除下载 * * @param file 文件名 */ public void deleteDownload(String file) { taskListPanel.deleteRow(file); } /** * 设置下载 * * @param file 文件名 * @param progress 下载进度 */ public void setDownload(String file, int progress) { taskListPanel.setProgress(file, progress); } /*------------------------------------------------------------------------* * 私有方法 * *------------------------------------------------------------------------*/ /** * 生成请求URL * * @return 生成的请求URL */ private String genURL() { StringBuffer url = new StringBuffer(); url.append("http://"); url.append(XMLConfig.getServicesIP()); url.append(":"); url.append(XMLConfig.getGettingPort()); url.append("/"); return url.toString(); } /** * 生成XML-RPC请求消息 * * @return 生成的XML-RPC请求消息 */ private XmlRpcRequest genRequest() { //请求方法 StringBuffer method = new StringBuffer(); method.append(XMLConfig.getGettingClass()); method.append("."); method.append(XMLConfig.getGettingMethod()); //请求参数 Vector params = new Vector(); XMLOutputter outputter = new XMLOutputter(); params.addElement(outputter.outputString(genDocument())); return new XmlRpcRequest(method.toString(), params); } /** * 生成XML文档 * * @return 生成的XML文档 */ private Document genDocument() { Element request = new Element("Request"); Element level1 = new Element("ID"); level1.setText(ViewPanel.getInstance().getUserID()); request.addContent(level1); return new Document(request); } /** * 设置树型组件的附加选项 * * @param tree 待设置的树型组件 */ private void setTree(JTree tree) { DefaultTreeSelectionModel model = new DefaultTreeSelectionModel(); model.setSelectionMode(model.SINGLE_TREE_SELECTION); tree.setSelectionModel(model); tree.setSelectionRow(0); tree.expandRow(0); } /*------------------------------------------------------------------------* * 内部类 * *------------------------------------------------------------------------*/ /** * 工具栏面板 */ class Toolbar extends JPanel { /** * Create a new instance of this class */ public Toolbar() { super(new FlowLayout(FlowLayout.CENTER, 5, 0)); //下载 JButton button = new JButton(DownloadAction.getInstance()); button.setIcon(ImageShop.DOWNLOAD_IMAGEICON); button.addMouseListener(new SBChanger( DownloadAction.getInstance().getHintInfo(), false)); add(button); } } /** * 好友视图面板 */ class FriendViewPanel extends JPanel implements TreeSelectionListener { /** * 好友视图标签 */ private JLabel label; /** * 我的好友树的视图 */ private JTree friendTree; /** * 我的好友树的模型 */ private DefaultTreeModel friendModel; /** * Create a new instance of this class */ public FriendViewPanel() { super(new GridBagLayout()); //好友视图标签 label = new JLabel("好友视图"); GridBagConstraints constraints = new GridBagConstraints( //gridx, gridy 0, 0, //gridwidth, gridheight 1, 1, //weightx, weighty 0.0, 0.0, //anchor GridBagConstraints.NORTHWEST, //fill GridBagConstraints.NONE, //insets new Insets(0, 5, 0, 0), //ipadx, ipady 0, 0); add(label, constraints); //树型组件 friendModel = new DefaultTreeModel(FRIEND_ROOT); friendTree = new JTree(friendModel); friendTree.addTreeSelectionListener(this); setTree(friendTree); setTreeCellRenderer(); JScrollPane scroller = new JScrollPane(friendTree, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); constraints.gridy = 1; constraints.weightx = 1.0; constraints.weighty = 1.0; constraints.fill = GridBagConstraints.BOTH; constraints.insets = new Insets(0, 0, 0, 0); add(scroller, constraints); } /** * 设置好友信息 * * @param info 待设置的好友信息 */ public void setValue(UserInfo info) { if (info != null) { unloadFriends(); loadFriends(info); FRIEND_ROOT.setUserObject("我的好友(已登录)"); } } /** * 添加好友 * * @param id 待添加好友的ID */ public void addFriend(String id) { if (id != null) { DefaultMutableTreeNode child = new DefaultMutableTreeNode(id); int count = FRIEND_ROOT.getChildCount(); friendModel.insertNodeInto(child, FRIEND_ROOT, count); friendTree.expandRow(0); friendTree.setSelectionRow(count + 1); } } /** * 删除好友 * * @param id 待删除好友的ID */ public void deleteFriend(String id) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -