📄 clustermodel.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.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.eclipse.swt.graphics.Image;
import edu.tsinghua.lumaqq.qq.QQ;
import edu.tsinghua.lumaqq.qq.beans.ClusterInfo;
import edu.tsinghua.swt.models.AbstractNode;
/**
* <pre>
* 一个群的model对象,集成自AbstractGeneralNode,具有以下属性
* clusterId: 群的号码,Integer类型
* name: 群的名称
* image: 群的头像,这个是本地头像,为了好看我自己加上的
* face: 头像的代码,目前随便提供了18个头像,从1到18,Integer类型
* info: ClusterInfo对象
* type: 群的类型,比如是固定群,还是多人对话,还是讨论组
* message: 对待消息的方式,可以是accept, eject, record, block
* accept = 接收并提示消息
* eject = 自动弹出消息
* record = 接收但不提示(只保存在聊天记录中)
* block = 阻止一切该群的消息
* members: 群的成员列表,为Map类型,其中的元素类型为FriendModel
* temp: 临时属性,可以放任何东西,目前用在了消息提示中,这个属性保留给程序员内部使用
* originalModel: 临时属性,如果一个model A是从model B克隆的,那么B就是originalModel
* messageCount: 未读的消息条数,Integer
* </pre>
*
* @author 马若劼
*/
public class ClusterModel extends AbstractNode implements IQQNode {
/**
* 私有构造函数
*/
private ClusterModel() {
// 没有什么要做的
}
/**
* 构造函数,群名称将会设成群ID的字符串形式
* @param clusterId 群ID
*/
public ClusterModel(int clusterId) {
this(new Integer(clusterId), String.valueOf(clusterId));
}
/**
* 构造函数
* @param clusterId 群ID
* @param name 群名称
*/
public ClusterModel(int clusterId, String name) {
this(new Integer(clusterId), name);
}
/**
* 构造函数
* @param clusterId 群ID
* @param name 群名称
*/
public ClusterModel(Integer clusterId, String name) {
super();
addProperty(CLUSTER_ID, clusterId);
addProperty(NAME, name);
addProperty(TYPE, new Integer(QQ.QQ_CLUSTER_TYPE_PERMANENT));
addProperty(MESSAGE, VALUE_ACCEPT);
addProperty(MEMBERS, new HashMap());
}
/**
* 复制所有properties到一个新创建的FriendModel中
*/
public ClusterModel cloneProperties() {
ClusterModel c = new ClusterModel();
Iterator iter = properties.keySet().iterator();
while(iter.hasNext()) {
String key = (String)iter.next();
c.addProperty(key, properties.get(key));
}
return c;
}
/**
* 构造函数
* @param clusterId 群ID
* @param name 群名称
* @param image 群头像
*/
public ClusterModel(Integer clusterId, String name, Image image) {
super();
addProperty(CLUSTER_ID, clusterId);
addProperty(NAME, name);
addProperty(IMAGE, image);
addProperty(TYPE, new Integer(QQ.QQ_CLUSTER_TYPE_PERMANENT));
addProperty(MESSAGE, VALUE_ACCEPT);
addProperty(MEMBERS, new HashMap());
}
/**
* 构造函数
* @param clusterId 群ID
* @param name 群名称
* @param image 群头像
*/
public ClusterModel(String clusterId, String name, Image image) {
this(new Integer(clusterId), name, image);
}
/**
* 构造函数
* @param clusterId 群ID
* @param name 群名称
* @param image 群头像
*/
public ClusterModel(int clusterId, String name, Image image) {
this(new Integer(clusterId), name, image);
}
/**
* @return
* 成员QQ号列表
*/
public List getMemberQQList() {
Map hash = getMembers();
List members = new ArrayList();
members.addAll(hash.keySet());
return members;
}
/**
* @return
* 消息设置字符常量
*/
public String getMessageOption() {
if(hasProperty(MESSAGE))
return (String)getProperty(MESSAGE);
else
return IQQNode.VALUE_ACCEPT;
}
/**
* @return
* ClusterInfo对象
*/
public ClusterInfo getClusterInfo() {
if(hasProperty(INFO))
return (ClusterInfo)getProperty(INFO);
else
return null;
}
/**
* @return
* 成员哈希表
*/
public Map getMembers() {
return (Map)getProperty(MEMBERS);
}
/**
* @return
* Member Version Id
*/
public int getVersionId() {
if(hasProperty(INFO))
return getClusterInfo().versionId;
else
return 0;
}
/**
* @param newId
* 新的Member Version Id
* @return
* true如果群资料已经过时,false表示群资料是最新的
*/
public boolean isClusterDirty(int newId) {
return getVersionId() != newId;
}
/**
* 设置version id
*
* @param newId
*/
public void setVersionId(int newId) {
if(hasProperty(INFO)) {
getClusterInfo().versionId = newId;
}
}
/**
* @return
* 成员的FriendModel列表
*/
public List getMemberList() {
Map hash = getMembers();
List members = new ArrayList();
members.addAll(hash.values());
return members;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
if(obj instanceof ClusterModel)
return ((Integer)getProperty(CLUSTER_ID)).equals(((ClusterModel)obj).getProperty(CLUSTER_ID));
else if(obj instanceof Integer)
return ((Integer)getProperty(QQ_NUMBER)).equals(((FriendModel)obj).getProperty(QQ_NUMBER));
else
return false;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return ((Integer)getProperty(CLUSTER_ID)).intValue();
}
/**
* @return true表示其是一个固定群
*/
public boolean isPermanent() {
return getType() == QQ.QQ_CLUSTER_TYPE_PERMANENT && getParentClusterId() != 0 ;
}
/**
* @return
* 父群内部ID,如果没有父群,返回0
*/
public int getParentClusterId() {
return getExternalId();
}
/**
* @return
* 群类型
*/
public byte getType() {
if(hasProperty(TYPE))
return (byte)((Integer)getProperty(TYPE)).intValue();
else
return 0;
}
/**
* 添加一个成员
* @param f
*/
public void addMember(FriendModel f) {
Map hash = getMembers();
hash.put(f, f);
}
/**
* 得到成员的model
* @param qqNum 成员的QQ号
* @return 成员的model,没有这个成员则返回null
*/
public FriendModel getMember(int qqNum) {
Map hash = getMembers();
return (FriendModel)hash.get(new Integer(qqNum));
}
/**
* 除去一个成员
* @param f
* @return
*/
public FriendModel removeMember(FriendModel f) {
Map hash = getMembers();
return (FriendModel)hash.remove(f);
}
/**
* 除去一个成员
* @param qqNum
* @return
*/
public FriendModel removeMember(Integer qqNum) {
Map hash = getMembers();
return (FriendModel)hash.remove(qqNum);
}
/**
* 除去一个成员
* @param qqNum
* @return
*/
public FriendModel removeMember(int qqNum) {
return removeMember(new Integer(qqNum));
}
/**
* 设置群的创建者QQ号
* @param qqNum
*/
public void setCreator(int qqNum) {
ClusterInfo info = (ClusterInfo)getProperty(INFO);
info.creator = qqNum;
}
/**
* 得到创建者的QQ号
* @return 创建者的QQ号
*/
public int getCreator() {
if(hasProperty(INFO)) {
ClusterInfo info = (ClusterInfo)getProperty(INFO);
return info.creator;
} else
return -1;
}
/**
* @return 头像ID,如果没有,返回一个缺省值
*/
public int getFaceId() {
if(hasProperty(HEAD))
return ((Integer)getProperty(HEAD)).intValue();
else
return 1;
}
/**
* @return 群的内部ID
*/
public int getClusterId() {
return ((Integer)getProperty(CLUSTER_ID)).intValue();
}
/**
* @return 群的头像
*/
public Image getImage() {
return hasProperty(IMAGE) ? ((Image)getProperty(IMAGE)) : null;
}
/**
* @return 群的内部ID
*/
public int getExternalId() {
if(hasProperty(INFO))
return ((ClusterInfo)getProperty(INFO)).externalId;
else
return -1;
}
/**
* @return
* 群名称
*/
public String getName() {
if(hasProperty(NAME))
return (String)getProperty(NAME);
else
return "";
}
/**
* @return
* 未读消息条数
*/
public int getMessageCount() {
if(hasProperty(MESSAGE_COUNT))
return ((Integer)getProperty(MESSAGE_COUNT)).intValue();
else
return 0;
}
/**
* 增加消息条数
*/
public void increaseMessageCount() {
addProperty(MESSAGE_COUNT, new Integer(getMessageCount() + 1));
}
/**
* 消息条数置0
*/
public void resetMessageCount() {
addProperty(MESSAGE_COUNT, new Integer(0));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -