KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > ImageManager


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 JavaDoc PROP_IMAGE_MANAGER = TeamUIPlugin.ID + ".imageManager"; //$NON-NLS-1$
26

27     // Cache for images that have been overlayed
28
private Map fgImageCache = new HashMap(10);
29     
30     // Contains direction images
31
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