现在的位置: 首页 > 综合 > 正文

方便管理plugin或rcp的icons

2013年09月21日 ⁄ 综合 ⁄ 共 2523字 ⁄ 字号 评论关闭
 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”)就可以取到。

抱歉!评论已关闭.