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

📄 receivesystemmessageshell.java

📁 java写的qq代码实现qq的部分功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.tsinghua.lumaqq.ui;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.tsinghua.lumaqq.IconHolder;
import edu.tsinghua.lumaqq.LumaQQ;
import edu.tsinghua.lumaqq.models.ClusterModel;
import edu.tsinghua.lumaqq.models.FriendModel;
import edu.tsinghua.lumaqq.models.IQQNode;
import edu.tsinghua.lumaqq.qq.QQ;
import edu.tsinghua.lumaqq.qq.events.IQQListener;
import edu.tsinghua.lumaqq.qq.events.QQEvent;
import edu.tsinghua.lumaqq.qq.packets.BasicInPacket;
import edu.tsinghua.lumaqq.qq.packets.in.ClusterCommandReplyPacket;
import edu.tsinghua.lumaqq.qq.packets.in.ReceiveIMPacket;
import edu.tsinghua.lumaqq.qq.packets.in.SystemNotificationPacket;
import edu.tsinghua.lumaqq.qq.packets.out.AddFriendAuthPacket;
import edu.tsinghua.lumaqq.qq.packets.out.AddFriendPacket;
import edu.tsinghua.lumaqq.ui.config.user.UserInfoWindow;
import edu.tsinghua.swt.models.INode;
import edu.tsinghua.swt.widgets.CoolButton;
import edu.tsinghua.swt.widgets.MySWT;

/**
 * 查看系统消息窗口,这个窗口不光要查看系统的消息,还要负责查看群的通知消息,但是其
 * 基本结构还是差不多,都要是验证,拒绝之类的,不过由于消息的类型可能不同,一些控制
 * 逻辑是不可避免的,也许看起来有点晕。
 * 
 * @author 马若劼
 */
public class ReceiveSystemMessageShell extends ShellAdapter implements IQQListener {
	// Log对象
    protected static Log log = LogFactory.getLog(ReceiveSystemMessageShell.class);
    // 窗口的打开模式,有查看模式,删除模式和添加模式
    public static final int VIEW_MODE = 0;
    public static final int DELETE_MODE = 1;
    public static final int ADD_MODE = 2;
    
    private MainShell main;
	private Shell shell;
	private Display display;
	private Label lblHint;
	private Text textQQ, textNick, textMsg;
	private CoolButton btnUserInfo, btnApprove, btnReject, btnAdd, btnAddStranger;
	private IconHolder icons = IconHolder.getInstance();
	// 系统消息的来源QQ号和头像号,如果是群消息,则实际为群的内部ID
	private int qq, faceId;
	// qqNum是用户还是群的标志
	private boolean isCluster;
	// 系统消息类型
	private byte type;
	// 是否在拒绝请求的第二步
	private boolean rejectSecondStep;
	// 是否在添加好友的第二步
	private boolean addSecondStep;
	// 动画帧
	private Image[] frames;
	// 用户的model
	private FriendModel f;
	// 消息的发送者
    private int sender;
	
	public ReceiveSystemMessageShell(MainShell main) {
		this.main = main;
		this.display = main.getDisplay();
		shell = new Shell(display, SWT.TITLE | SWT.CLOSE | SWT.MIN);
		shell.setSize(380, 250);
		shell.setText(LumaQQ.getString("receive.system.message.title"));
		shell.setImage(icons.getImage(IconHolder.icoQQ));
		// 设置layout
		shell.setLayout(new FormLayout());
		// 添加事件监听器
		shell.addShellListener(this);
		
		// 初始化变量
		rejectSecondStep = false;
		addSecondStep = false;
		isCluster = false;
		initAnimateFrames(0);
		
		initLayout();
	}
	
	// 初始化其他控件
	private void initLayout() {
		Group from = new Group(shell, SWT.SHADOW_ETCHED_IN);
		from.setText(LumaQQ.getString("receive.system.message.from"));
		FormData fd = new FormData();
		fd.left = fd.top =  new FormAttachment(0, 5);
		fd.right = new FormAttachment(100, -5);
		fd.bottom = new FormAttachment(0, 55);
		from.setLayoutData(fd);
		from.setLayout(new FormLayout());
		// qq号标签
		Label lblQQ = new Label(from, SWT.NONE);
		lblQQ.setText(LumaQQ.getString("receive.system.message.from.qq"));
		fd = new FormData();
		fd.left = fd.top =  new FormAttachment(0, 5);
		lblQQ.setLayoutData(fd);
		// qq号文本框
		textQQ = new Text(from, SWT.SINGLE | SWT.READ_ONLY | SWT.BORDER);
		textQQ.setBackground(from.getBackground());
		fd = new FormData();
		fd.left = new FormAttachment(lblQQ, 5, SWT.RIGHT);
		fd.right = new FormAttachment(lblQQ, 85, SWT.RIGHT);
		fd.top = new FormAttachment(0, 5);
		textQQ.setLayoutData(fd);
		// 昵称标签
		Label lblNick = new Label(from, SWT.NONE);
		lblNick.setText(LumaQQ.getString("receive.system.message.from.nick"));
		fd = new FormData();
		fd.left = new FormAttachment(textQQ, 5, SWT.RIGHT); 
		fd.top =  new FormAttachment(0, 5);
		lblNick.setLayoutData(fd);
		// 昵称文本框
		textNick = new Text(from, SWT.SINGLE | SWT.READ_ONLY | SWT.BORDER);
		textNick.setBackground(from.getBackground());
		fd = new FormData();
		fd.left = new FormAttachment(lblNick, 5, SWT.RIGHT);
		fd.right = new FormAttachment(lblNick, 85, SWT.RIGHT);
		fd.top = new FormAttachment(0, 5);
		textNick.setLayoutData(fd);
		// 查看用户资料按钮
		btnUserInfo = new CoolButton(from, MySWT.HOVER | MySWT.FLAT | SWT.CENTER, null, icons.getHead(0));
		btnUserInfo.setShowSmallImage(false);
		btnUserInfo.setDrawEmptyString(false);
		fd = new FormData();
		fd.right = new FormAttachment(100, -5);
		fd.top = new FormAttachment(0, 0);
		btnUserInfo.setLayoutData(fd);
		btnUserInfo.addMouseListener(
			new MouseAdapter() {
				public void mouseUp(MouseEvent e) {
					if(isCluster) {
						ClusterModel c = new ClusterModel(qq);
						main.getShellLauncher().openClusterInfoWindow(c);
						main.getClient().getClusterInfo(qq);
					} else {
						FriendModel f = new FriendModel(String.valueOf(qq));
						f.addProperty(IQQNode.HEAD, new Integer(0));
						main.getShellLauncher().openUserInfoWindow(f, UserInfoWindow.READ_ONLY);
						main.getClient().getUserInfo(qq);						
					}
				}
			}
		);
		// 查看详细资料标签
		Label lblSeeDetail = new Label(from, SWT.NONE);
		lblSeeDetail.setText(LumaQQ.getString("receive.system.message.from.seedetail"));
		fd = new FormData();
		fd.right = new FormAttachment(btnUserInfo, -1, SWT.LEFT); 
		fd.top =  new FormAttachment(0, 5);
		lblSeeDetail.setLayoutData(fd);
		// 消息内容标签或者拒绝理由提示标签
		lblHint = new Label(shell, SWT.NONE);
		lblHint.setText(LumaQQ.getString("receive.system.message.content.label"));
		fd = new FormData();
		fd.left = new FormAttachment(0, 5); 
		fd.right = new FormAttachment(100, -5);
		fd.top =  new FormAttachment(from, 1, SWT.BOTTOM);
		lblHint.setLayoutData(fd);
		// 消息内容文本框
		textMsg = new Text(shell, SWT.MULTI | SWT.READ_ONLY | SWT.BORDER | SWT.WRAP);
		textMsg.setBackground(shell.getBackground());
		fd = new FormData();
		fd.left = new FormAttachment(0, 5);
		fd.right = new FormAttachment(100, -5);
		fd.top = new FormAttachment(lblHint, 1, SWT.RIGHT);
		fd.bottom = new FormAttachment(100, -30);
		textMsg.setLayoutData(fd);
		// 通过验证按钮
		btnApprove = new CoolButton(shell, MySWT.HOVER | SWT.CENTER, LumaQQ.getString("receive.system.message.button.approve"), null);
		fd = new FormData();
		fd.left = new FormAttachment(0, 5);
		fd.right = new FormAttachment(20, 5);
		fd.top = new FormAttachment(textMsg, 5, SWT.BOTTOM);
		btnApprove.setLayoutData(fd);
		btnApprove.addMouseListener(
			new MouseAdapter() {
				public void mouseUp(MouseEvent e) {
					textMsg.setEditable(false);
					textMsg.setBackground(shell.getBackground());
					btnApprove.setEnabled(false);
					btnReject.setEnabled(false);
					btnAdd.setEnabled(false);
					btnUserInfo.startAnimate(frames);
					if(isCluster)
						main.getClient().approveJoinCluster(qq, sender);
					else
						main.getClient().approveAddMe(qq);
				}
			}
		);
		// 拒绝请求按钮
		btnReject = new CoolButton(shell, MySWT.HOVER | SWT.CENTER, LumaQQ.getString("receive.system.message.button.reject"), null);
		fd = new FormData();
		fd.left = new FormAttachment(20, 5);
		fd.right = new FormAttachment(40, 5);
		fd.top = new FormAttachment(textMsg, 5, SWT.BOTTOM);
		btnReject.setLayoutData(fd);
		btnReject.addMouseListener(
			new MouseAdapter() {
				public void mouseUp(MouseEvent e) {
					if(rejectSecondStep) {
						/* 拒绝的第二步是发送拒绝的信息 */
						textMsg.setEditable(false);
						textMsg.setBackground(shell.getBackground());
						btnApprove.setEnabled(false);
						btnReject.setEnabled(false);
						btnAdd.setEnabled(false);
						btnUserInfo.startAnimate(frames);
						if(isCluster)
							main.getClient().rejectJoinCluster(qq, sender, textMsg.getText());
						else
							main.getClient().rejectAddMe(qq, textMsg.getText());
					} else {
						/* 拒绝的第一步是提示用户输入拒绝理由 */
						textMsg.setEditable(true);
						textMsg.setBackground(main.getDisplay().getSystemColor(SWT.COLOR_WHITE));
						textMsg.setText("");
						textMsg.setFocus();
						lblHint.setText(LumaQQ.getString("receive.system.message.reject.label"));						
						rejectSecondStep = true;
						addSecondStep = false;
					}
				}
			}
		);
		// 关闭按钮
		CoolButton btnClose = new CoolButton(shell, MySWT.HOVER | SWT.CENTER, LumaQQ.getString("receive.system.message.button.close"), null);
		fd = new FormData();
		fd.right = new FormAttachment(100, -5);
		fd.left = new FormAttachment(80, -5);
		fd.top = new FormAttachment(textMsg, 5, SWT.BOTTOM);
		btnClose.setLayoutData(fd);
		btnClose.addMouseListener(
			new MouseAdapter() {
				public void mouseUp(MouseEvent e) {
					close();
				}
			}
		);
		// 加为好友按钮
		btnAdd = new CoolButton(shell, MySWT.HOVER | SWT.CENTER, LumaQQ.getString("receive.system.message.button.add"), null);
		fd = new FormData();
		fd.right = new FormAttachment(80, -5);
		fd.left = new FormAttachment(60, -5);
		fd.top = new FormAttachment(textMsg, 5, SWT.BOTTOM);
		btnAdd.setLayoutData(fd);
		btnAdd.addMouseListener(
			new MouseAdapter() {
				public void mouseUp(MouseEvent e) {
					if(addSecondStep) {
						/* 第二步表示发送验证信息,这一步只会在对方需要验证时出现 */
						btnApprove.setEnabled(false);
						btnReject.setEnabled(false);
						btnAdd.setEnabled(false);
						textMsg.setEditable(false);
						textMsg.setBackground(shell.getBackground());
						main.getClient().sendAddFriendAuth(qq, textMsg.getText());
					} else {
						/* 第一步是直接发送添加请求,如果对方不需要验证,则直接成功 */
						textMsg.setEditable(false);
						textMsg.setBackground(shell.getBackground());
						textMsg.setText(LumaQQ.getString("receive.system.message.adding", new Object[] { String.valueOf(qq) }));
						btnApprove.setEnabled(false);
						btnReject.setEnabled(false);						
						btnAdd.setEnabled(false);
						main.getClient().addFriend(qq);						
					}
					btnUserInfo.startAnimate(frames);
				}
			}
		);
		btnAddStranger = new CoolButton(shell, MySWT.HOVER | SWT.CENTER, LumaQQ.getString("receive.system.message.button.addstranger"), null);
		fd = new FormData();
		fd.right = new FormAttachment(80, -5);
		fd.left = new FormAttachment(55, -5);
		fd.top = new FormAttachment(textMsg, 5, SWT.BOTTOM);
		btnAddStranger.setLayoutData(fd);
		btnAddStranger.addMouseListener(
			new MouseAdapter() {
				public void mouseUp(MouseEvent e) {
					// 找到陌生人组的索引
					int[] stranger = main.getMVCHelper().listStrangerGroup();
					if(stranger != null && stranger.length > 0)
						main.getMVCHelper().addFriendViewPart(qq, stranger[0]);
					btnAddStranger.setVisible(false);
				}
			}
		);
	}
	
	/**
	 * 设置当前窗口运行在删除好友模式
	 */
	private void setDeleteMode() {
		btnAdd.setVisible(false);
		btnApprove.setVisible(false);
		btnReject.setVisible(false);
		btnAddStranger.setVisible(false);
		textMsg.setText(LumaQQ.getString("receive.system.message.deleting"));
		btnUserInfo.startAnimate(frames);
	}
	
	/**
	 * 设置当前窗口运行在添加好友模式
	 */
	private void setAddMode() {
		btnAdd.setVisible(false);
		btnApprove.setVisible(false);
		btnReject.setVisible(false);
		btnAddStranger.setVisible(false);
		textMsg.setText(LumaQQ.getString("receive.system.message.adding", new Object[] { String.valueOf(qq) }));
		btnUserInfo.startAnimate(frames);
	}
	
	/**
	 * 初始化动画帧
	 * @param faceId
	 */
	private void initAnimateFrames(int faceId) {
		frames = new Image[] { icons.getHead(faceId), icons.getHead(faceId + 2) };
	}
	
	/**
	 * 设置头像号
	 * @param faceId
	 */
	public void setFaceId(int f) {
		faceId = f - f % 3;
		btnUserInfo.setImage(icons.getHead(faceId));
		initAnimateFrames(faceId);
	}
	
	/**
	 * 设置好友的model
	 * @param f
	 */
	public void setFriendModel(FriendModel f) {
		this.f = f;
		setQQ(f.getQQ());
		setFaceId(f.getHeadId());
		setNick((String)f.getProperty(INode.NAME));
	}
	
	/**
	 * 缺省打开为查看消息模式
	 */
	public void open() {
		open(VIEW_MODE);
	}

	// 打开shell
	public void open(int mode)	{
		// 设置打开模式
		if(mode == DELETE_MODE)
			setDeleteMode();
		else if(mode == ADD_MODE)
			setAddMode();
		
		// 打开shell
		shell.layout();
		shell.open();
		main.getClient().addQQListener(this);
	}

	/**
	 * @return Returns the qq.
	 */
	public int getQQ() {

⌨️ 快捷键说明

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