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

📄 modelsorter.java

📁 java写的qq代码实现qq的部分功能
💻 JAVA
字号:
/*
* 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.models;

import java.text.Collator;
import java.util.Comparator;
import java.util.Locale;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;

import edu.tsinghua.swt.models.INode;


/**
 * <pre>
 * 用于对群组排序,群组和好友组是不同的,因为其有一个父子结构,所以排序的策略是:
 * 1. 如果都是群,则先按照类型排,固定群排在上面,然后相同类型的按照名称排
 * 2. 如果一个是群,一个是成员,如果这个成员属于这个群,那肯定成员排在后面,如果
 *    这个成员不属于这个群,则和这个成员的父群比较
 * 3. 如果两个都是成员且两个都在一个群中,按照好友那么排;如果两个在不同的群中,
 *    比较他们的父群
 * 
 * 好友排序的方法,原则是上线的在最前面,离开的在后面,离线或隐身的在最后,如果某个好友有消息
 * 来了,那么把他当作上线状态对待,其次按照昵称排序。
 * </pre>
 * @author 马若劼
 */
public class ModelSorter extends ViewerSorter implements Comparator {
    private static Collator collator = Collator.getInstance(Locale.CHINESE);
    
    /* (non-Javadoc)
     * @see org.eclipse.jface.viewers.ViewerSorter#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
     */
    public int compare(Viewer viewer, Object e1, Object e2) {
        return compare(e1, e2);
    }
    
	/* (non-Javadoc)
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 */
	public int compare(Object o1, Object o2) {
		if(o1 instanceof ClusterModel && o2 instanceof ClusterModel) {
			/* 如果两者都是群组 */
			ClusterModel c1 = (ClusterModel)o1;
			ClusterModel c2 = (ClusterModel)o2;
			return compareClusterModel(c1, c2);
		} else if(o1 instanceof ClusterModel || o2 instanceof ClusterModel) {
			/* 有一个是ClusterModel,另一个是FriendModel */
			ClusterModel c1 = null;
			ClusterModel c2 = null;
			if(o1 instanceof ClusterModel) {
				c1 = (ClusterModel)o1;
				c2 = (ClusterModel)((FriendModel)o2).getProperty(IQQNode.PARENT);
			} else {
				c1 = (ClusterModel)((FriendModel)o1).getProperty(IQQNode.PARENT);
				c2 = (ClusterModel)o2;
			}
			if(c1 == null || c2 == null)
			    return -1;
			return (c1 == c2) ? -1 : compareClusterModel(c1, c2);
		} else {
			/* 两个都是FriendModel */
			return compareFriendModel((FriendModel)o1, (FriendModel)o2);
		}
	}	
	
	/**
	 * 比较两个FriendModel

	 * @param f1
	 * 			好友1的model
	 * @param f2
	 * 			好友2的model
	 * @return
	 * 			根据排序位置返回1,0或者-1
	 */
	private int compareFriendModel(FriendModel f1, FriendModel f2) {
		// 得到好友状态,如果当前好友有消息未读,则也当成在线状态看待,所以
	    // 免得好友变成隐身之后就下去了,还要去找是哪个好友发了消息
		String s1 = f1.getStatus();
		String s2 = f2.getStatus();
		if(f1.hasMessage())
		    s1 = IQQNode.VALUE_ONLINE;
		if(f2.hasMessage())
		    s2 = IQQNode.VALUE_ONLINE;
		// 得到显示名
		String _n1 = (String)f1.getProperty(INode.NAME);
		String _n2 = (String)f2.getProperty(INode.NAME);
		// 比较
        if(s1.equalsIgnoreCase(s2))
        	return compareChineseString(_n1, _n2);
        else if(s1.equalsIgnoreCase(IQQNode.VALUE_ONLINE))
        	return -1;
        else if(s2.equalsIgnoreCase(IQQNode.VALUE_ONLINE))
        	return 1;
        else if(s1.equalsIgnoreCase(IQQNode.VALUE_AWAY))
        	return -1;
        else if(s2.equalsIgnoreCase(IQQNode.VALUE_AWAY))
        	return 1;
        else if(s1.equalsIgnoreCase(IQQNode.VALUE_HIDDEN))
            return -1;
        else if(s2.equalsIgnoreCase(IQQNode.VALUE_HIDDEN))
            return 1;
        else
        	return compareChineseString(_n1, _n2);
	}

	/**
	 * 比较两个ClusterModel
	 * 
	 * @param c1
	 * @param c2
	 * @return
	 */
	private int compareClusterModel(ClusterModel c1, ClusterModel c2) {
		if(c1.isPermanent()) {
			/* 如果群1是固定群 */
			if(c2.isPermanent()) // 如果群2也是固定群,比较名字
				return compareChineseString((String)c1.getProperty(INode.NAME), (String)c2.getProperty(INode.NAME));
			else // 如果群2不是固定群,群1排在前面
				return -1;
		} else {
			/* 如果c1不是固定群 */
			if(c2.isPermanent()) // 如果群2是固定群,群2排在前面
				return 1;
			else // 如果c2也不是固定群,比较名字
				return compareChineseString((String)c1.getProperty(INode.NAME), (String)c2.getProperty(INode.NAME));
		}
	}
	
	/**
	 * 比较中文字符串,比较后的结果按照拼音方式排序
	 * 
	 * @param _n1
	 * 			source
	 * @param _n2
	 * 			dest
	 * @return source大于dest返回1,等于返回0,小于返回-1
	 */
	private int compareChineseString(String _n1, String _n2) {
	    return collator.compare(_n1, _n2);
	}
}

⌨️ 快捷键说明

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