⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 selectfrienddialog.java

📁 该系统是一个基于p2p的即时聊天系统
💻 JAVA
字号:
/* * @(#) SelectFriendDialog.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.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;//导入自定义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;//导入第三方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;    /*------------------------------------------------------------------------*     *                                构造函数                                *     *------------------------------------------------------------------------*/    /**     * 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);        //设置对话框        setSize(600, 450);        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);        PopToolkit.makeWindowCenter(this);    }    /*------------------------------------------------------------------------*     *                                公共方法                                *     *------------------------------------------------------------------------*/    /**     * 返回选中用户的ID     *     * @return 选中用户的ID     */    public static String getSelectedUserID() {        return id;    }    /*------------------------------------------------------------------------*     *                                私有方法                                *     *------------------------------------------------------------------------*/    /**     * 从远程服务器中取得所有注册用户的详细信息     *     * @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(),                "系统找不到类库“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);        }        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());        //请求参数        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);    }    /*------------------------------------------------------------------------*     *                                 内部类                                 *     *------------------------------------------------------------------------*/    /**     * 工具栏面板     */    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);        }        /**         * 实现接口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);            }        }        /**         * 实现接口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);            }        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -