📄 chatviewpanel.java
字号:
/* * @(#) ChatViewPanel.java * Copyright 2004 HWStudio. All rights reserved. */package hws.item.smart.panel.function.chat.misc;//导入核心Java类库import java.io.IOException;import java.awt.Color;import java.awt.Cursor;import java.awt.Insets;import java.awt.Graphics;import java.awt.FontMetrics;import java.awt.GridBagLayout;import java.awt.GridBagConstraints;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseAdapter;import java.net.MalformedURLException;import java.util.List;import java.util.Vector;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JButton;import javax.swing.JTextArea;import javax.swing.JOptionPane;import javax.swing.JScrollPane;import javax.swing.JColorChooser;//导入自定义Java类库import hws.item.smart.Smart;import hws.item.smart.misc.ColorShop;import hws.item.smart.misc.ImageShop;import hws.item.smart.misc.SBChanger;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.dialog.SelectFontDialog;import hws.item.smart.dialog.ViewFriendDialog;import hws.item.smart.utility.chat.UserInfo;import hws.item.smart.utility.chat.FriendsInfo;//导入第三方Java类库import org.jdom.Element;import org.jdom.Document;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;import net.infonode.docking.View;import net.infonode.docking.RootWindow;import net.infonode.docking.SplitWindow;import net.infonode.docking.util.ViewMap;import net.infonode.docking.util.DockingUtil;import net.infonode.docking.theme.DockingWindowsTheme;import net.infonode.docking.theme.GradientDockingTheme;import net.infonode.docking.properties.TabWindowProperties;import net.infonode.docking.properties.RootWindowProperties;/** * 聊天场景面板 * * @version 0.1 2005-08-29 * @author Hwerz */public class ChatViewPanel extends JPanel { /*------------------------------------------------------------------------* * 属性定义 * *------------------------------------------------------------------------*/ /** * 聊天场景根面板 */ private RootWindow root; /** * 聊天记录文本域 */ private JTextArea logTextArea; /** * 发送消息文本域 */ private JTextArea msgTextArea; /** * 好友信息 */ private UserInfo user; /*------------------------------------------------------------------------* * 构造函数 * *------------------------------------------------------------------------*/ /** * Create a new instance of this class * * @param id 好友ID */ public ChatViewPanel(String id) { super(new GridBagLayout()); user = UserInfo.getRemoteUserInfo(id); //聊天记录文本域 logTextArea = new JTextArea(); logTextArea.setEditable(false); JScrollPane scroller1 = new JScrollPane(logTextArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); View view1 = new View("", null, scroller1); List list = view1.getCustomTabComponents(); list.add(new JLabel("与")); list.add(new LabelPlus(user.getBasicInfo().getNickname())); list.add(new JLabel("聊天")); //发送消息文本域 msgTextArea = new JTextArea(); JScrollPane scroller2 = new JScrollPane(msgTextArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); View view2 = new View("", null, scroller2); view2.getCustomTabComponents().add(new Toolbar()); //分割条面板 SplitWindow spliter = new SplitWindow(false, 0.45f, view1, view2); //聊天场景根面板 root = createRootWindow(); root.setWindow(spliter); setRootWindow(root); GridBagConstraints constraints = new GridBagConstraints( //gridx, gridy 0, 0, //gridwidth, gridheight 1, 1, //weightx, weighty 1.0, 1.0, //anchor GridBagConstraints.NORTH, //fill GridBagConstraints.BOTH, //insets new Insets(0, 0, 0, 0), //ipadx, ipady 0, 0); add(root, constraints); } /*------------------------------------------------------------------------* * 公共方法 * *------------------------------------------------------------------------*/ /** * 发送消息 */ public void send() { String msg = msgTextArea.getText().trim(); if (msg.length() == 0) { JOptionPane.showMessageDialog(Smart.getInstance(), "要发送的消息不可为空!", StringShop.HINT_TITLE, JOptionPane.INFORMATION_MESSAGE); } else { try { XmlRpc.setDriver("org.apache.xerces.parsers.SAXParser"); XmlRpcClient client = new XmlRpcClient(genURL()); client.execute(genRequest(msg)); log(ViewPanel.getInstance().getUserNickname(), msg); msgTextArea.setText(""); } 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); } } } /** * 接收消息 * * @param msg 待接收的消息 */ public void receive(String msg) { log(user.getBasicInfo().getNickname(), msg); } /*------------------------------------------------------------------------* * 私有方法 * *------------------------------------------------------------------------*/ /** * 生成请求URL * * @return 生成的请求URL */ private String genURL() { StringBuffer url = new StringBuffer(); url.append("http://"); String friendID = user.getBasicInfo().getID(); url.append(FriendsInfo.getFriendIP(friendID)); url.append(":"); int delta = Integer.parseInt(friendID); url.append(XMLConfig.getChatPort() + delta); url.append("/"); return url.toString(); } /** * 生成XML-RPC请求消息 * * @param msg 待发送的消息 * @return 生成的XML-RPC请求消息 */ private XmlRpcRequest genRequest(String msg) { //请求方法 StringBuffer method = new StringBuffer(); method.append(XMLConfig.getChatClass()); method.append("."); method.append(XMLConfig.getChatMethod()); //请求参数 Vector params = new Vector(); XMLOutputter outputter = new XMLOutputter(); params.addElement(outputter.outputString(genDocument(msg))); return new XmlRpcRequest(method.toString(), params); } /** * 生成XML文档 * * @param msg 待发送的消息 * @return 生成的XML文档 */ private Document genDocument(String msg) { Element request = new Element("Chat"); Element level1 = new Element("Sender"); level1.setAttribute("id", ViewPanel.getInstance().getUserID()); level1.setAttribute("msg", msg); request.addContent(level1); return new Document(request); } /** * 创建聊天场景根面板 * * @return 创建的聊天场景根面板 */ private RootWindow createRootWindow() { View view = new View(null, null, null); ViewMap viewMap = new ViewMap(); viewMap.addView(0, view); RootWindow root = DockingUtil.createRootWindow(viewMap, false); view.close(); viewMap.removeView(0); return root; } /** * 设置聊天场景根面板 * * @param root 待设置的聊天场景根面板 */ private void setRootWindow(RootWindow root) { DockingWindowsTheme theme = new GradientDockingTheme(); RootWindowProperties rwp = new RootWindowProperties(); rwp.addSuperObject(theme.getRootWindowProperties()); TabWindowProperties twp = rwp.getTabWindowProperties(); twp.getRestoreButtonProperties().setToolTipText("还原"); twp.getMaximizeButtonProperties().setToolTipText("最大化"); twp.getTabProperties().getTitledTabProperties() .getNormalProperties().setToolTipText(null); root.getRootWindowProperties().addSuperObject(rwp); } /** * 日志 * * @param nickname 昵称 * @param msg 消息 */ private void log(String nickname, String msg) { logTextArea.append(PopToolkit.getLongTime()); logTextArea.append(" "); logTextArea.append(nickname); logTextArea.append("\n"); logTextArea.append(msg); logTextArea.append("\n"); } /*------------------------------------------------------------------------* * 内部类 * *------------------------------------------------------------------------*/ /** * 工具栏面板 */ class Toolbar extends JPanel implements ActionListener { /** * Create a new instance of this class */ public Toolbar() { super(new GridBagLayout()); setOpaque(false); //颜色 JButton button = new JButton("颜色", ImageShop.COLOR_IMAGEICON); button.setFocusable(false); button.addActionListener(this); button.addMouseListener(new SBChanger("改变字体颜色", false)); GridBagConstraints constraints = new GridBagConstraints( //gridx, gridy 0, 0, //gridwidth, gridheight 1, 1, //weightx, weighty 0.0, 0.0, //anchor GridBagConstraints.WEST, //fill GridBagConstraints.NONE, //insets new Insets(4, 0, 0, 0), //ipadx, ipady 0, 0); add(button, constraints); //字体 button = new JButton("字体", ImageShop.FONT_IMAGEICON); button.setFocusable(false); button.addActionListener(this); button.addMouseListener(new SBChanger("改变字体", false)); constraints.gridx = 1; constraints.insets = new Insets(4, 4, 0, 0); add(button, constraints); //日期 button = new JButton("日期", ImageShop.DATE_IMAGEICON); button.setFocusable(false); button.addActionListener(this); button.addMouseListener(new SBChanger("插入日期", false)); constraints.insets = new Insets(4, 4, 0, 0); constraints.gridx = 2; add(button, constraints); //发送 button = new JButton("发送", ImageShop.SEND_IMAGEICON); button.setFocusable(false); button.addActionListener(this); button.addMouseListener(new SBChanger("发送消息", false)); constraints.insets = new Insets(4, 4, 0, 7); constraints.gridx = 3; add(button, constraints); } /** * 实现接口ActionListener的方法 * * @param event the event that characterizes the action */ public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if (command.equals("颜色") == true) { Color color = JColorChooser.showDialog(Smart.getInstance(), "选择字体颜色", msgTextArea.getForeground()); msgTextArea.setForeground(color); } else if (command.equals("字体") == true) { new SelectFontDialog(msgTextArea.getFont()); msgTextArea.setFont(SelectFontDialog.getSelectedFont()); } else if (command.equals("日期") == true) { msgTextArea.append(PopToolkit.getCurrentDate()); } else { send(); } } } /** * 自定义标签 */ class LabelPlus extends JLabel { /** * 标记鼠标是否进入到标签的边界内 */ private boolean mouseEntered; /** * Create a new instance of this class * * @param text 标签上显示的文本 */ public LabelPlus(String text) { super(text); mouseEntered = false; addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent event) { mouseEntered = true; setForeground(ColorShop.HYPERLINK1_COLOR); Smart.getInstance().setCursor( Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); } public void mouseExited(MouseEvent event) { mouseEntered = false; setForeground(ColorShop.HYPERLINK2_COLOR); Smart.getInstance().setCursor(Cursor.getDefaultCursor()); } public void mouseClicked(MouseEvent event) { new ViewFriendDialog(user); } }); } /** * 覆盖超类JLabel的方法 * * @param g the specified Graphics context */ public void paint(Graphics g) { super.paint(g); if (mouseEntered == true) { FontMetrics metrics = g.getFontMetrics(); int width = metrics.stringWidth(getText()) - 3; int height = metrics.getHeight() - 1; g.drawLine(0, height, width, height); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -