📄 imageresolver.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.widgets.rich;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageLoader;
import edu.tsinghua.lumaqq.IconHolder;
import edu.tsinghua.lumaqq.utils.FaceUtil;
/**
* 维护动画:载入,释放,等等
*
* @author luma
*/
public class ImageResolver implements DisposeListener {
// 图片哈希,key是图片tag和图片索引的组合,value是ImageLoader对象
private Map loaders;
// Image对象缓冲区,用来保存Image的引用,释放资源用
private Map imagePool;
private RichBox richbox;
public ImageResolver(RichBox richbox) {
this.richbox = richbox;
imagePool = new HashMap();
loaders = new HashMap();
}
/**
* 得到图片的Image对象
*
* @param tag
* @param code
* @return
*/
public Image getImage(int tag, int code) {
Image image = (Image)imagePool.get(getKey(tag, code));
if(image != null)
return image;
ImageLoader loader = getImageLoader(tag, code);
if(loader == null) {
return IconHolder.getInstance().getImage(IconHolder.icoNotFound);
}
Image ret = new Image(richbox.getDisplay(), loader.data[0]);
imagePool.put(getKey(tag, code), ret);
return ret;
}
/**
* 得到ImageLoader
*
* @param tag
* @param code
* @return
*/
public ImageLoader getImageLoader(int tag, int code) {
Integer key = getKey(tag, code);
ImageLoader loader = (ImageLoader)loaders.get(key);
if(loader == null) {
if(tag == IRichContent.DEFAULT_FACE_TAG) {
loader = new ImageLoader();
String path = IconHolder.getInstance().getFacePathByCode(code);
loader.load(this.getClass().getResourceAsStream(path));
loaders.put(key, loader);
} else if(tag == IRichContent.CUSTOM_FACE_TAG){
String path = null;
String md5 = FaceUtil.getInstance().getMd5ById(code);
if(md5 == null) {
md5 = FaceUtil.getInstance().getReceiveFaceMd5(code);
path = IconHolder.getInstance().getReceivedCustomFacePath(FaceUtil.getInstance().getReceivedFaceFileNameById(code));
} else
path = IconHolder.getInstance().getCustomFacePath(FaceUtil.getInstance().getFaceFileName(md5));
if(md5 == null)
return null;
if(path == null)
return null;
loader = new ImageLoader();
try {
loader.load(path);
} catch (Exception e) {
return null;
}
loaders.put(key, loader);
} else
return null;
}
return loader;
}
/**
* 检查某个图片是否是动画
*
* @param tag
* @param code
* @return
* true表示是动画
*/
public boolean isAnimate(int tag, int code) {
ImageLoader loader = getImageLoader(tag, code);
if(loader == null)
return false;
return loader.data.length > 1;
}
/**
* 得到图像的key
*
* @param tag
* @param code
* @return
*/
private Integer getKey(int tag, int code) {
return new Integer(((tag & 0xFFFF) << 16) | (code & 0xFFFF));
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
*/
public void widgetDisposed(DisposeEvent e) {
Iterator i = imagePool.values().iterator();
while(i.hasNext())
((Image)i.next()).dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -