📄 iconholder.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;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Display;
import edu.tsinghua.lumaqq.qq.Utils;
/**
* 管理图像资源
*/
public class IconHolder {
// Log
private static Log log = LogFactory.getLog(IconHolder.class);
// 使用2003还是2004头像
private boolean use2004;
// resource id
public static final int
icoOnline = 0,
icoAway = 1,
icoHidden = 2,
icoOffline = 3,
icoQQ = 4,
icoAbout = 5,
icoExit = 6,
icoSysMsg = 7,
icoSysOpt = 8,
icoChangeUser = 9,
icoBackground = 10,
icoPersonInfo = 11,
bmpLogin = 12,
icoReply = 13,
icoNoReply = 14,
icoCustomReply = 15,
icoMessage = 16,
icoFind = 17,
icoLargeFace = 18,
icoSmallFace = 19,
icoAddGroup = 20,
icoDelGroup = 21,
icoAddFriend = 22,
icoShowOnlineOnly = 23,
icoShowBlackList = 24,
icoRename = 25,
icoSortFriend = 26,
icoDisplayName = 27,
icoHideBlackList = 28,
icoChecked = 29,
icoFriendView = 30,
icoDelFriend = 31,
icoSendReceiveMessage = 32,
icoFont = 33,
icoSmiley = 34,
icoQuickReply = 35,
icoBlank = 36,
icoArrow = 37,
bmpSearch = 38,
icoSendFile = 39,
icoGroup = 40,
icoUploadGroup = 41,
icoDownloadGroup = 42,
icoMe = 43,
bmpTipBackground = 44,
bmpDefaultQQShow = 45,
bmpDownloading = 46,
icoMessageManage = 47,
icoTxtFile = 48,
icoCopy = 49,
icoCluster = 50,
icoSearchCluster = 51,
icoAddCluster = 52,
icoFolder = 53,
icoRightArrow = 54,
icoLeftArrow = 55,
bmpOnlineTipBackground = 56,
bmpMessageTipBackground = 57,
icoYes = 58,
icoNo = 59,
icoFriend = 60,
icoApply = 61,
icoDownloadRemark = 62,
icoBack = 63,
icoForward = 64,
icoRefresh = 65,
icoStop = 66,
icoUpdate = 67,
icoHomepage = 68,
bmpCheckUpdate = 69,
icoSearchIp = 70,
icoNA = 71;
// resource location
public static final String[] resourceLocations = {
"/icons/online.gif",
"/icons/away.gif",
"/icons/hidden.gif",
"/icons/offline.gif",
"/icons/qq.gif",
"/icons/about.gif",
"/icons/exit.gif",
"/icons/sysmsg.gif",
"/icons/sysoption.gif",
"/icons/changeuser.gif",
"/icons/bg.gif",
"/icons/personinfo.gif",
"/bitmaps/login.gif",
"/icons/reply.gif",
"/icons/noreply.gif",
"/icons/customreply.gif",
"/icons/message.gif",
"/icons/find.gif",
"/icons/largeface.gif",
"/icons/smallface.gif",
"/icons/addgroup.gif",
"/icons/delgroup.gif",
"/icons/addfriend.gif",
"/icons/showonlineonly.gif",
"/icons/showblacklist.gif",
"/icons/rename.gif",
"/icons/sortfriend.gif",
"/icons/displayname.gif",
"/icons/hideblacklist.gif",
"/icons/checked.gif",
"/icons/friendview.gif",
"/icons/delfriend.gif",
"/icons/sendreceivemessage.gif",
"/icons/font.gif",
"/icons/smiley.gif",
"/icons/quickreply.gif",
"/icons/blank.gif",
"/icons/arrow.gif",
"/bitmaps/search.gif",
"/icons/sendfile.gif",
"/icons/group.gif",
"/icons/uploadgroup.gif",
"/icons/downloadgroup.gif",
"/icons/me.gif",
"/bitmaps/tipbackground.gif",
"/bitmaps/defaultqqshow.gif",
"/bitmaps/downloading.gif",
"/icons/messagemanage.gif",
"/icons/txtfile.gif",
"/icons/copy.gif",
"/icons/cluster.gif",
"/icons/searchcluster.gif",
"/icons/addcluster.gif",
"/icons/folder.gif",
"/icons/rightarrow.gif",
"/icons/leftarrow.gif",
"/bitmaps/onlinetipbackground.gif",
"/bitmaps/messagetipbackground.gif",
"/icons/yes.gif",
"/icons/no.gif",
"/icons/friend.gif",
"/icons/apply.gif",
"/icons/downloadremark.gif",
"/icons/back.gif",
"/icons/forward.gif",
"/icons/refresh.gif",
"/icons/stop.gif",
"/icons/update.gif",
"/icons/homepage.gif",
"/bitmaps/checkupdate.gif",
"/icons/searchip.gif",
"/icons/na.gif"
};
// 存放资源的hash表
private Hashtable resources;
// 存放表情映射关系的properties类
private Properties smileyMap;
private static IconHolder holder = new IconHolder();
/**
* 私有构造,singleton模式
*/
private IconHolder() {
this.resources = new Hashtable();
this.smileyMap = new Properties();
try {
this.smileyMap.load(this.getClass().getResourceAsStream("/face.properties"));
} catch (IOException e) {
log.error(e.getMessage());
}
}
/**
* @return 实例
*/
public static IconHolder getInstance() {
return holder;
}
/**
* @param imageId 图标id
* @return 图标
*/
public Image getResource(int resId) {
if(resId < 0 || resId >= resourceLocations.length) return null;
return getResource(resourceLocations[resId]);
}
/**
* 得到程序图标
* @param s 资源路径
* @return Image
*/
private Image getResource(String s) {
Image ret = (Image)resources.get(s);
if(ret == null) {
ret = createStockImage(Display.getCurrent(), s);
if(ret != null)
resources.put(s, ret);
return ret;
} else
return ret;
}
/**
* <pre>
* 得到2003或者2004的头像,2003的头像3种状态都有,2004只有一种,所以处理上
* 有些不同
* </pre>
* @param faceId 头像ID
* @return 头像Image对象
*/
public Image getFace(int faceId) {
if(faceId < 0 || faceId >= 258) return null;
int major = faceId / 3 + 1;
int minor = faceId % 3 + 1;
if(use2004) {
String face = "/newface_2004/" + major + ".gif";
if(minor == 1)
return getResource(face);
else if(minor == 2){
// 检查cache中是否有不在线头像
String faceOffline = face + ".offline";
if(resources.containsKey(faceOffline))
return getResource(faceOffline);
else {
// 得到在线图像的灰度图像来用作不在线头像
Image image = getResource(face);
image = new Image(Display.getCurrent(), image, SWT.IMAGE_GRAY);
resources.put(faceOffline, image);
return image;
}
} else {
// 检查cache中是否有离开头像
String faceAway = face + ".away";
if(resources.containsKey(faceAway))
return getResource(faceAway);
else {
// 构建2004风格离开头像
Display dispay = Display.getCurrent();
Image original = new Image(dispay, getClass().getResourceAsStream(face));
Image na = getResource(icoNA);
Image image = new Image(dispay, original, SWT.IMAGE_COPY);
GC gc = new GC(image);
gc.drawImage(na, 12, 21);
gc.dispose();
original.dispose();
resources.put(faceAway, image);
return image;
}
}
} else {
String face = "/newface/" + major + "-" + minor + ".gif";
return getResource(face);
}
}
/**
* 得到头像小图标,没办法,在table控件和linux下,居然不会自动缩小,只好再搞一套
* 小图标
* @param faceId 头像Id
* @return 头像
*/
public Image getSmallFace(int faceId) {
if(faceId < 0 || faceId >= 255) return null;
int major = faceId / 3 + 1;
int minor = faceId % 3 + 1;
String face = "/newface_s/" + major + "-" + minor + ".gif";
return getResource(face);
}
/**
* 得到群组头像图标,如果索引不对则返回null
* @param faceId 群组头像,目前提供了18张
* @return 群组头像图标,如果索引不对返回null
*/
public Image getClusterFace(int faceId) {
if(faceId < 1 || faceId > 6) return null;
String face = "/clusterface/" + faceId + ".gif";
return getResource(face);
}
/**
* 得到群组头像小图标,如果索引不对则返回null,没办法,linux下面不会自动缩小
* @param faceId
* @return
*/
public Image getSmallClusterFace(int faceId) {
if(faceId < 1 || faceId > 6) return null;
String face = "/clusterface/" + faceId + "-1.gif";
return getResource(face);
}
/**
* 得到表情图标
* @param smileyId
* @return
*/
public Image getSmiley(int smileyId) {
String smiley = "/face/" + getSmileySeq(smileyId) + ".gif";
return getResource(smiley);
}
/**
* 释放资源
*/
public void freeResources() {
Iterator iter = resources.values().iterator();
while(iter.hasNext())
((Image)iter.next()).dispose();
resources.clear();
}
/**
* @param display
* the display
* @param path
* the relative path to the icon
*
* 从文件创建Image对象
*/
private Image createStockImage(Display display, String path) {
try {
InputStream stream = IconHolder.class.getResourceAsStream(path);
if (stream != null) {
ImageData imageData = new ImageData(stream);
if (imageData != null) {
ImageData mask = imageData.getTransparencyMask();
return new Image(display, imageData, mask);
}
}
} catch (Exception e) {
log.error(e.getMessage());
}
return null;
}
/**
* 根据代码,得到表情id,这个id和文件名是等同的,可是直接用getSmiley继续得到Image
* @param code
* @return
*/
private int getSmileySeq(int code) {
int ret = Utils.getInt(smileyMap.getProperty(String.valueOf(code)), 0);
return ret;
}
/**
* @param use2004 The use2004 to set.
*/
public void setUse2004(boolean use2004) {
this.use2004 = use2004;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -