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

📄 receivesystemmessageshell.java

📁 lumaQQ的源文件
💻 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 static edu.tsinghua.lumaqq.resource.Messages.*;

import edu.tsinghua.lumaqq.models.Cluster;
import edu.tsinghua.lumaqq.models.ModelRegistry;
import edu.tsinghua.lumaqq.models.User;
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.InPacket;
import edu.tsinghua.lumaqq.qq.packets.in.AddFriendExReplyPacket;
import edu.tsinghua.lumaqq.qq.packets.in.AuthorizeReplyPacket;
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.AddFriendAuthResponsePacket;
import edu.tsinghua.lumaqq.qq.packets.out.ClusterCommandPacket;
import edu.tsinghua.lumaqq.record.IKeyConstants;
import edu.tsinghua.lumaqq.record.RecordEntry;
import edu.tsinghua.lumaqq.resource.Resources;
import edu.tsinghua.lumaqq.ui.config.user.UserInfoWindow;
import edu.tsinghua.lumaqq.widgets.qstyle.Slat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.osgi.util.NLS;
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.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
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;

/**
 * 查看系统消息窗口,这个窗口不光要查看系统的消息,还要负责查看群的通知消息,但是其
 * 基本结构还是差不多,都要是验证,拒绝之类的,不过由于消息的类型可能不同,一些控制
 * 逻辑是不可避免的,也许看起来有点晕。
 * 
 * @author luma
 */
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 Slat btnApprove, btnReject, btnAdd;
	private Slat btnUserInfo;
	private Resources res = Resources.getInstance();
	// 头像id
	private int headId;
	// 查看的号码和发送认证消息的目的号码
	private int infoQQ, authQQ;
	// qqNum是用户还是群的标志
	private boolean isCluster;
	// 发送用户验证还是群验证
	private boolean isClusterAuth;
	// 系统消息类型
	private byte type;
	// 是否在拒绝请求的第二步
	private boolean rejectSecondStep;
	// 是否在添加好友的第二步
	private boolean addSecondStep;
	// 动画帧
	private Image[] frames;
	
	public ReceiveSystemMessageShell(MainShell main) {
		this.main = main;
		this.display = main.getDisplay();
		shell = new Shell(display, SWT.TITLE | SWT.CLOSE | SWT.MIN);
		shell.setText(receive_system_message_title);
		shell.setImage(res.getImage(Resources.icoLumaQQ));
		// 添加事件监听器
		shell.addShellListener(this);
		
		// 初始化变量
		rejectSecondStep = false;
		addSecondStep = false;
		isCluster = false;
		isClusterAuth = false;
		initAnimateFrames(0);
		
		initLayout();
	}
	
	// 初始化其他控件
	private void initLayout() {
		shell.setLayout(new GridLayout());
		
		Group from = new Group(shell, SWT.SHADOW_ETCHED_IN);
		from.setText(receive_system_message_from);
		from.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		GridLayout layout = new GridLayout(6, false);
		layout.marginHeight = 0;
		layout.marginWidth = 3;
		from.setLayout(layout);
		// qq号标签
		Label lblQQ = new Label(from, SWT.NONE);
		lblQQ.setText(receive_system_message_from_qq);
		lblQQ.setLayoutData(new GridData());
		// qq号文本框
		textQQ = new Text(from, SWT.SINGLE | SWT.READ_ONLY | SWT.BORDER);
		textQQ.setBackground(from.getBackground());
		GridData gd = new GridData();
		gd.widthHint = 80;
		textQQ.setLayoutData(gd);
		// 昵称标签
		Label lblNick = new Label(from, SWT.NONE);
		lblNick.setText(receive_system_message_from_nick);
		lblNick.setLayoutData(new GridData());
		// 昵称文本框
		textNick = new Text(from, SWT.SINGLE | SWT.READ_ONLY | SWT.BORDER);
		textNick.setBackground(from.getBackground());
		gd = new GridData();
		gd.widthHint = 80;
		textNick.setLayoutData(gd);
		// 查看详细资料标签
		Label lblSeeDetail = new Label(from, SWT.NONE);
		lblSeeDetail.setText(receive_system_message_from_seedetail);
		lblSeeDetail.setLayoutData(new GridData());
		// 查看用户资料按钮
		btnUserInfo = new Slat(from, SWT.CENTER | SWT.FLAT, null, res.getHead(0));
		gd = new GridData();
		gd.widthHint = gd.heightHint = 48;
		btnUserInfo.setLayoutData(gd);
		btnUserInfo.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				if(isCluster) {
					Cluster c = ModelRegistry.getCluster(infoQQ);
					if(c == null) {
						c = new Cluster();
						c.clusterId = infoQQ;						
					}
					main.getShellLauncher().openClusterInfoWindow(c);
					main.getClient().getClusterInfo(infoQQ);
				} else {
					User f = new User();
					f.qq = infoQQ;
					main.getShellLauncher().openUserInfoWindow(f, UserInfoWindow.READ_ONLY);
					main.getClient().getUserInfo(infoQQ);						
				}
			}
		});
		// 消息内容标签或者拒绝理由提示标签
		lblHint = new Label(shell, SWT.NONE);
		lblHint.setText(receive_system_message_content_label);
		lblHint.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		// 消息内容文本框
		textMsg = new Text(shell, SWT.MULTI | SWT.READ_ONLY | SWT.BORDER | SWT.WRAP);
		textMsg.setBackground(shell.getBackground());
		textMsg.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		// 按钮容器
		Composite buttonComposite = new Composite(shell, SWT.NONE);
		layout = new GridLayout(4, false);
		layout.marginHeight = layout.marginWidth = 0;
		layout.horizontalSpacing = 3;
		buttonComposite.setLayout(layout);
		buttonComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		// 通过验证按钮
		btnApprove = new Slat(buttonComposite, SWT.NONE);
		btnApprove.setText(receive_system_message_button_approve);
		gd = new GridData();
		gd.widthHint = 80;		
		btnApprove.setLayoutData(gd);
		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(isClusterAuth)
						main.getClient().approveJoinCluster(authQQ, infoQQ);
					else
						main.getClient().approveAddMe(authQQ);
				}
			}
		);
		// 拒绝请求按钮
		btnReject = new Slat(buttonComposite, SWT.NONE);
		btnReject.setText(receive_system_message_button_reject);
		gd = new GridData();
		gd.widthHint = 80;		
		btnReject.setLayoutData(gd);
		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(isClusterAuth)
							main.getClient().rejectJoinCluster(authQQ, infoQQ, textMsg.getText());
						else
							main.getClient().rejectAddMe(authQQ, textMsg.getText());
					} else {
						/* 拒绝的第一步是提示用户输入拒绝理由 */
						textMsg.setEditable(true);
						textMsg.setBackground(main.getDisplay().getSystemColor(SWT.COLOR_WHITE));
						textMsg.setText("");
						textMsg.setFocus();
						lblHint.setText(receive_system_message_reject_label);						
						rejectSecondStep = true;
						addSecondStep = false;
					}
				}
			}
		);
		// 加为好友按钮
		btnAdd = new Slat(buttonComposite, SWT.NONE);
		btnAdd.setText(receive_system_message_button_add);
		gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
		gd.grabExcessHorizontalSpace = true;
		gd.widthHint = 80;		
		btnAdd.setLayoutData(gd);
		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(authQQ, textMsg.getText());
					} else {
						/* 第一步是直接发送添加请求,如果对方不需要验证,则直接成功 */
						textMsg.setEditable(false);
						textMsg.setBackground(shell.getBackground());
						textMsg.setText(NLS.bind(receive_system_message_adding, String.valueOf(authQQ)));
						btnApprove.setEnabled(false);
						btnReject.setEnabled(false);						
						btnAdd.setEnabled(false);
						main.getClient().addFriend(authQQ);						
					}
					btnUserInfo.startAnimate(frames);
				}
			}
		);
		// 关闭按钮
		Slat btnClose = new Slat(buttonComposite, SWT.NONE);
		btnClose.setText(button_close);
		gd = new GridData();
		gd.widthHint = 80;		
		btnClose.setLayoutData(gd);
		btnClose.addMouseListener(
			new MouseAdapter() {
				public void mouseUp(MouseEvent e) {
					close();
				}
			}
		);
	}
	
	/**
	 * 设置当前窗口运行在删除好友模式
	 */
	private void setDeleteMode() {
		btnAdd.setVisible(false);
		btnApprove.setVisible(false);
		btnReject.setVisible(false);
		textMsg.setText(receive_system_message_deleting);
		btnUserInfo.startAnimate(frames);
	}
	
	/**
	 * 设置当前窗口运行在添加好友模式
	 */
	private void setAddMode() {
		btnAdd.setVisible(false);
		btnApprove.setVisible(false);
		btnReject.setVisible(false);
		textMsg.setText(NLS.bind(receive_system_message_adding, String.valueOf(authQQ)));
		btnUserInfo.startAnimate(frames);
	}
	
	/**
	 * 初始化动画帧
	 * @param head
	 */
	private void initAnimateFrames(int head) {
		frames = new Image[] { res.getHead(head), res.getHead(head + 2) };
	}
	
	/**
	 * 设置头像号
	 * @param headId
	 */
	public void setHeadId(int h) {
		headId = h - h % 3;
		btnUserInfo.setImage(res.getHead(headId));
		initAnimateFrames(headId);
	}
	
	/**
	 * 设置好友的model
	 * @param f
	 */
	public void setFriendModel(User f) {
		setQQ(f.qq, f.qq, String.valueOf(f.qq));
		setHeadId(f.headId);
		setNick(f.displayName);
	}
	
	/**
	 * 缺省打开为查看消息模式
	 */
	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.pack();
		Point size = shell.getSize();
		size.x = Math.max(400, size.x);
		size.y = Math.max(250, size.y);
		shell.setSize(size);
		shell.open();
		main.getClient().addQQListener(this);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.ShellListener#shellClosed(org.eclipse.swt.events.ShellEvent)
	 */
	public void shellClosed(ShellEvent e) {
		main.getClient().removeQQListener(this);
	}
	
	/**
	 * 是当前窗口激活
	 */
	public void setActive() {
		shell.setActive();
	}	
	
	/**
	 * 代理方法,设置窗口的最小化状态
	 * @param b
	 */
	public void setMinimized(boolean b) {
		shell.setMinimized(b);
	}
	
	/**
	 * 设置要显示的系统消息
	 * @param in
	 */
	public void setSystemMessage(InPacket in) {
		if(in instanceof SystemNotificationPacket)
			setSystemMessage((SystemNotificationPacket)in);
		else if(in instanceof ReceiveIMPacket)
			setSystemMessage((ReceiveIMPacket)in);
	}
	
	private void setSystemMessage(ReceiveIMPacket packet) {
		switch(packet.header.type) {
			case QQ.QQ_RECV_IM_SYS_MESSAGE:
				setSystemMessage(10000, 
						0, 
						packet.sysMessage,
						false,
						false,
						false,
						false,
						false);
				break;
			case QQ.QQ_RECV_IM_ADDED_TO_CLUSTER:
			case QQ.QQ_RECV_IM_CREATE_CLUSTER:
			case QQ.QQ_RECV_IM_APPROVE_JOIN_CLUSTER:
			case QQ.QQ_RECV_IM_REJECT_JOIN_CLUSTER:
			case QQ.QQ_RECV_IM_CLUSTER_NOTIFICATION:
				setSystemMessage(packet.header.sender,
						0,
						packet.message,
						true,
						false,
						false,
						false,
						false);
				break;
			case QQ.QQ_RECV_IM_DELETED_FROM_CLUSTER:
				if(packet.sender == main.getMyModel().qq) 
					setSystemMessage(packet.header.sender,
							0,
							packet.message,
							true,
							false,
							false,
							false,
							false);
				else
					setSystemMessage(packet.sender,
							0,
							packet.message,
							false,
							false,
							false,
							false,
							false);
				break;
			case QQ.QQ_RECV_IM_REQUEST_JOIN_CLUSTER:
				setSystemMessage(packet.sender,
						packet.header.sender,
						packet.message,
						false,
						true,
						true,
						true,
						false);
				break;
		}
	}
	
	private void setSystemMessage(SystemNotificationPacket packet) {
		switch(packet.type) {
			case QQ.QQ_SYS_BEING_ADDED:
			case QQ.QQ_SYS_BEING_ADDED_EX:
				setSystemMessage(packet.from, 
						packet.from,
						packet.message,
						false,
						false,
						false,
						false,
						true);
				break;

⌨️ 快捷键说明

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