eroself

          關于人生的程式,在這里譜寫......
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 ::  :: 管理

          方便管理plugin或rcp的icons

          Posted on 2008-01-30 17:24 鬼谷子 閱讀(572) 評論(2)  編輯  收藏 所屬分類: Eclipse

          轉(zhuǎn)自:http://blog.csdn.net/moneyice/archive/2006/04/25/677423.aspx

          每個UI程序都離不開圖片,一般的Eclipse插件,在一個類中定義了若干靜態(tài)常量,每一個常量指定一個icon的名字,在程序中用到圖片的時候,通過這個常量計算得到圖片。Eclipse的插件一般規(guī)模較大,圖片很多,而且分不同的像素,彩色,灰白等。這樣有利于統(tǒng)一的管理和開發(fā)人員的溝通。
                 但并不是每個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目錄下,在系統(tǒng)中如果用到名字為default.gif的圖片,只要調(diào)用ImageShop.get(“default”)即可;有時在Action中需要用到ImageDescriptor,調(diào)用ImageShop. getDescriptor(“default”)就可以取到。

          評論

          # re: 方便管理plugin或rcp的icons  回復  更多評論   

          2008-10-04 23:23 by noia_zhou
          PwdgatePlugin 類代碼是什么,能否貼出來看看?

          # re: 方便管理plugin或rcp的icons[未登錄]  回復  更多評論   

          2008-10-07 09:37 by 鬼谷子
          public class PwdgatePlugin extends AbstractUIPlugin {

          public static final String ID = "pwdgate";

          // The shared instance
          private static PwdgatePlugin plugin;

          /**
          * The constructor
          */
          public PwdgatePlugin() {
          }

          /*
          * (non-Javadoc)
          * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
          */
          public void start(BundleContext context) throws Exception {
          super.start(context);
          plugin = this;
          }

          /*
          * (non-Javadoc)
          * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
          */
          public void stop(BundleContext context) throws Exception {
          plugin = null;
          super.stop(context);
          }

          /**
          * Returns the shared instance
          *
          * @return the shared instance
          */
          public static PwdgatePlugin getDefault() {
          return plugin;
          }

          /**
          * Returns an image descriptor for the image file at the given
          * plug-in relative path
          *
          * @param path the path
          * @return the image descriptor
          */
          public static ImageDescriptor getImageDescriptor(String path) {
          return imageDescriptorFromPlugin(ID, path);
          }

          public static void log(String str, Exception e) {
          log(new Status(IStatus.ERROR, ID, IStatusConstants.INTERNAL_ERROR, str, e));
          }

          public static void log(IStatus status) {
          getDefault().getLog().log(status);
          }

          }
          主站蜘蛛池模板: 大城县| 岗巴县| 天门市| 冀州市| 绥德县| 沂南县| 密山市| 孟津县| 宁河县| 镇赉县| 黎川县| 龙山县| 岳阳县| 洛隆县| 科尔| 河西区| 司法| 姚安县| 洮南市| 宁夏| 铜鼓县| 德昌县| 巩义市| 利津县| 公主岭市| 滕州市| 马公市| 昌邑市| 嘉义县| 井冈山市| 江阴市| 大新县| 大厂| 化隆| 库伦旗| 太湖县| 胶州市| 修武县| 韶山市| 湘乡市| 汶上县|