JFace ImageRegistry 封装,遍历目录,实现自动注册
来源:互联网 发布:js控制audio播放 编辑:程序博客网 时间:2024/05/29 17:48
背景:
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()));}}}
注意一点,此类在做单元测试时要注意:一定要搞个shell出来,不然JFaceResources.getImageRegistry()会报空!调了N长时间
单元测试参考代码:
@Testpublic 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();}
- JFace ImageRegistry 封装,遍历目录,实现自动注册
- SWT(JFace)体验之ImageRegistry
- C++实现遍历目录
- PHP实现目录遍历
- MATLAB实现目录遍历
- PHP目录文件遍历实现
- PHP实现目录文件遍历
- File类实现目录遍历
- 封装MVP 实现登录注册功能
- C#实现自动注册自定义文件类型介绍
- ruckus 设置OPTION43 实现设备自动注册
- C++中实现自动注册的工厂
- 自动注册类厂 代码实现
- php5中用迭代器实现目录遍历
- 基于ACE实现C++目录遍历
- 使用非递归实现遍历目录
- c++与erlang实现目录遍历
- CFileFind类实现的目录树遍历
- VC++中unhandled exception的处理
- fatal error LNK1112: 模块计算机类型“X86”与目标计算机类型“x64”冲突——我的解决方案
- Windows8开发指南(16)开发基于Windows8的第一个metro界面C++程序
- 清楚换行和回车符
- 求1+2+...+n
- JFace ImageRegistry 封装,遍历目录,实现自动注册
- 取中间文本 c#
- session
- 调用log4net.dll时报一大堆错误的情况
- .mm不能滥用
- Extjs与php数据交互(增删查改)
- 从上往下打印树节点
- c# API 设置系统时间
- Application