KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > decorators > OverlayCache


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.ui.internal.decorators;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.jface.resource.JFaceResources;
18 import org.eclipse.jface.resource.LocalResourceManager;
19 import org.eclipse.jface.viewers.DecorationOverlayIcon;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.graphics.Point;
22 import org.eclipse.swt.graphics.Rectangle;
23 import org.eclipse.ui.PlatformUI;
24
25 /**
26  * The OverlayCache is a helper class used by the DecoratorManger to manage the
27  * life cycle of overlaid images.
28  */

29 class OverlayCache {
30
31     /**
32      * The CacheEntry
33      *
34      * @since 3.3
35      *
36      */

37
38     private Set JavaDoc keys = new HashSet JavaDoc(); // Hold onto the cache entries we
39
// created
40
// Use a resource manager to hold onto any images we have to create
41
private LocalResourceManager resourceManager;
42
43     /**
44      *
45      */

46     public OverlayCache() {
47         super();
48         //As we are not in the UI Thread lookup the Display
49
resourceManager = new LocalResourceManager(JFaceResources
50                 .getResources(PlatformUI.getWorkbench().getDisplay()));
51     }
52
53     /**
54      * Returns and caches an image corresponding to the specified icon.
55      *
56      * @param icon
57      * the icon
58      * @return the image
59      */

60     private Image getImageFor(DecorationOverlayIcon icon) {
61         keys.add(icon);// Cache the keys so there is a reference somewhere
62
return resourceManager.createImage(icon);
63     }
64
65     /**
66      * Disposes of all images in the cache.
67      */

68     void disposeAll() {
69         keys.clear();
70         resourceManager.dispose();
71     }
72
73     /**
74      * Apply the descriptors for the receiver to the supplied image.
75      *
76      * @param source
77      * @param descriptors
78      * @return Image
79      */

80
81     Image applyDescriptors(Image source, ImageDescriptor[] descriptors) {
82         Rectangle bounds = source.getBounds();
83         Point size = new Point(bounds.width, bounds.height);
84         DecorationOverlayIcon icon = new DecorationOverlayIcon(source, descriptors, size);
85         return getImageFor(icon);
86     }
87
88 }
89
Popular Tags