1 11 package org.eclipse.jdt.internal.ui.viewsupport; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 16 import org.eclipse.core.runtime.Assert; 17 18 import org.eclipse.swt.graphics.Image; 19 import org.eclipse.swt.widgets.Display; 20 21 import org.eclipse.jdt.internal.ui.util.SWTUtil; 22 import org.eclipse.jface.resource.ImageDescriptor; 23 24 27 public class ImageDescriptorRegistry { 28 29 private HashMap fRegistry= new HashMap (10); 30 private Display fDisplay; 31 32 36 public ImageDescriptorRegistry() { 37 this(SWTUtil.getStandardDisplay()); 38 } 39 40 46 public ImageDescriptorRegistry(Display display) { 47 fDisplay= display; 48 Assert.isNotNull(fDisplay); 49 hookDisplay(); 50 } 51 52 60 public Image get(ImageDescriptor descriptor) { 61 if (descriptor == null) 62 descriptor= ImageDescriptor.getMissingImageDescriptor(); 63 64 Image result= (Image)fRegistry.get(descriptor); 65 if (result != null) 66 return result; 67 68 Assert.isTrue(fDisplay == SWTUtil.getStandardDisplay(), "Allocating image for wrong display."); result= descriptor.createImage(); 70 if (result != null) 71 fRegistry.put(descriptor, result); 72 return result; 73 } 74 75 78 public void dispose() { 79 for (Iterator iter= fRegistry.values().iterator(); iter.hasNext(); ) { 80 Image image= (Image)iter.next(); 81 image.dispose(); 82 } 83 fRegistry.clear(); 84 } 85 86 private void hookDisplay() { 87 fDisplay.disposeExec(new Runnable () { 88 public void run() { 89 dispose(); 90 } 91 }); 92 } 93 } 94 | Popular Tags |