方便管理plugin或rcp的icons
2016-06-15 00:00
501 查看
每个UI程序都离不开图片,一般的Eclipse插件,在一个类中定义了若干静态常量,每一个常量指定一个icon的名字,在程序中用到图片的时候,通过这个常量计算得到图片。Eclipse的插件一般规模较大,图片很多,而且分不同的像素,彩色,灰白等。这样有利于统一的管理和开发人员的沟通。
但并不是每个plugin或者rcp都要用到这么多图片,如果只有很少的图片的话,可以用图片的名字作为key,来存取图片。程序例子如下:
public class ImageShop {
private static ImageRegistry register = new ImageRegistry();
private static Set keys = new HashSet();
static {
initial();
}
public static ImageDescriptor getDescriptor(String key) {
ImageDescriptor image = register.getDescriptor(key);
if (image == null) {
image = ImageDescriptor.getMissingImageDescriptor();
}
return image;
}
public static Image get(String key) {
Image image = register.get(key);
if (image == null) {
image = ImageDescriptor.getMissingImageDescriptor().createImage();
}
return image;
}
public static String[] getImageKey() {
return (String[]) keys.toArray(new String[keys.size()]);
}
private static void initial() {
Bundle bundle = Platform.getBundle(PwdgatePlugin.ID);
URL url = bundle.getEntry("icons");
try {
url = Platform.asLocalURL(url);
} catch (Exception e) {
3ff0
PwdgatePlugin.log("get root path", e);
}
File file = new File(url.getPath());
File[] images = file.listFiles();
for (int i = 0; i < images.length; i++) {
File f = images[i];
if (!f.isFile()) {
continue;
}
String name = f.getName();
if (!name.endsWith(".gif")) {
continue;
}
String key = name.substring(0, name.indexOf('.'));
URL fullPathString = bundle.getEntry("icons/" + name);
ImageDescriptor des = ImageDescriptor.createFromURL(fullPathString);
register.put(key, des);
keys.add(key);
}
}
}
所有的图片都放在根目录/icons目录下,在系统中如果用到名字为default.gif的图片,只要调用ImageShop.get(“default”)即可;有时在Action中需要用到ImageDescriptor,调用ImageShop. getDescriptor(“default”)就可以取到。
但并不是每个plugin或者rcp都要用到这么多图片,如果只有很少的图片的话,可以用图片的名字作为key,来存取图片。程序例子如下:
public class ImageShop {
private static ImageRegistry register = new ImageRegistry();
private static Set keys = new HashSet();
static {
initial();
}
public static ImageDescriptor getDescriptor(String key) {
ImageDescriptor image = register.getDescriptor(key);
if (image == null) {
image = ImageDescriptor.getMissingImageDescriptor();
}
return image;
}
public static Image get(String key) {
Image image = register.get(key);
if (image == null) {
image = ImageDescriptor.getMissingImageDescriptor().createImage();
}
return image;
}
public static String[] getImageKey() {
return (String[]) keys.toArray(new String[keys.size()]);
}
private static void initial() {
Bundle bundle = Platform.getBundle(PwdgatePlugin.ID);
URL url = bundle.getEntry("icons");
try {
url = Platform.asLocalURL(url);
} catch (Exception e) {
3ff0
PwdgatePlugin.log("get root path", e);
}
File file = new File(url.getPath());
File[] images = file.listFiles();
for (int i = 0; i < images.length; i++) {
File f = images[i];
if (!f.isFile()) {
continue;
}
String name = f.getName();
if (!name.endsWith(".gif")) {
continue;
}
String key = name.substring(0, name.indexOf('.'));
URL fullPathString = bundle.getEntry("icons/" + name);
ImageDescriptor des = ImageDescriptor.createFromURL(fullPathString);
register.put(key, des);
keys.add(key);
}
}
}
所有的图片都放在根目录/icons目录下,在系统中如果用到名字为default.gif的图片,只要调用ImageShop.get(“default”)即可;有时在Action中需要用到ImageDescriptor,调用ImageShop. getDescriptor(“default”)就可以取到。
相关文章推荐
- GitHub和SourceTree入门教程
- DBA应该掌握的SQL语句(二)
- 红黑树
- 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-02-Example
- 什么是线程安全和线程不安全
- 手势操作
- python自定义模块导入方法
- VMWare 中Ubuntu 中的NAT 网络设置
- VMware提示:已将该虚拟机配置为使用 64 位客户机操作系统。但是,无法执行 64 位操作。解决方案
- NLP分析技术的三个层面
- 【CCCF专题】深度学习在自然语言处理中的应用
- Hadoop RPC机制总结
- 搭建kafka运行环境
- 推荐系统思考小结(基于Mahout)
- Eclipse下使用Stanford CoreNLP的方法
- 集群、分布式、负载均衡区别与联系
- nltk-比较英文文档相似度-完整实例
- msysGit管理GitHub代码
- Python fabric远程自动部署简介
- python列表 数组 字典