?? selectfrienddialog.java
字號:
/* * @(#) SelectFriendDialog.java * Copyright 2004 HWStudio. All rights reserved. */package hws.item.smart.dialog;//導(dǎo)入核心Java類庫import java.io.IOException;import java.io.StringReader;import java.awt.Insets;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.JButton;import javax.swing.JDialog;import javax.swing.JSplitPane;import javax.swing.JOptionPane;import javax.swing.JScrollPane;import javax.swing.ListSelectionModel;import javax.swing.table.DefaultTableModel;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;//導(dǎo)入自定義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.misc.OptionalInfoPanel2;import hws.item.smart.utility.chat.UserInfo;import hws.item.smart.utility.chat.OptionalInfo;//導(dǎo)入第三方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-25 * @author Hwerz */public class SelectFriendDialog extends JDialog { /*------------------------------------------------------------------------* * 屬性定義 * *------------------------------------------------------------------------*/ /** * 基本信息面板 */ private BasicInfoPanel basicInfoPanel; /** * 可選信息面板 */ private OptionalInfoPanel2 optionalInfoPanel; /** * 被選中用戶的ID */ private static String id; /*------------------------------------------------------------------------* * 構(gòu)造函數(shù) * *------------------------------------------------------------------------*/ /** * Create a new instance of this class */ public SelectFriendDialog() { super(Smart.getInstance(), "選擇好友", true); id = null; Container c = getContentPane(); c.setLayout(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); c.add(new Toolbar(), constraints); //分割條面板 optionalInfoPanel = new OptionalInfoPanel2(); basicInfoPanel = new BasicInfoPanel(); JSplitPane spliter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false, basicInfoPanel, optionalInfoPanel); spliter.setOneTouchExpandable(true); spliter.setDividerLocation(150); constraints.gridy = 1; constraints.weighty = 1.0; constraints.fill = GridBagConstraints.BOTH; constraints.insets = new Insets(5, 5, 5, 5); c.add(spliter, constraints); //設(shè)置對話框 setSize(600, 450); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); PopToolkit.makeWindowCenter(this); } /*------------------------------------------------------------------------* * 公共方法 * *------------------------------------------------------------------------*/ /** * 返回選中用戶的ID * * @return 選中用戶的ID */ public static String getSelectedUserID() { return id; } /*------------------------------------------------------------------------* * 私有方法 * *------------------------------------------------------------------------*/ /** * 從遠程服務(wù)器中取得所有注冊用戶的詳細信息 * * @return 所有注冊用戶的詳細信息 */ private List getAllUsers() { List users = new ArrayList(); 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(); List children = root.getChildren(); for (int i = 0; i < children.size(); i++) { Element child = (Element) children.get(i); users.add(UserInfo.userElement2UserInfo(child)); } } catch (ClassNotFoundException e) { JOptionPane.showMessageDialog(Smart.getInstance(), "系統(tǒng)找不到類庫“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(), "好友服務(wù)已關(guān)閉,請稍候再試!", StringShop.HINT_TITLE, JOptionPane.INFORMATION_MESSAGE); } catch (JDOMException e) { JOptionPane.showMessageDialog(Smart.getInstance(), "操作失敗,請稍候再試!", StringShop.HINT_TITLE, JOptionPane.INFORMATION_MESSAGE); } return users; } /** * 生成請求URL * * @return 生成的請求URL */ private String genURL() { StringBuffer url = new StringBuffer(); url.append("http://"); url.append(XMLConfig.getServicesIP()); url.append(":"); url.append(XMLConfig.getFriendPort()); url.append("/"); return url.toString(); } /** * 生成XML-RPC請求消息 * * @return 生成的XML-RPC請求消息 */ private XmlRpcRequest genRequest() { //請求方法 StringBuffer method = new StringBuffer(); method.append(XMLConfig.getFriendClass()); method.append("."); method.append(XMLConfig.getFriendMethod()); //請求參數(shù) 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 id = new Element("ID"); request.addContent(id); id.setText(ViewPanel.getInstance().getUserID()); return new Document(request); } /*------------------------------------------------------------------------* * 內(nèi)部類 * *------------------------------------------------------------------------*/ /** * 工具欄面板 */ 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); //刷新 button = new JButton("刷新", ImageShop.REFRESH_IMAGEICON); button.addActionListener(this); add(button); } /** * 實現(xiàn)接口ActionListener的方法 * * @param event the event that characterizes the action */ public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if (command.equals("確定") == true) { id = basicInfoPanel.getSelectedUserID(); SelectFriendDialog.this.dispose(); } else if (command.equals("取消") == true) { id = null; SelectFriendDialog.this.dispose(); } else { basicInfoPanel.reloadUsers(); } } } /** * 基本信息面板 */ class BasicInfoPanel extends JPanel implements ListSelectionListener { /** * 基本信息表格的視圖 */ private JTable userTable; /** * 基本信息表格的模型 */ private DefaultTableModel userModel; /** * Create a new instance of this class */ public BasicInfoPanel() { super(new GridBagLayout()); //所有用戶標簽 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(new JLabel("所有用戶"), constraints); //基本信息表格 Object[] header = {"ID", "昵稱"}; userModel = new DefaultTableModel(); userModel.setColumnIdentifiers(header); loadUsers(); userTable = new JTable(userModel) { public boolean isCellEditable(int row, int column) { return false; } }; userTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); userTable.getSelectionModel().addListSelectionListener(this); if (userModel.getRowCount() > 0) { userTable.getSelectionModel().setSelectionInterval(0, 0); } JScrollPane scroller = new JScrollPane(userTable, 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); } /** * 返回選中用戶的ID * * @return 選中用戶的ID */ public String getSelectedUserID() { String id = null; int row = userTable.getSelectedRow(); if (row != -1) { id = String.valueOf(userModel.getValueAt(row, 0)); } return id; } /** * 重載用戶 */ public void reloadUsers() { unloadUsers(); loadUsers(); if (userModel.getRowCount() > 0) { userTable.getSelectionModel().setSelectionInterval(0, 0); } } /** * 裝載用戶 */ private void loadUsers() { List users = getAllUsers(); for (int i = 0; i < users.size(); i++) { UserInfo info = (UserInfo) users.get(i); String id = info.getBasicInfo().getID(); String nickname = info.getBasicInfo().getNickname(); userModel.addRow(new Object[] {id, nickname}); } } /** * 卸載用戶 */ private void unloadUsers() { int count = userModel.getRowCount(); for (int i = 0; i < count; i++) { userModel.removeRow(i); } } /** * 實現(xiàn)接口ListSelectionListener的方法 * * @param event the event that characterizes the change */ public void valueChanged(ListSelectionEvent event) { int row = userTable.getSelectedRow(); if (row != -1) { UserInfo info = (UserInfo) getAllUsers().get(row); OptionalInfo info2 = info.getOptionalInfo(); optionalInfoPanel.setValue(info2); } else { optionalInfoPanel.setValue(null); } } }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -