Eclipse插件开发学习笔记 图像管理

Image对象是一个包装了本地资源的Java结构,不能被Java的垃圾回收器管理。加载图像而不卸载,会导致内存泄漏。

Eclipse使用ImageDescriptor来描述ImageImageDescriptor可以通过getImageDescriptor()方法来获得。

ImageCache 类为一个专门管理图像的类。
package  com.plugindev.addressbook.util;

import  java.util.HashMap;
import  java.util.Iterator;

import  org.eclipse.jface.resource.ImageDescriptor;
import  org.eclipse.swt.graphics.Image;

public   class  ImageCache {
    
private   final   static  HashMap < ImageDescriptor, Image >  imageMap  =   new  HashMap < ImageDescriptor, Image > ();

    
private   static  ImageCache instance;

    
// 单例,获得ImageCache的唯一实例
     public   static  ImageCache getInstance() {
        
if  (instance  ==   null )
            instance 
=   new  ImageCache();
        
return  instance;
    }

    
// 保证每个图像资源只有唯一的创建操作
     public  Image getImage(ImageDescriptor descriptor) {
        
if  (descriptor  ==   null )
            
return   null ;
        Image image 
=  (Image) imageMap.get(descriptor);
        
if  (image  ==   null ) {
            image 
=  descriptor.createImage();
            imageMap.put(descriptor, image);
        }
        
return  image;
    }

    
// 卸载图像资源
     public   void  dispose() {
        Iterator iter 
=  imageMap.values().iterator();
        
while  (iter.hasNext())
            ((Image) iter.next()).dispose();
        imageMap.clear();
    }
}

ImageKey保存所有图像路径字符串描述符
package  com.plugindev.addressbook.util;

import  org.eclipse.jface.resource.ImageDescriptor;

import  com.plugindev.addressbook.Activator;

public   final   class  ImageKeys {
    
public   static   final  String IMAGE_CATEGORY  =   " icons/category.gif " ;
    
public   static   final  String IMAGE_PEOPLE  =   " icons/people.gif " ;
    
    
// 类别图像
     public   static   final  String IMG_CAT_BUSINESS  =   " icons/category/business.gif " ;
    
public   static   final  String IMG_CAT_FAMILY  =   " icons/category/family.gif " ;
    
public   static   final  String IMG_CAT_FRIENDS  =   " icons/category/friends.gif " ;
    
public   static   final  String IMG_CAT_LOVER  =   " icons/category/lover.gif " ;
    
public   static   final  String IMG_CAT_MATE  =   " icons/category/mate.gif " ;
    
public   static   final  String IMG_CAT_ORDINARY  =   " icons/category/ordinary.gif " ;
    
public   static   final  String IMG_CAT_VIP  =   " icons/category/vip.gif " ;
    
public   static   final  String IMG_CAT_UNKNOWN  =   " icons/category/unknown.gif " ;
    
public   static   final  String IMG_CAT_TEACHER  =   " icons/category/teacher.gif " ;
    
    
// 操作图像
     public   static   final  String IMG_TOOL_ADD  =   " icons/actions/add.gif " ;
    
public   static   final  String IMG_TOOL_DELETE  =   " icons/actions/delete.gif " ;
    
public   static   final  String IMG_TOOL_DISABLEDELETE  =   " icons/actions/delete_dis.gif " ;
    
public   static   final  String IMG_TOOL_FILTER  =   " icons/actions/filter.gif " ;
    
    
// 编辑器相关
     public   static   final  String IMG_FORM_BG  =   " icons/editors/form_banner.gif " // $NON-NLS-1$
     public   static   final  String IMG_HORIZONTAL  =   " icons/editors/th_horizontal.gif " // $NON-NLS-1$
     public   static   final  String IMG_VERTICAL  =   " icons/editors/th_vertical.gif " // $NON-NLS-1$
    
    
// ScrollBlock图标
     public   static   final  String IMG_SCROL_BASIC  =   " icons/editors/basic.gif " ;
    
public   static   final  String IMG_SCROL_PHONE  =   " icons/editors/phone.gif " ;
    
public   static   final  String IMG_SCROL_AREA  =   " icons/editors/area.gif " ;
    
public   static   final  String IMG_SCROL_CONTACT  =   " icons/editors/contact.gif " ;
    
    
// 向导相关
     public   static   final  String IMG_WIZARD_NEW  =   " icons/wizards/new_wiz.gif " ;
    
    
// 帮助相关
     public   static   final  String IMG_HELP  =   " icons/help/help.gif " ;
    
    
public   static  ImageDescriptor getImageDescriptor(String path)
    {
        
return  Activator.getImageDescriptor(path);
    }
}


你可能感兴趣的:(Eclipse插件开发学习笔记 图像管理)