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

📄 mvchelper.java

📁 java写的qq代码实现qq的部分功能
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
* 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.helper;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.tsinghua.lumaqq.IconHolder;
import edu.tsinghua.lumaqq.LumaQQ;
import edu.tsinghua.lumaqq.MessageQueue;
import edu.tsinghua.lumaqq.ModelUtils;
import edu.tsinghua.lumaqq.events.GroupMouseListener;
import edu.tsinghua.lumaqq.events.ItemMouseListener;
import edu.tsinghua.lumaqq.events.ItemMouseTrackListener;
import edu.tsinghua.lumaqq.models.BlacklistMatcher;
import edu.tsinghua.lumaqq.models.ClusterIdMatcher;
import edu.tsinghua.lumaqq.models.ClusterMatcher;
import edu.tsinghua.lumaqq.models.ClusterModel;
import edu.tsinghua.lumaqq.models.FriendModel;
import edu.tsinghua.lumaqq.models.FriendlyMatcher;
import edu.tsinghua.lumaqq.models.GroupModel;
import edu.tsinghua.lumaqq.models.IQQNode;
import edu.tsinghua.lumaqq.models.LatestMatcher;
import edu.tsinghua.lumaqq.models.MobileModel;
import edu.tsinghua.lumaqq.models.MobileNumberMatcher;
import edu.tsinghua.lumaqq.models.QQNumberMatcher;
import edu.tsinghua.lumaqq.models.StrangerMatcher;
import edu.tsinghua.lumaqq.qq.Util;
import edu.tsinghua.lumaqq.qq.beans.ClusterInfo;
import edu.tsinghua.lumaqq.ui.MainShell;
import edu.tsinghua.lumaqq.ui.dialogs.SelectGroupDialog;
import edu.tsinghua.lumaqq.ui.tool.ShellRegistry;
import edu.tsinghua.lumaqq.utils.MobileUtil;
import edu.tsinghua.lumaqq.utils.OptionUtil;
import edu.tsinghua.lumaqq.utils.RemarkUtil;
import edu.tsinghua.lumaqq.xml.groups.Cluster;
import edu.tsinghua.lumaqq.xml.groups.Friend;
import edu.tsinghua.lumaqq.xml.groups.Group;
import edu.tsinghua.lumaqq.xml.groups.Groups;
import edu.tsinghua.lumaqq.xml.groups.GroupsImpl;
import edu.tsinghua.lumaqq.xml.mobiles.Mobile;
import edu.tsinghua.lumaqq.xml.mobiles.MobileImpl;
import edu.tsinghua.lumaqq.xml.remarks.Remark;
import edu.tsinghua.swt.models.DefaultShutterModel;
import edu.tsinghua.swt.models.INode;
import edu.tsinghua.swt.models.ShutterModel;
import edu.tsinghua.swt.widgets.CoolButton;
import edu.tsinghua.swt.widgets.Shutter;

/**
 * 帮助类,封装一些查询方法,方便在model或者view中找到想要的部分
 * 
 * @author luma
 */
public class MVCHelper {
    private static Log log = LogFactory.getLog(MVCHelper.class);
    
    private MainShell main;
    private IconHolder icons;
    
    // 分组信息文件根元素对象
    private Groups groups;    
    // 群查找器
    private ClusterIdMatcher clusterIdMatcher;
    // 群组查找器
    private ClusterMatcher clusterMatcher;
    // 好友组查找器
    private FriendlyMatcher friendlyMatcher;
    // QQ号码比较器
    private QQNumberMatcher qqNumMatcher;
    // 最近联系人查找器
    private LatestMatcher latestMatcher;
    // 黑名单组查找器
    private BlacklistMatcher blacklistMatcher;
    // 陌生人组查找器
    private StrangerMatcher strangerMatcher;
    // 手机好友查找器
    private MobileNumberMatcher mobileNumberMatcher; 
    
    // item鼠标事件监听器,item可能是群,也可能是好友
    private ItemMouseListener itemMouseListener;
    private ItemMouseTrackListener itemMouseTrackListener;
    // 组鼠标事件监听器
    private GroupMouseListener groupMouseListener;
    
    // 好友添加的哈希表,这个哈希表保存了要把好友添加到的目的组
    private Hashtable addTos;
    
    /**
     * 创建一个帮助类
     * 
     * @param main
     * 		MainShell
     */
    public MVCHelper(MainShell main) {
        this.main = main;
        this.icons = IconHolder.getInstance();
        clusterIdMatcher = new ClusterIdMatcher();
        clusterMatcher = new ClusterMatcher();
		friendlyMatcher = new FriendlyMatcher();
		qqNumMatcher = new QQNumberMatcher();
		latestMatcher = new LatestMatcher();
		blacklistMatcher = new BlacklistMatcher();
		strangerMatcher = new StrangerMatcher();
		mobileNumberMatcher = new MobileNumberMatcher();
		addTos = new Hashtable();
		itemMouseListener = new ItemMouseListener(main);
		itemMouseTrackListener = new ItemMouseTrackListener(main);
		groupMouseListener = new GroupMouseListener(main);
    }    
    	
    /**
     * 移动一个好友到另外一个组
     * 
     * @param gIndex
     * @param fIndex
     * @param destGroup
     * @param f
     */
    public void moveFriend(int gIndex, int fIndex, int destGroup, FriendModel f) {
		main.getModel().removeItem(gIndex, fIndex);
		main.getModel().addItem(destGroup, f);
		main.setGroupDirty(true);
    }
    
	/**
	 * 创建最近联系人组
	 */
	public GroupModel createLatestGroup() {
	    int index = main.getModel().getTabCount();
		GroupModel g = new GroupModel(LumaQQ.getString("group.default.latest"));
		g.addProperty(IQQNode.UPLOAD, "false");
		g.addProperty(IQQNode.FRIENDLY, "false");		
		main.getModel().insertTab(index, g);
		Shutter shutter = main.getShutter();
		shutter.addItemMouseListener(itemMouseListener, index);
		shutter.addItemMouseTrackListener(itemMouseTrackListener, index);
		shutter.addContentMouseListener(groupMouseListener, index);
		return g;
	}
	
	/**
	 * 删除当前组
	 */
	public void removeCurrentGroup() {
		main.getModel().removeTab(main.getShutter().getCurrentTab());
		main.setGroupDirty(true);
	}
	
	/**
	 * 添加一个组
	 */
	public void addGroup() {
		Shutter shutter = main.getShutter();
		int index = shutter.getCurrentTab() + 1;
		main.getModel().insertTab(index, new GroupModel(""));
		shutter.getTabControl(index).editText();
		shutter.addItemMouseListener(itemMouseListener, index);
		shutter.addItemMouseTrackListener(itemMouseTrackListener, index);
		shutter.addContentMouseListener(groupMouseListener, index);
		main.setGroupDirty(true);
	}
	
	/**
	 * 设置model
	 * 
	 * @param newModel
	 * 		新的model
	 */
	public void setModel(ShutterModel model) {
		Shutter shutter = main.getShutter();
		MenuHelper menuHelper = main.getMenuHelper();
		MVCHelper mvcHelper = main.getMVCHelper();
		UIHelper uiHelper = main.getUIHelper();
		MessageQueue mq = main.getMessageQueue();
		OptionUtil options = OptionUtil.getInstance();
		
		// 设置model
		main.setModel(model);
		shutter.setModel(model);
		menuHelper.setModel(model);
		// 添加listener
		int size = model.getTabCount();
		for(int i = 0; i < size; i++) {
			// 添加事件监听器
			shutter.addItemMouseListener(itemMouseListener, i);				
			shutter.addItemMouseTrackListener(itemMouseTrackListener, i);				
			shutter.addContentMouseListener(groupMouseListener, i);
		}
		// 把this添加为model listener,这是为了判断model是否数据有改变,如果不用listener
		//     而是每个可能改变的操作里面都加上相应的代码,显的过于繁琐,且不好管理
		model.addModelListener(main);
		model.addPropertyListener(main);
		// 设置消息队列的model为这个model
		mq.setModel(model);
		// 根据系统设置中的是否只显示在线好友,来调整当前显示状态
		if(options.isShowOnlineOnly())
			uiHelper.showOnlineFriendOnly();
		// 根据系统设置中的是否显示小头像,来调整当前显示状态
		uiHelper.showSmallFace(options.isSmallFace());
		// 初始化分组上的好友数和在线数
		mvcHelper.setAllGroupStatistics();
	}
    	
	/**
	 * 添加一个好友到指定的组中,这个方法只会发送一个消息,要等服务器回复后才算
	 * 真正的添加了好友
	 * 
	 * @param qqNum
	 * 			好友的QQ号
	 * @param g 
	 * 			组的model
	 */
	public void addFriend(int qqNum, GroupModel g) {
		addTos.put(new Integer(qqNum), g);
		main.getClient().addFriend(qqNum);
	}
	
	/**
	 * 添加一个现成的friendmodel到我的好友组
	 * 
	 * @param f
	 */
	public void addFriendModel(FriendModel f) {
	    main.getModel().addItem(0, f);
	    main.setGroupDirty(true);
	}
	
	/**
	 * 添加一个好友的View part到shutter控件中
	 * 
	 * @param qqNum
	 * 			好友QQ号
	 */
	public void addFriendViewPart(int qqNum) {
		// 首先我们判断加入好友目的组的哈希表中是否有该项,如果有,则加入到指定的组
		// 如果没有或者指定的组已经不存在,加入到第一个好友组中
		Integer key = new Integer(qqNum);
		if(addTos.containsKey(key)) {
			GroupModel g = (GroupModel)addTos.remove(key);
			int index = main.getModel().indexOf(g); 
			if(index != -1) {
				addFriendViewPart(qqNum, index);
				return;				
			}
		}
		
		// 出一个窗口要用户选择一个组,然后把这个好友添加到用户选择的组中
		SelectGroupDialog sgd = new SelectGroupDialog(main.getShell());
		sgd.setModel(main.getModel());
		addFriendViewPart(qqNum, sgd.open());
	}	
	
	/**
	 * 添加一个好友的view part,这个好友将被添加到group索引指定的组中,一般来说0是"我的好友"组,
	 * 因为我的好友组在最前面,其次是1,2,3....
	 * 
	 * @param qqNum 
	 * 			好友号码
	 * @param group 
	 * 			目的组索引
	 */
	public void addFriendViewPart(int qqNum, int group) {
		FriendModel f = null;
		ShutterModel model = main.getModel();
		// 得到要添加的好友的组索引和组内索引,如果它已经在model中的话,返回的就不是null
		int[] indices = getFriendCoordinate(qqNum);
		if(indices != null) {
			/* 如果不是null,就要检查是否他已经是好友了 */
			GroupModel gModel = (GroupModel)model.getTab(indices[0]);
			if(gModel.isFriendly()) {
				log.debug("要添加的好友已经添加过了,忽略该次操作");
				return;				
			} else {
				/* 如果这个号码是在非好友组里,就直接得到他的model。然后删除这个model */
				f = (FriendModel)model.removeItem(indices[0], indices[1]);				
			}
		} else {
			/* 如果是null,那么还没有这个好友,添加之 */
			String qq = String.valueOf(qqNum);
			f = new FriendModel(qq, icons.getHead(0));
			f.addProperty(IQQNode.QQ_NUMBER, new Integer(qqNum));
			f.addProperty(IQQNode.STATUS, IQQNode.VALUE_OFFLINE);
			f.addProperty(IQQNode.HEAD, new Integer(0));
			f.addProperty(IQQNode.MEMBER, "false");
			if(!OptionUtil.getInstance().isShowNick()) {
			    Remark remark = RemarkUtil.getInstance().getRemark(qqNum);
			    if(remark != null)
			        f.addProperty(IQQNode.NAME, remark.getName());
			}
		}
		// 检查组索引是否合法,如果超出了,就加到第0组,即我的好友组
		if(group < 0 || group >= model.getTabCount())
			group = 0;
		// 添加到组中,同时请求得到这个好友的信息和刷新在线好友列表
		model.addItem(group, f);
		main.addOnline(f);
		main.getClient().getUserInfo(qqNum);			
		main.getClient().getFriendOnline();
		// 调整动画状态
		if(indices != null)
		    main.getUIHelper().adjustAnimateAfterDrag(qqNum, indices[0], group, f);
		
		// 分组信息已改变
		main.setGroupDirty(true);
		// 更新服务器端列表
		// 这个功能不再使用,因为LumaQQ会允许不自动更新分组信息,所以,这里搞成自动的会带来问题
		//client.addFriendToList(group, qqNum);
	}	
	
	/**
	 * 得到群的view part
	 * 
	 * @param clusterId
	 * 		群内部ID
	 * @return
	 * 		CoolButton对象
	 */
	public CoolButton getClusterViewPart(int clusterId) {
	    ShutterModel model = main.getModel();
	    Shutter shutter = main.getShutter();
		int[] cluster = model.findTabIndex(clusterMatcher);
		if(cluster != null && cluster.length > 0) {
			clusterIdMatcher.setClusterId(clusterId);
			int[] item = model.findItemIndex(cluster[0], clusterIdMatcher);
			if(item != null && item.length > 0)
				return shutter.getItemControl(cluster[0], item[0]);
			else
				return null;
		} else
			return null;
	}	
	
	/**
	 * 得到第一个群组的索引,一般也只有一个群组,除非用户去手动修改分组信息文件
	 * 
	 * @return 
	 * 		群组的索引
	 */

⌨️ 快捷键说明

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