?? setsharedialog.java
字號:
/* * @(#) SetShareDialog.java * Copyright 2004 HWStudio. All rights reserved. */package hws.item.smart.dialog;//導入核心Java類庫import java.io.IOException;import java.io.StringReader;import java.awt.Insets;import java.awt.Component;import java.awt.Container;import java.awt.FlowLayout;import java.awt.GridBagLayout;import java.awt.GridBagConstraints;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.net.MalformedURLException;import java.util.List;import java.util.Vector;import java.util.ArrayList;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTable;import javax.swing.JDialog;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JTextField;import javax.swing.JOptionPane;import javax.swing.JScrollPane;import javax.swing.DefaultCellEditor;import javax.swing.table.TableColumn;import javax.swing.table.DefaultTableModel;import javax.swing.table.TableCellRenderer;//導入自定義Java類庫import hws.item.smart.Smart;import hws.item.smart.misc.ImageShop;import hws.item.smart.misc.XMLConfig;import hws.item.smart.misc.PopToolkit;import hws.item.smart.misc.StringShop;import hws.item.smart.panel.function.chat.ViewPanel;import hws.item.smart.panel.function.chat.FriendPanel;import hws.item.smart.panel.function.share.UploadPanel;import hws.item.smart.utility.chat.UserInfo;//導入第三方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-29 * @author Hwerz */public class SetShareDialog extends JDialog { /*------------------------------------------------------------------------* * 屬性定義 * *------------------------------------------------------------------------*/ /** * 好友列表的視圖 */ private JTable friendTable; /** * 好友列表的模型 */ private DefaultTableModel friendModel; /** * 用戶ID */ private String id; /*------------------------------------------------------------------------* * 構造函數 * *------------------------------------------------------------------------*/ /** * Create a new instance of this class * * @param id 用戶ID */ public SetShareDialog(String id) { super(Smart.getInstance(), "設置共享", true); this.id = id; Container c = getContentPane(); c.setLayout(new GridBagLayout()); //工具欄面板 GridBagConstraints constraints = new GridBagConstraints( //gridx, gridy 0, 0, //gridwidth, gridheight GridBagConstraints.REMAINDER, 1, //weightx, weighty 1.0, 0.0, //anchor GridBagConstraints.NORTHWEST, //fill GridBagConstraints.HORIZONTAL, //insets new Insets(5, 0, 0, 0), //ipadx, ipady 0, 0); c.add(new Toolbar(), constraints); //共享文件標簽 constraints.gridy = 1; constraints.gridwidth = 1; constraints.weightx = 0.0; constraints.fill = GridBagConstraints.NONE; constraints.insets = new Insets(5, 5, 0, 0); c.add(new JLabel("共享文件:"), constraints); //共享文件文本框 JTextField fileTextField = new JTextField(UploadPanel.getInstance().getSelectedFile()); fileTextField.setEditable(false); constraints.gridx = 1; constraints.gridwidth = GridBagConstraints.REMAINDER; constraints.weightx = 1.0; constraints.fill = GridBagConstraints.HORIZONTAL; constraints.insets = new Insets(5, 5, 0, 5); c.add(fileTextField, constraints); //好友列表組件 Object[] header = {"ID", "昵稱", "姓名", "共享"}; friendModel = new DefaultTableModel(); friendModel.setColumnIdentifiers(header); List friendList1 = UploadPanel.getInstance().getAllShareFriends(); List friendList2 = FriendPanel.getInstance().getAllFriends(); for (int i = 0; i < friendList2.size(); i++) { Boolean selected = Boolean.FALSE; String friendID = friendList2.get(i).toString(); if (friendList1.contains(friendID)) { selected = Boolean.TRUE; } UserInfo user = UserInfo.getRemoteUserInfo(friendID); friendModel.addRow(new Object[] { friendID, user.getBasicInfo().getNickname(), user.getOptionalInfo().getName(), selected}); } friendTable = new JTable(friendModel) { public boolean isCellEditable(int row, int column) { boolean editable; if (column == friendModel.getColumnCount() - 1) { editable = true; } else { editable = false; } return editable; } }; TableColumn column = friendTable.getColumn("共享"); column.setCellRenderer(new CheckCellRenderer()); column.setCellEditor(new DefaultCellEditor(new JCheckBox())); JScrollPane scroller = new JScrollPane(friendTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); constraints.gridx = 0; constraints.gridy = 2; constraints.weighty = 1.0; constraints.fill = GridBagConstraints.BOTH; constraints.insets = new Insets(5, 5, 5, 5); c.add(scroller, constraints); //設置對話框 setSize(600, 450); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); PopToolkit.makeWindowCenter(this); } /*------------------------------------------------------------------------* * 私有方法 * *------------------------------------------------------------------------*/ /** * 設置共享 */ private void setShare() { try { XmlRpc.setDriver("org.apache.xerces.parsers.SAXParser"); XmlRpcClient client = new XmlRpcClient(genURL()); String response = client.execute(genRequest()).toString(); SAXBuilder builder = new SAXBuilder(); StringReader reader = new StringReader(response); Document document = builder.build(reader); Element root = document.getRootElement(); String result = root.getChild("Result").getText(); if (result.equals(StringShop.SET_SHARE_SUCCESS)) { JOptionPane.showMessageDialog(Smart.getInstance(), "設置共享成功!", StringShop.HINT_TITLE, JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(Smart.getInstance(), "設置共享失敗,請稍候再試!", StringShop.HINT_TITLE, JOptionPane.INFORMATION_MESSAGE); } } catch (ClassNotFoundException e) { JOptionPane.showMessageDialog(Smart.getInstance(), "系統找不到類庫“org.apache.xerces.parsers.SAXParser”!", StringShop.HINT_TITLE, JOptionPane.INFORMATION_MESSAGE); } catch (MalformedURLException e) { e.printStackTrace(); } catch (XmlRpcException e) { e.printStackTrace(); } catch (IOException e) { JOptionPane.showMessageDialog(Smart.getInstance(), "設置服務已關閉,請稍候再試!", StringShop.HINT_TITLE, JOptionPane.INFORMATION_MESSAGE); } catch (JDOMException e) { JOptionPane.showMessageDialog(Smart.getInstance(), "操作失敗,請稍候再試!", StringShop.HINT_TITLE, JOptionPane.INFORMATION_MESSAGE); } } /** * 生成請求URL * * @return 生成的請求URL */ private String genURL() { StringBuffer url = new StringBuffer(); url.append("http://"); url.append(XMLConfig.getServicesIP()); url.append(":"); url.append(XMLConfig.getSettingPort()); url.append("/"); return url.toString(); } /** * 生成XML-RPC請求消息 * * @return 生成的XML-RPC請求消息 */ private XmlRpcRequest genRequest() { //請求方法 StringBuffer method = new StringBuffer(); method.append(XMLConfig.getSettingClass()); method.append("."); method.append(XMLConfig.getSettingMethod()); //請求參數 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 share = new Element("Share"); request.addContent(share); share.setAttribute("id", ViewPanel.getInstance().getUserID()); share.setAttribute("file", UploadPanel.getInstance().getSelectedFile()); Element friends = new Element("Friends"); share.addContent(friends); List ids = getSelectedFriends(); for (int i = 0; i < ids.size(); i++) { Element friend = new Element("Friend"); friends.addContent(friend); friend.setAttribute("id", ids.get(i).toString()); } return new Document(request); } /** * 返回選中的好友列表 * * @return 選中的好友列表 */ private List getSelectedFriends() { List friends = new ArrayList(); for (int i = 0; i < friendModel.getRowCount(); i++) { if (friendModel.getValueAt(i, 3).toString().equals("true")) { friends.add(friendModel.getValueAt(i, 0)); } } return friends; } /*------------------------------------------------------------------------* * 內部類 * *------------------------------------------------------------------------*/ /** * 工具欄面板 */ class Toolbar extends JPanel implements ActionListener { /** * Create a new instance of this class */ public Toolbar() { super(new FlowLayout(FlowLayout.CENTER, 5, 0)); //確定 JButton button = new JButton("確定", ImageShop.OK_IMAGEICON); button.addActionListener(this); add(button); //取消 button = new JButton("取消", ImageShop.CANCEL_IMAGEICON); button.addActionListener(this); add(button); } /** * 實現接口ActionListener的方法 * * @param event the event that characterizes the action */ public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if (command.equals("確定") == true) { setShare(); UploadPanel.getInstance().refresh(); SetShareDialog.this.dispose(); } else { SetShareDialog.this.dispose(); } } } /** * 表格渲染器 */ class CheckCellRenderer extends JCheckBox implements TableCellRenderer { /** * Create a new instance of this class */ public CheckCellRenderer() { super(); setOpaque(true); } /** * 實現接口TableCellRenderer的方法 * * @param table the JTable * @param value the value to assign to the cell at [row, column] * @param isSelected true true if cell is selected * @param hasFocus true if cell has focus * @param row the row of the cell to render * @param column the column of the cell to render * @return the component used for drawing the cell */ public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (value instanceof Boolean) { Boolean b = (Boolean) value; setSelected(b.booleanValue()); } setBackground(isSelected && hasFocus == false ? table.getSelectionBackground() : table.getBackground()); setForeground(isSelected && hasFocus == false ? table.getSelectionForeground() : table.getForeground()); return this; } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -