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

📄 friendpanel.java

📁 该系统是一个基于p2p的即时聊天系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * @(#) FriendPanel.java * Copyright 2004 HWStudio. All rights reserved. */package hws.item.smart.panel.function.chat;//导入核心Java类库import java.awt.Insets;import java.awt.Component;import java.awt.FlowLayout;import java.awt.GridBagLayout;import java.awt.GridBagConstraints;import java.awt.event.MouseEvent;import java.awt.event.MouseAdapter;import java.util.List;import java.util.ArrayList;import javax.swing.JTree;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JButton;import javax.swing.JSplitPane;import javax.swing.JOptionPane;import javax.swing.JScrollPane;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;//导入自定义Java类库import hws.item.smart.Smart;import hws.item.smart.misc.ImageShop;import hws.item.smart.misc.SBChanger;import hws.item.smart.misc.StringShop;import hws.item.smart.misc.ActionCenter;import hws.item.smart.panel.function.chat.misc.ChatViewPanel;import hws.item.smart.panel.function.share.DownloadPanel;import hws.item.smart.action.chat.friend.ChattingAction;import hws.item.smart.action.chat.friend.AddFriendAction;import hws.item.smart.action.chat.friend.DeleteFriendAction;import hws.item.smart.dialog.SelectFriendDialog;import hws.item.smart.utility.chat.UserInfo;import hws.item.smart.utility.chat.ChatAgent;//导入第三方Java类库import net.infonode.util.Direction;import net.infonode.docking.View;import net.infonode.docking.RootWindow;import net.infonode.docking.DockingWindow;import net.infonode.docking.DockingWindowAdapter;import net.infonode.docking.util.ViewMap;import net.infonode.docking.util.DockingUtil;import net.infonode.docking.theme.DockingWindowsTheme;import net.infonode.docking.theme.SoftBlueIceDockingTheme;import net.infonode.docking.properties.TabWindowProperties;import net.infonode.docking.properties.RootWindowProperties;import net.infonode.docking.properties.WindowTabStateProperties;/** * 我的好友面板 * * @version 0.1 2005-08-26 * @author Hwerz */public class FriendPanel extends JPanel {    /*------------------------------------------------------------------------*     *                                属性定义                                *     *------------------------------------------------------------------------*/    /**     * 好友树根节点     */    private static final DefaultMutableTreeNode FRIEND_ROOT =        new DefaultMutableTreeNode("我的好友(未登录)");    /**     * 该类自身的一个静态引用     */    private static FriendPanel panel;    /**     * 好友视图面板     */    private FriendViewPanel friendViewPanel;    /**     * 在线聊天面板     */    private OnlineChatPanel onlineChatPanel;    /*------------------------------------------------------------------------*     *                                构造函数                                *     *------------------------------------------------------------------------*/    /**     * 构造函数为私有,这样在整个运行过程中该类就只能有一个实例     */    private FriendPanel() {        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);        //分割条面板        friendViewPanel = new FriendViewPanel();        onlineChatPanel = new OnlineChatPanel();        JSplitPane spliter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false,            friendViewPanel, onlineChatPanel);        spliter.setOneTouchExpandable(true);        spliter.setDividerLocation(200);        constraints.gridy = 1;        constraints.weighty = 1.0;        constraints.fill = GridBagConstraints.BOTH;        constraints.insets = new Insets(5, 5, 5, 5);        add(spliter, constraints);    }    /*------------------------------------------------------------------------*     *                                公共方法                                *     *------------------------------------------------------------------------*/    /**     * 对该类提供的一个全局访问点,用来实例化该对象     *     * @return 该类唯一的一个实例     */    public static FriendPanel getInstance() {        if (panel == null) {            panel = new FriendPanel();        }        return panel;    }    /**     * 设置好友信息     *     * @param info 待设置的好友信息     */    public void setValue(UserInfo info) {        friendViewPanel.setValue(info);        onlineChatPanel.setValue(info);        ChatAgent.getInstance(info.getBasicInfo().getID()).start();    }    /**     * 返回用户的所有好友     *     * @return 用户的所有好友     */    public List getAllFriends() {        return friendViewPanel.getAllFriends();    }    /**     * 添加好友     */    public void addFriend() {        new SelectFriendDialog();        String id = SelectFriendDialog.getSelectedUserID();        if (id != null) {            friendViewPanel.addFriend(id);            ViewPanel.getInstance().update();            DownloadPanel.getInstance().addFriend(id);        }    }    /**     * 删除好友     */    public void deleteFriend() {        String id = friendViewPanel.getSelectedFriendID();        onlineChatPanel.deleteView(id);        friendViewPanel.deleteFriend();        ViewPanel.getInstance().update();        DownloadPanel.getInstance().deleteFriend(id);    }    /**     * 开始聊天     */    public void chatting() {        onlineChatPanel.chatWith(friendViewPanel.getSelectedFriendID());    }    /**     * 接收信息     *     * @param sid 消息发送者ID     * @param msg 待接收的消息     */    public void receive(String sid, String msg) {        ChatViewPanel panel = onlineChatPanel.getChatViewPanel(sid);        if (panel != null) {            panel.receive(msg);        } else {            StringBuffer content = new StringBuffer();            content.append("ID为");            content.append(sid);            content.append("的用户已将您加为好友,并发来如下消息:\n");            content.append(msg);            content.append("\n您必须也将其加为好友才能相互聊天。");            JOptionPane.showMessageDialog(Smart.getInstance(),                content.toString(), StringShop.HINT_TITLE,                JOptionPane.INFORMATION_MESSAGE);        }    }    /*------------------------------------------------------------------------*     *                                 内部类                                 *     *------------------------------------------------------------------------*/    /**     * 工具栏面板     */    class Toolbar extends JPanel {        /**         * Create a new instance of this class         */        public Toolbar() {            super(new FlowLayout(FlowLayout.CENTER, 5, 0));            //聊天            JButton button = new JButton(ChattingAction.getInstance());            button.setIcon(ImageShop.CHAT_IMAGEICON);            button.addMouseListener(new SBChanger(                ChattingAction.getInstance().getHintInfo(), false));            add(button);            //添加            button = new JButton(AddFriendAction.getInstance());            button.setIcon(ImageShop.ADD_IMAGEICON);            button.addMouseListener(new SBChanger(                AddFriendAction.getInstance().getHintInfo(), false));            add(button);            //删除            button = new JButton(DeleteFriendAction.getInstance());            button.setIcon(ImageShop.DELETE_IMAGEICON);            button.addMouseListener(new SBChanger(                DeleteFriendAction.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);            friendTree.addMouseListener(new MouseAdapter() {                public void mouseClicked(MouseEvent event) {                    int x = event.getX();                    int y = event.getY();                    TreePath path = friendTree.getPathForLocation(x, y);                    if (path != null && event.getClickCount() == 2) {                        if (path.getLastPathComponent() != FRIEND_ROOT) {                            ChattingAction.getInstance().actionPerformed(null);                        }                    }                }            });            setTree();            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);        }

⌨️ 快捷键说明

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