01: /*******************************************************************************
02: * Copyright (c) 2000, 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal.decorators;
11:
12: import java.util.HashSet;
13: import java.util.Set;
14:
15: import org.eclipse.jface.resource.ImageDescriptor;
16: import org.eclipse.jface.resource.JFaceResources;
17: import org.eclipse.jface.resource.LocalResourceManager;
18: import org.eclipse.jface.viewers.DecorationOverlayIcon;
19: import org.eclipse.swt.graphics.Image;
20: import org.eclipse.swt.graphics.Point;
21: import org.eclipse.swt.graphics.Rectangle;
22: import org.eclipse.ui.PlatformUI;
23:
24: /**
25: * The OverlayCache is a helper class used by the DecoratorManger to manage the
26: * life cycle of overlaid images.
27: */
28: class OverlayCache {
29:
30: /**
31: * The CacheEntry
32: *
33: * @since 3.3
34: *
35: */
36:
37: private Set keys = new HashSet(); // Hold onto the cache entries we
38: // created
39: // Use a resource manager to hold onto any images we have to create
40: private LocalResourceManager resourceManager;
41:
42: /**
43: *
44: */
45: public OverlayCache() {
46: super ();
47: //As we are not in the UI Thread lookup the Display
48: resourceManager = new LocalResourceManager(JFaceResources
49: .getResources(PlatformUI.getWorkbench().getDisplay()));
50: }
51:
52: /**
53: * Returns and caches an image corresponding to the specified icon.
54: *
55: * @param icon
56: * the icon
57: * @return the image
58: */
59: private Image getImageFor(DecorationOverlayIcon icon) {
60: keys.add(icon);// Cache the keys so there is a reference somewhere
61: return resourceManager.createImage(icon);
62: }
63:
64: /**
65: * Disposes of all images in the cache.
66: */
67: void disposeAll() {
68: keys.clear();
69: resourceManager.dispose();
70: }
71:
72: /**
73: * Apply the descriptors for the receiver to the supplied image.
74: *
75: * @param source
76: * @param descriptors
77: * @return Image
78: */
79:
80: Image applyDescriptors(Image source, ImageDescriptor[] descriptors) {
81: Rectangle bounds = source.getBounds();
82: Point size = new Point(bounds.width, bounds.height);
83: DecorationOverlayIcon icon = new DecorationOverlayIcon(source,
84: descriptors, size);
85: return getImageFor(icon);
86: }
87:
88: }
|