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

📄 setsharedialog.java

📁 该系统是一个基于p2p的即时聊天系统
💻 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 + -