1 11 package org.eclipse.team.internal.ui.synchronize; 12 13 import java.util.*; 14 15 import org.eclipse.compare.CompareConfiguration; 16 import org.eclipse.jface.resource.ImageDescriptor; 17 import org.eclipse.swt.graphics.Image; 18 import org.eclipse.team.core.ICache; 19 import org.eclipse.team.core.ICacheListener; 20 import org.eclipse.team.core.mapping.ISynchronizationContext; 21 import org.eclipse.team.internal.ui.TeamUIPlugin; 22 23 public class ImageManager { 24 25 private static final String PROP_IMAGE_MANAGER = TeamUIPlugin.ID + ".imageManager"; 27 private Map fgImageCache = new HashMap(10); 29 30 private CompareConfiguration compareConfig = new CompareConfiguration(); 32 33 private boolean disposed = false; 34 35 public synchronized static ImageManager getImageManager(ISynchronizationContext context) { 36 ImageManager manager = (ImageManager)context.getCache().get(PROP_IMAGE_MANAGER); 37 if (manager == null) { 38 final ImageManager newRegistry = new ImageManager(); 39 context.getCache().put(PROP_IMAGE_MANAGER, newRegistry); 40 context.getCache().addCacheListener(new ICacheListener() { 41 public void cacheDisposed(ICache cache) { 42 newRegistry.dispose(); 43 } 44 }); 45 manager = newRegistry; 46 } 47 return manager; 48 } 49 50 public Image getImage(ImageDescriptor descriptor) { 51 if (descriptor == null || disposed) 52 return null; 53 Image image = (Image)fgImageCache.get(descriptor); 54 if (image == null) { 55 image = descriptor.createImage(); 56 fgImageCache.put(descriptor, image); 57 } 58 return image; 59 } 60 61 public void dispose() { 62 disposed = true; 63 compareConfig.dispose(); 64 if (fgImageCache != null) { 65 Iterator it = fgImageCache.values().iterator(); 66 while (it.hasNext()) { 67 Image element = (Image) it.next(); 68 element.dispose(); 69 } 70 } 71 } 72 73 public Image getImage(Image base, int compareKind) { 74 if (disposed) 75 return null; 76 return compareConfig.getImage(base, compareKind); 77 } 78 } | Popular Tags |