Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7c0df56b21473c1f3ad585c61f4e6144b4f0c09d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*******************************************************************************
 * Copyright (c) 2013 Soft-Maint.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    David Couvrand (Soft-Maint) - Bug 418418 - [Customization] Overlay icons not implemented
 *    Thomas Cicognani (Soft-Maint) - Bug 424414 - ImageManager doesn't cache images
 *******************************************************************************/
package org.eclipse.papyrus.emf.facet.custom.ui.internal;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.util.URI;
import org.eclipse.papyrus.emf.facet.custom.ui.internal.custompt.ImageWrapper;
import org.eclipse.papyrus.emf.facet.custom.ui.internal.custompt.URIImage;
import org.eclipse.papyrus.emf.facet.util.core.Logger;
import org.eclipse.papyrus.emf.facet.util.swt.imageprovider.IImageProvider;
import org.eclipse.papyrus.emf.facet.util.swt.imageprovider.IImageProviderFactory;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.emf.facet.custom.metamodel.custompt.IImage;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.PlatformUI;
import org.osgi.framework.Bundle;

public class ImageManager {

	private final Map<String, ImageDescriptor> uriImageCache = new HashMap<String, ImageDescriptor>();

	private Image getImage(final URIImage uriImage) {
		Image result;
		final String uriStr = uriImage.getUri();
		ImageDescriptor imageDescriptor;
		if (this.uriImageCache.keySet().contains(uriStr)) {
			imageDescriptor = this.uriImageCache.get(uriStr);
		} else {
			imageDescriptor = createImageDescriptor(uriStr);
			this.uriImageCache.put(uriStr, imageDescriptor);
		}
		final IImageProvider imageProvider = IImageProviderFactory.DEFAULT
				.createIImageProvider(Activator.getDefault());
		result = imageProvider.getImage(imageDescriptor);
		return result;
	}


	private ImageDescriptor createImageDescriptor(final String uriStr) {
		ImageDescriptor imgDecriptor;
		final URI uri = URI.createURI(uriStr);
		final String bundleId = uri.segment(1);
		final Bundle bundle = Platform.getBundle(bundleId);
		final URI baseURI = URI.createPlatformPluginURI(bundleId + '/',
				false);
		final String resourcePath = uri.deresolve(baseURI).toString();
		final URL url = bundle.getResource(resourcePath);

		if (url == null) {
			Logger.logError(NLS.bind("Resource not found: {0}", //$NON-NLS-1$
					resourcePath), Activator.getDefault());
			imgDecriptor = ImageDescriptor.getMissingImageDescriptor();
		} else {
			imgDecriptor = ImageDescriptor.createFromURL(url);
		}
		return imgDecriptor;
	}


	public Image getImage(final IImage image) {
		Image result = null;
		final Device device = PlatformUI.getWorkbench().getDisplay();
		if (image instanceof ImageWrapper) {
			final ImageWrapper wrapper = (ImageWrapper) image;
			result = wrapper.getImage();
		} else if (image instanceof URIImage) {
			final URIImage uriImage = (URIImage) image;
			result = getImage(uriImage);
		} else if (image != null) {
			result = new Image(device, image.getInputStream());
		}
		return result;
	}

	public ImageDescriptor getImageDescriptor(final IImage image) {
		ImageDescriptor result = null;
		if (image instanceof URIImage) {
			final URIImage uriImage = (URIImage) image;
			result = createImageDescriptor(uriImage.getUri());
		}
		return result;
	}
}

Back to the top