方便管理plugin或rcp的icons

 From: http://blog.csdn.net/moneyice/archive/2006/04/25/677423.aspx
        每个UI程序都离不开图片,一般的Eclipse插件,在一个类中定义了若干静态常量,每一个常量指定一个icon的名字,在程序中用到图片的 时候,通过这个常量计算得到图片。Eclipse的插件一般规模较大,图片很多,而且分不同的像素,彩色,灰白等。这样有利于统一的管理和开发人员的沟 通。
       但并不是每个plugin或者rcp都要用到这么多图片,如果只有很少的图片的话,可以用图片的名字作为key,来存取图片。程序例子如下:
java 代码
  1. public class ImageShop {   
  2.        private static ImageRegistry register = new ImageRegistry();   
  3.     
  4.        private static Set keys = new HashSet();   
  5.        static {   
  6.               initial();   
  7.        }   
  8.        public static ImageDescriptor getDescriptor(String key) {   
  9.               ImageDescriptor image = register.getDescriptor(key);   
  10.               if (image == null) {   
  11.                      image = ImageDescriptor.getMissingImageDescriptor();   
  12.               }   
  13.               return image;   
  14.        }   
  15.        public static Image get(String key) {   
  16.               Image image = register.get(key);   
  17.               if (image == null) {   
  18.                      image = ImageDescriptor.getMissingImageDescriptor().createImage();   
  19.               }   
  20.               return image;   
  21.        }   
  22.        public static String[] getImageKey() {   
  23.               return (String[]) keys.toArray(new String[keys.size()]);   
  24.        }   
  25.        private static void initial() {   
  26.               Bundle bundle = Platform.getBundle(PwdgatePlugin.ID);   
  27.               URL url = bundle.getEntry("icons");   
  28.               try {   
  29.                      url = Platform.asLocalURL(url);   
  30.               } catch (Exception e) {   
  31.                      PwdgatePlugin.log("get root path", e);   
  32.               }   
  33.               File file = new File(url.getPath());   
  34.               File[] images = file.listFiles();   
  35.               for (int i = 0; i < images.length; i++) {   
  36.                      File f = images[i];   
  37.                      if (!f.isFile()) {   
  38.                             continue;   
  39.                      }   
  40.                      String name = f.getName();   
  41.                      if (!name.endsWith(".gif")) {   
  42.                             continue;   
  43.                      }   
  44.                      String key = name.substring(0, name.indexOf('.'));   
  45.                      URL fullPathString = bundle.getEntry("icons/" + name);   
  46.                      ImageDescriptor des = ImageDescriptor.createFromURL(fullPathString);   
  47.                      register.put(key, des);   
  48.                      keys.add(key);   
  49.               }   
  50.        }   
  51. }   
  52.    
所有的图片都放在根目录/icons目录下,在系统中如果用到名字为default.gif的图片,只要调用ImageShop.get (“default”)即可;有时在Action中需要用到ImageDescriptor,调用ImageShop. getDescriptor(“default”)就可以取到。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值