JFace ImageRegistry 封装,遍历目录,实现自动注册

背景:

SWT的图像创建是重要级的,需要释放资源

JFace封装了ImageRegistry以提供资源自动动释放功能,一般这样做: http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/ImageRegistryDemo.htm

import org.eclipse.jface.resource.*;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
 * This class tests ImageRegistry
 */
public class ImageRegistryTest extends ApplicationWindow {
  // Keys for the registry
  private static final String ONE = "one";
  private static final String TWO = "two";
  private static final String THREE = "three";

  /**
   * ImageRegistryTest constructor
   */
  public ImageRegistryTest() {
    super(null);
  }

  /**
   * Runs the application
   */
  public void run() {
    setBlockOnOpen(true);
    open();
    Display.getCurrent().dispose();
  }

  /**
   * Creates the window's contents
   * 
   * @param parent the parent composite
   * @return Control
   */
  protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new FillLayout());

    // Put the images in the registry
    ImageRegistry ir = new ImageRegistry();
    ir.put(ONE, ImageDescriptor.createFromFile(ImageRegistryTest.class,
        "java2s.gif"));
    ir.put(TWO, ImageDescriptor.createFromFile(ImageRegistryTest.class,
        "java2s.gif"));
    ir.put(THREE, ImageDescriptor.createFromFile(ImageRegistryTest.class,
        "java2s.gif"));

    // Create the labels and add the images
    Label label = new Label(composite, SWT.NONE);
    label.setImage(ir.get(ONE));
    label = new Label(composite, SWT.NONE);
    label.setImage(ir.get(TWO));
    label = new Label(composite, SWT.NONE);
    label.setImage(ir.get(THREE));

    getShell().pack();

    return composite;
  }

  /**
   * The application entry point
   * 
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    new ImageRegistryTest().run();
  }
}

但是一般应用程序图片比较多,图片增加,删除,修改名称时,都要动这个文件,注册起来比较麻烦,于是想了一个办法,把图片放到一个目录里,然后,遍历目录自动注册,以图片名称作为key

public class Registry {

	private static Logger logger = LoggerFactory.getLogger(Registry.class);

	private static String imagePath = Registry.class.getResource("images/").getPath();

	private static ImageRegistry imageRegistry = JFaceResources.getImageRegistry();

	static {
		initRegistry();
	}

	public static Image getImage(String key) {
		Image image = imageRegistry.get(key);
		if (image == null) {
			image = ImageDescriptor.getMissingImageDescriptor().createImage();
		}
		return image;
	}

	public static ImageDescriptor getDescriptor(String key) {
		ImageDescriptor descriptor = imageRegistry.getDescriptor(key);
		if (descriptor == null) {
			descriptor = ImageDescriptor.getMissingImageDescriptor();
		}
		return descriptor;
	}

	private static void initRegistry() {
		Collection<File> files = FileUtils.listFiles(new File(imagePath), null, false);
		for (File file : files) {
			imageRegistry.put(file.getName(),
					ImageDescriptor.createFromFile(Registry.class, "images/" + file.getName()));

		}

	}
}

因为图片基本是配置在properties文件里,所有文件增加,删除,修改,不用手动注册,只要改一下properties文件即可!

注意一点,此类在做单元测试时要注意:一定要搞个shell出来,不然JFaceResources.getImageRegistry()会报空!调了N长时间

单元测试参考代码:

	@Test
	public void testImageRegistry() {
		Display display = new Display();
		Shell shell = new Shell(display, SWT.NO);
		shell.setImage(display.getSystemImage(SWT.ICON_WARNING));
		shell.setBounds(100, 100, 640, 480);
		shell.setText("testImageRegistry");
		assertNotNull(JFaceResources.getImageRegistry());// must initialize a shell!!!

		// shell.open();
		// while (!shell.isDisposed()) {
		// if (!display.readAndDispatch())
		// display.sleep();
		// }
		// display.dispose();
	}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值