服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 开发工具 > 查看文档

方便管理plugin或rcp的icons

每个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) {
                     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”)就可以取到。

扫描关注微信公众号