?? friendpanel.java
字號(hào):
/* * @(#) FriendPanel.java * Copyright 2004 HWStudio. All rights reserved. */package hws.item.smart.panel.function.chat;//導(dǎo)入核心Java類庫(kù)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;//導(dǎo)入自定義Java類庫(kù)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;//導(dǎo)入第三方Java類庫(kù)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 { /*------------------------------------------------------------------------* * 屬性定義 * *------------------------------------------------------------------------*/ /** * 好友樹根節(jié)點(diǎn) */ private static final DefaultMutableTreeNode FRIEND_ROOT = new DefaultMutableTreeNode("我的好友(未登錄)"); /** * 該類自身的一個(gè)靜態(tài)引用 */ private static FriendPanel panel; /** * 好友視圖面板 */ private FriendViewPanel friendViewPanel; /** * 在線聊天面板 */ private OnlineChatPanel onlineChatPanel; /*------------------------------------------------------------------------* * 構(gòu)造函數(shù) * *------------------------------------------------------------------------*/ /** * 構(gòu)造函數(shù)為私有,這樣在整個(gè)運(yùn)行過(guò)程中該類就只能有一個(gè)實(shí)例 */ 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); } /*------------------------------------------------------------------------* * 公共方法 * *------------------------------------------------------------------------*/ /** * 對(duì)該類提供的一個(gè)全局訪問(wèn)點(diǎn),用來(lái)實(shí)例化該對(duì)象 * * @return 該類唯一的一個(gè)實(shí)例 */ public static FriendPanel getInstance() { if (panel == null) { panel = new FriendPanel(); } return panel; } /** * 設(shè)置好友信息 * * @param info 待設(shè)置的好友信息 */ 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 消息發(fā)送者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("的用戶已將您加為好友,并發(fā)來(lái)如下消息:\n"); content.append(msg); content.append("\n您必須也將其加為好友才能相互聊天。"); JOptionPane.showMessageDialog(Smart.getInstance(), content.toString(), StringShop.HINT_TITLE, JOptionPane.INFORMATION_MESSAGE); } } /*------------------------------------------------------------------------* * 內(nèi)部類 * *------------------------------------------------------------------------*/ /** * 工具欄面板 */ 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 { /** * 好友視圖標(biāo)簽 */ private JLabel label; /** * 我的好友樹的視圖 */ private JTree friendTree; /** * 我的好友樹的模型 */ private DefaultTreeModel friendModel; /** * Create a new instance of this class */ public FriendViewPanel() { super(new GridBagLayout()); //好友視圖標(biāo)簽 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); }
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -