How to load image to view in RCP?

前端 未结 3 1390
孤独总比滥情好
孤独总比滥情好 2021-01-23 19:03

I am developing an RCP plugin project that includes certain views.First view take employee details like name ,address etc.There is an opt

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-23 19:26

    If it's an RCP App, I would go with a scalable solution.

    Create an ImageCache object, which you instantiate at the beginning of the app lifecycle (preferably in the Activator class of the app).

    This ImageCache can get images (and cache them, of course) from a path relative to the plugin (e.g. plugin has a folder icons; then, when you need an icon, you just call Activator.getDefault().getImage("icons/random.png"); - where getDefault() is the Activator singleton instance).

    Have two of these in the ImageCache:

    public ImageDescriptor getImageDescriptor(final String path)
    {
        ImageDescriptor imgD = AbstractUIPlugin.imageDescriptorFromPlugin(PLUGIN_ID, path);
    
        if (imgD == null)
        {
            return null; // OR a "missing icon", e.g. a red flag
        }
    }
    

    and

    public Image getImage(final String path)
    {
        Image image = imageCacheMap.get(path);
    
        if (image == null)
        {
            image = getImageDescripto(path).createImage();
            imageCacheMap.put(path, image);
        }
    
        return image;
    }
    

    Since these images need to be disposed, have a method dispose() in the ImageCache which is called in the stop() method of the Activator.

    There are many approaches to this. In my opinion, this is the best one for RCP apps.

提交回复
热议问题