📄 resources.java
字号:
* @param imageId 图标id
* @return 图标
*/
public Image getImage(int resId) {
if(resId < 0 || resId >= resourceLocations.length) return null;
return getImage(resourceLocations[resId]);
}
/**
* 得到某个图标的灰度图像
*
* @param resId
* @return
*/
public Image getGrayImage(int resId) {
// 尝试在registry中查找
String path = resourceLocations[resId] + ".offline";
Image img = registry.get(path);
// 尝试得到非灰度图像
if(img == null) {
img = getImage(resourceLocations[resId]);
if(img == null)
return null;
// 生成灰度图像
img = new Image(Display.getCurrent(), img, SWT.IMAGE_GRAY);
registry.put(path, img);
}
return img;
}
/**
* 得到程序图标
* @param s 资源路径
* @return Image
*/
private Image getImage(String s) {
Image ret = registry.get(s);
if(ret == null) {
ret = createImageFromJar(s);
if(ret != null)
registry.put(s, ret);
return ret;
} else
return ret;
}
/**
* 得到图像描述符
*
* @param resId
* 资源ID
* @return
* ImageDescriptor
*/
public ImageDescriptor getImageDescriptor(int resId) {
ImageDescriptor ret = registry.getDescriptor(resourceLocations[resId]);
if(ret == null) {
ret = ImageDescriptor.createFromFile(LumaQQ.class, resourceLocations[resId]);
if(ret != null)
registry.put(resourceLocations[resId], ret);
return ret;
} else
return ret;
}
/**
* <pre>
* 得到头像
* </pre>
*
* @param headId
* 头像ID
* @return
* 头像Image对象
*/
public Image getHead(int headId) {
// 检查头像范围,如果超出了,调整之
if(headId < 0) headId = 0;
if(headId >= QQ_2005_FACE_MAX_INDEX)
headId = QQ_2005_FACE_MAX_INDEX - 3;
// 计算头像的主号码和次号码,对于2004头像来说,次号码无用
int major = headId / 3 + 1;
int minor = headId % 3 + 1;
// 获得头像
String face = HEAD_PATH_PREFIX + major + ".gif";
if(minor == 1)
return getImage(face);
else {
// 检查cache中是否有不在线头像
String faceOffline = face + ".offline";
Image image = registry.get(faceOffline);
if(image == null) {
// 得到在线图像的灰度图像来用作不在线头像
image = getImage(face);
image = new Image(Display.getCurrent(), image, SWT.IMAGE_GRAY);
registry.put(faceOffline, image);
}
return image;
}
}
/**
* 得到头像小图标,没办法,table控件中的图像在linux下,居然不会自动缩小,只好再搞一套
* 小图标
*
* @param headId
* 头像Id
* @return 头像
*/
public Image getSmallHead(int headId) {
// 检查头像范围,如果超出了,调整之
if(headId < 0) headId = 0;
if(headId >= QQ_2005_FACE_MAX_INDEX) headId = QQ_2005_FACE_MAX_INDEX - 3;
int major = headId / 3 + 1;
int minor = headId % 3 + 1;
String face = SMALL_HEAD_PATH_PREFIX + major + "-" + minor + ".gif";
return getImage(face);
}
/**
* 得到群组头像图标,如果索引不对则返回null
* @param headId 群组头像,目前提供了18张
* @return 群组头像图标,如果索引不对返回null
*/
public Image getClusterHead(int headId) {
if(headId < 1 || headId > 6) return null;
String face = CLUSTER_HEAD_PATH_PREFIX + headId + ".gif";
return getImage(face);
}
/**
* 得到群组头像小图标,如果索引不对则返回null,没办法,linux下面不会自动缩小
* @param faceId
* @return
*/
public Image getSmallClusterHead(int faceId) {
if(faceId < 1 || faceId > 6) return null;
String face = CLUSTER_HEAD_PATH_PREFIX + faceId + "-1.gif";
return getImage(face);
}
/**
* 通过表情的顺序号得到图像,顺序号是从0开始的整数,和code不同,code不是顺序的
*
* @param seq
* 顺序号
* @return
* Image
*/
public Image getFaceBySequence(int seq) {
String smiley = FACE_PATH_PREFIX + seq + ".gif";
return getImage(smiley);
}
/**
* 根据表情代码得到表情的路径
*
* @param code
* 表情代码
* @return
* 路径
*/
public String getFacePathByCode(int code) {
int seq = getFaceSequence(code);
if(seq == -1)
return null;
else
return FACE_PATH_PREFIX + seq + ".gif";
}
/**
* 根据表情代码得到表情的图片
*
* @param code
* 表情代码
* @return
* Image
*/
public Image getFaceByCode(int code) {
return getFaceBySequence(getFaceSequence(code));
}
/**
* 根据表情代码得到ImageLoader
*
* @param code
* @return
*/
public ImageLoader getFaceLoaderByCode(int code) {
return getFaceLoaderBySequence(getFaceSequence(code));
}
/**
* 根据表情序号得到ImageLoader
*
* @param seq
* @return
*/
public ImageLoader getFaceLoaderBySequence(int seq) {
String smiley = FACE_PATH_PREFIX + seq + ".gif";
try {
ImageLoader loader = new ImageLoader();
loader.load(Resources.class.getResourceAsStream(smiley));
return loader;
} catch(RuntimeException e) {
return null;
}
}
/**
* 从文件创建Image对象
*
* @param path
* the relative path to the icon
*/
private Image createImageFromJar(String path) {
return createImageFromStream(Resources.class.getResourceAsStream(path));
}
/**
* 从一个外部文件中创建一个图片
*
* @param path
* @return
*/
private Image createImageFromFile(String path) {
try {
return createImageFromStream(new FileInputStream(path));
} catch (FileNotFoundException e) {
return null;
}
}
/**
* 从一个输入流中创建一个图片
*
* @param is
* @return
*/
private Image createImageFromStream(InputStream is) {
try {
if (is != null) {
ImageData imageData = new ImageData(is);
if (imageData != null) {
ImageData mask = imageData.getTransparencyMask();
return new Image(Display.getCurrent(), imageData, mask);
}
}
} catch (Exception e) {
log.error(e.getMessage());
} finally {
try {
if(is != null)
is.close();
} catch (IOException e1) {
}
}
return null;
}
/**
* 根据顺序号,得到表情代码
*
* @param seq
* 顺序号
* @return
* 表情代码,失败则返回-1
*/
public int getFaceCode(int seq) {
return DefaultFace.getFaceCode(seq);
}
/**
* 根据表情代码,得到顺序号
*
* @param code
* 表情代码
* @return
* 表情顺序号,失败返回1
*/
public int getFaceSequence(int code) {
return DefaultFace.getFaceSequence(code);
}
/**
* @return
* 表情个数
*/
public int getFaceCount() {
return DefaultFace.SEQ_CODE.length;
}
/**
* 得到自定义表情的缩略图
*
* @param md5
* 自定义表情的md5
* @return
* Image对象
*/
public Image getSmallCustomFace(String md5) {
if(md5 == null)
return null;
String path = FaceRegistry.getInstance().getSmallFacePath(md5);
Image ret = registry.get(path);
if(ret == null) {
ret = createImageFromFile(path);
if(ret != null)
registry.put(path, ret);
return ret;
} else
return ret;
}
/**
* 根据md5得到接收到的表情Image对象
*
* @param filename
* 图片文件名
* @return
* Image对象,如果失败返回null
*/
public Image getReceivedCustomFace(String filename) {
if(filename == null)
return null;
String path = FaceRegistry.getInstance().getReceivedFacePath(filename);
Image ret = registry.get(path);
if(ret == null) {
ret = createImageFromFile(path);
if(ret != null)
registry.put(path, ret);
return ret;
} else
return ret;
}
/**
* 根据id得到自定义头像Image对象
*
* @param id
* 头像id
* @param gray
* true表示得到灰度图像
* @return
* Image对象,如果失败返回null
*/
public Image getCustomHead(int id, boolean gray) {
String path = FaceRegistry.getInstance().getCustomHeadPath(id);
if(path == null)
return null;
if(!gray) {
Image ret = registry.get(path);
if(ret == null) {
ret = createImageFromFile(path);
if(ret != null)
registry.put(path, ret);
}
return ret;
} else{
String grayKey = path + ".offline";
Image ret = registry.get(grayKey);
if(ret == null) {
ret = registry.get(path);
if(ret == null) {
ret = createImageFromFile(path);
if(ret != null)
registry.put(path, ret);
}
if(ret != null) {
ret = new Image(Display.getCurrent(), ret, SWT.IMAGE_GRAY);
registry.put(grayKey, ret);
}
}
return ret;
}
}
/**
* 根据id得到小自定义头像Image对象
*
* @param id
* 头像id
* @param gray
* true表示得到灰度图像
* @return
* Image对象,如果失败返回null
*/
public Image getSmallCustomHead(int id, boolean gray) {
String path = FaceRegistry.getInstance().getSmallCustomHeadPath(id);
if(path == null)
return null;
if(!gray) {
Image ret = registry.get(path);
if(ret == null) {
ret = createImageFromFile(path);
if(ret != null)
registry.put(path, ret);
}
return ret;
} else{
String grayKey = path + ".offline";
Image ret = registry.get(grayKey);
if(ret == null) {
ret = registry.get(path);
if(ret == null) {
ret = createImageFromFile(path);
if(ret != null)
registry.put(path, ret);
}
if(ret != null) {
ret = new Image(Display.getCurrent(), ret, SWT.IMAGE_GRAY);
registry.put(grayKey, ret);
}
}
return ret;
}
}
/**
* @return
* 缺省字体
*/
public Font getDefaultFont() {
return JFaceResources.getFont(LUMAQQ_DEFAULT_FONT);
}
/**
* @return
* 缺省斜体
*/
public Font getItalicDefaultFont() {
return JFaceResources.getFontRegistry().getItalic(LUMAQQ_DEFAULT_FONT);
}
/**
* 根据文件扩展名得到相应的图标
*
* @param ext
* @return
*/
public Image getExtensionImage(String ext) {
Image img = registry.get(ext);
if(img != null)
return img;
else {
try {
Program p = Program.findProgram(ext);
if(p.getImageData() == null)
return getImage(icoFile);
else {
img = new Image(Display.getCurrent(), p.getImageData());
registry.put(ext, img);
return img;
}
} catch(RuntimeException e) {
return null;
}
}
}
/**
* 根据文件系统路径得到imageloader
*
* @param path
* @return
*/
public ImageLoader getImageLoader(String path) {
try {
ImageLoader loader = new ImageLoader();
loader.load(path);
return loader;
} catch(RuntimeException e) {
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -