?? 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 + -