2020-10-29 07:48:35 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2011 Google, Inc. and others
|
|
|
|
* 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:
|
|
|
|
* Google, Inc. - initial API and implementation
|
|
|
|
* Wim Jongman - 1.8 and higher compliance
|
|
|
|
*******************************************************************************/
|
2015-10-22 00:05:29 +02:00
|
|
|
package org.eclipse.wb.swt;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import org.eclipse.core.runtime.Platform;
|
|
|
|
import org.eclipse.jface.resource.CompositeImageDescriptor;
|
|
|
|
import org.eclipse.jface.resource.ImageDescriptor;
|
|
|
|
import org.eclipse.swt.graphics.Image;
|
2020-10-29 07:48:35 +01:00
|
|
|
import org.eclipse.swt.graphics.ImageData;
|
|
|
|
import org.eclipse.swt.graphics.ImageDataProvider;
|
2015-10-22 00:05:29 +02:00
|
|
|
import org.eclipse.swt.graphics.Point;
|
|
|
|
import org.eclipse.swt.graphics.Rectangle;
|
2020-10-29 07:48:35 +01:00
|
|
|
import org.osgi.framework.Bundle;
|
|
|
|
|
2017-01-23 22:53:14 +01:00
|
|
|
import com.minres.scviewer.e4.application.Messages;
|
2015-10-22 00:05:29 +02:00
|
|
|
|
|
|
|
/**
|
2020-10-29 07:48:35 +01:00
|
|
|
* Utility class for managing OS resources associated with SWT/JFace controls
|
|
|
|
* such as colors, fonts, images, etc.
|
|
|
|
*
|
|
|
|
* This class is created automatically when you fiddle around with images and
|
|
|
|
* colors in WB. You might want to prevent your application from using this
|
|
|
|
* class and provide your own more effective means of resource caching.
|
|
|
|
*
|
|
|
|
* Even though this class can be used to manage these resources, if they are
|
|
|
|
* here for the duration of the application and not used then you still have an
|
|
|
|
* effective resource leak.
|
2015-10-22 00:05:29 +02:00
|
|
|
*
|
2020-10-29 07:48:35 +01:00
|
|
|
* Application code must explicitly invoke the <code>dispose()</code> method to
|
|
|
|
* release the operating system resources managed by cached objects when those
|
|
|
|
* objects and OS resources are no longer needed.
|
2015-10-22 00:05:29 +02:00
|
|
|
*
|
|
|
|
* This class may be freely distributed as part of any application or plugin.
|
|
|
|
* <p>
|
|
|
|
*
|
|
|
|
* @author scheglov_ke
|
|
|
|
* @author Dan Rubel
|
2020-10-29 07:48:35 +01:00
|
|
|
* @author Wim Jongman
|
2015-10-22 00:05:29 +02:00
|
|
|
*/
|
|
|
|
public class ResourceManager extends SWTResourceManager {
|
2020-10-29 07:48:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The map where we store our images.
|
|
|
|
*/
|
2021-01-09 20:10:58 +01:00
|
|
|
private static Map<ImageDescriptor, Image> descriptorImageMap = new HashMap<>();
|
2020-10-29 07:48:35 +01:00
|
|
|
|
2015-10-22 00:05:29 +02:00
|
|
|
/**
|
2020-10-29 07:48:35 +01:00
|
|
|
* Returns an {@link ImageDescriptor} stored in the file at the specified path
|
|
|
|
* relative to the specified class.
|
2015-10-22 00:05:29 +02:00
|
|
|
*
|
2020-10-29 07:48:35 +01:00
|
|
|
* @param clazz the {@link Class} relative to which to find the image
|
|
|
|
* descriptor.
|
|
|
|
* @param path the path to the image file.
|
2015-10-22 00:05:29 +02:00
|
|
|
* @return the {@link ImageDescriptor} stored in the file at the specified path.
|
|
|
|
*/
|
|
|
|
public static ImageDescriptor getImageDescriptor(Class<?> clazz, String path) {
|
|
|
|
return ImageDescriptor.createFromFile(clazz, path);
|
|
|
|
}
|
2020-10-29 07:48:35 +01:00
|
|
|
|
2015-10-22 00:05:29 +02:00
|
|
|
/**
|
|
|
|
* Returns an {@link ImageDescriptor} stored in the file at the specified path.
|
|
|
|
*
|
2020-10-29 07:48:35 +01:00
|
|
|
* @param path the path to the image file.
|
2015-10-22 00:05:29 +02:00
|
|
|
* @return the {@link ImageDescriptor} stored in the file at the specified path.
|
|
|
|
*/
|
|
|
|
public static ImageDescriptor getImageDescriptor(String path) {
|
|
|
|
try {
|
|
|
|
return ImageDescriptor.createFromURL(new File(path).toURI().toURL());
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2020-10-29 07:48:35 +01:00
|
|
|
|
2015-10-22 00:05:29 +02:00
|
|
|
/**
|
|
|
|
* Returns an {@link Image} based on the specified {@link ImageDescriptor}.
|
|
|
|
*
|
2020-10-29 07:48:35 +01:00
|
|
|
* @param descriptor the {@link ImageDescriptor} for the {@link Image}.
|
2015-10-22 00:05:29 +02:00
|
|
|
* @return the {@link Image} based on the specified {@link ImageDescriptor}.
|
|
|
|
*/
|
|
|
|
public static Image getImage(ImageDescriptor descriptor) {
|
2021-01-09 20:10:58 +01:00
|
|
|
if (descriptor == null)
|
2015-10-22 00:05:29 +02:00
|
|
|
return null;
|
2021-01-09 20:10:58 +01:00
|
|
|
return descriptorImageMap.computeIfAbsent(descriptor, ImageDescriptor::createImage);
|
2015-10-22 00:05:29 +02:00
|
|
|
}
|
2020-10-29 07:48:35 +01:00
|
|
|
|
2015-10-22 00:05:29 +02:00
|
|
|
/**
|
|
|
|
* Maps images to decorated images.
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
2021-01-09 20:10:58 +01:00
|
|
|
private static Map<Image, Map<Image, Image>>[] decoratedImageMap = new Map[LAST_CORNER_KEY];
|
2020-10-29 07:48:35 +01:00
|
|
|
|
2015-10-22 00:05:29 +02:00
|
|
|
/**
|
|
|
|
* Returns an {@link Image} composed of a base image decorated by another image.
|
|
|
|
*
|
2020-10-29 07:48:35 +01:00
|
|
|
* @param baseImage the base {@link Image} that should be decorated.
|
|
|
|
* @param decorator the {@link Image} to decorate the base image.
|
2015-10-22 00:05:29 +02:00
|
|
|
* @return {@link Image} The resulting decorated image.
|
|
|
|
*/
|
|
|
|
public static Image decorateImage(Image baseImage, Image decorator) {
|
|
|
|
return decorateImage(baseImage, decorator, BOTTOM_RIGHT);
|
|
|
|
}
|
2020-10-29 07:48:35 +01:00
|
|
|
|
2015-10-22 00:05:29 +02:00
|
|
|
/**
|
|
|
|
* Returns an {@link Image} composed of a base image decorated by another image.
|
|
|
|
*
|
|
|
|
* @param baseImage
|
|
|
|
* the base {@link Image} that should be decorated.
|
|
|
|
* @param decorator
|
|
|
|
* the {@link Image} to decorate the base image.
|
|
|
|
* @param corner
|
|
|
|
* the corner to place decorator image.
|
|
|
|
* @return the resulting decorated {@link Image}.
|
|
|
|
*/
|
|
|
|
public static Image decorateImage(final Image baseImage, final Image decorator, final int corner) {
|
|
|
|
if (corner <= 0 || corner >= LAST_CORNER_KEY) {
|
2017-01-23 22:53:14 +01:00
|
|
|
throw new IllegalArgumentException(Messages.ResourceManager_0);
|
2015-10-22 00:05:29 +02:00
|
|
|
}
|
2021-01-09 20:10:58 +01:00
|
|
|
Map<Image, Map<Image, Image>> cornerDecoratedImageMap = decoratedImageMap[corner];
|
2015-10-22 00:05:29 +02:00
|
|
|
if (cornerDecoratedImageMap == null) {
|
2021-01-09 20:10:58 +01:00
|
|
|
cornerDecoratedImageMap = new HashMap<>();
|
|
|
|
decoratedImageMap[corner] = cornerDecoratedImageMap;
|
2015-10-22 00:05:29 +02:00
|
|
|
}
|
2021-01-09 20:10:58 +01:00
|
|
|
Map<Image, Image> decoratedMap = cornerDecoratedImageMap.computeIfAbsent(baseImage, k -> new HashMap<Image, Image>());
|
2021-01-09 23:24:00 +01:00
|
|
|
return decoratedMap.computeIfAbsent(decorator, k -> createImage(baseImage, decorator, corner));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Image createImage(final Image baseImage, final Image decorator, final int corner) {
|
|
|
|
final Rectangle bib = baseImage.getBounds();
|
|
|
|
final Rectangle dib = decorator.getBounds();
|
|
|
|
final Point baseImageSize = new Point(bib.width, bib.height);
|
|
|
|
CompositeImageDescriptor compositImageDesc = new CompositeImageDescriptor() {
|
|
|
|
@Override
|
|
|
|
protected void drawCompositeImage(int width, int height) {
|
|
|
|
drawImage(createCachedImageDataProvider(baseImage), 0, 0);
|
|
|
|
if (corner == TOP_LEFT) {
|
|
|
|
drawImage(getUnzoomedImageDataProvider(decorator.getImageData()) , 0, 0);
|
|
|
|
} else if (corner == TOP_RIGHT) {
|
|
|
|
drawImage(getUnzoomedImageDataProvider(decorator.getImageData()), bib.width - dib.width, 0);
|
|
|
|
} else if (corner == BOTTOM_LEFT) {
|
|
|
|
drawImage(getUnzoomedImageDataProvider(decorator.getImageData()), 0, bib.height - dib.height);
|
|
|
|
} else if (corner == BOTTOM_RIGHT) {
|
|
|
|
drawImage(getUnzoomedImageDataProvider(decorator.getImageData()), bib.width - dib.width, bib.height - dib.height);
|
2015-10-22 00:05:29 +02:00
|
|
|
}
|
2021-01-09 23:24:00 +01:00
|
|
|
}
|
|
|
|
@Override
|
|
|
|
protected Point getSize() {
|
|
|
|
return baseImageSize;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return compositImageDesc.createImage();
|
2015-10-22 00:05:29 +02:00
|
|
|
}
|
2021-01-09 20:10:58 +01:00
|
|
|
|
2020-10-29 07:48:35 +01:00
|
|
|
private static ImageDataProvider getUnzoomedImageDataProvider(ImageData imageData) {
|
|
|
|
return zoom -> zoom == 100 ? imageData : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-22 00:05:29 +02:00
|
|
|
/**
|
|
|
|
* Dispose all of the cached images.
|
|
|
|
*/
|
|
|
|
public static void disposeImages() {
|
|
|
|
SWTResourceManager.disposeImages();
|
|
|
|
// dispose ImageDescriptor images
|
2021-01-09 20:10:58 +01:00
|
|
|
for (Iterator<Image> I = descriptorImageMap.values().iterator(); I.hasNext();) {
|
|
|
|
I.next().dispose();
|
2015-10-22 00:05:29 +02:00
|
|
|
}
|
2021-01-09 20:10:58 +01:00
|
|
|
descriptorImageMap.clear();
|
2015-10-22 00:05:29 +02:00
|
|
|
// dispose decorated images
|
2021-01-09 20:10:58 +01:00
|
|
|
for (int i = 0; i < decoratedImageMap.length; i++) {
|
|
|
|
Map<Image, Map<Image, Image>> cornerDecoratedImageMap = decoratedImageMap[i];
|
2015-10-22 00:05:29 +02:00
|
|
|
if (cornerDecoratedImageMap != null) {
|
|
|
|
for (Map<Image, Image> decoratedMap : cornerDecoratedImageMap.values()) {
|
|
|
|
for (Image image : decoratedMap.values()) {
|
|
|
|
image.dispose();
|
|
|
|
}
|
|
|
|
decoratedMap.clear();
|
|
|
|
}
|
|
|
|
cornerDecoratedImageMap.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// dispose plugin images
|
2021-01-09 20:10:58 +01:00
|
|
|
for (Iterator<Image> I = urlImageMap.values().iterator(); I.hasNext();) {
|
|
|
|
I.next().dispose();
|
2015-10-22 00:05:29 +02:00
|
|
|
}
|
2021-01-09 20:10:58 +01:00
|
|
|
urlImageMap.clear();
|
2015-10-22 00:05:29 +02:00
|
|
|
}
|
2020-10-29 07:48:35 +01:00
|
|
|
|
2015-10-22 00:05:29 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Plugin images support
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
|
|
* Maps URL to images.
|
|
|
|
*/
|
2021-01-09 20:10:58 +01:00
|
|
|
private static Map<String, Image> urlImageMap = new HashMap<>();
|
2020-10-29 07:48:35 +01:00
|
|
|
|
2015-10-22 00:05:29 +02:00
|
|
|
/**
|
|
|
|
* Provider for plugin resources, used by WindowBuilder at design time.
|
|
|
|
*/
|
|
|
|
public interface PluginResourceProvider {
|
|
|
|
URL getEntry(String symbolicName, String path);
|
|
|
|
}
|
2020-10-29 07:48:35 +01:00
|
|
|
|
2015-10-22 00:05:29 +02:00
|
|
|
/**
|
2020-10-29 07:48:35 +01:00
|
|
|
* Instance of {@link PluginResourceProvider}, used by WindowBuilder at design
|
|
|
|
* time.
|
2015-10-22 00:05:29 +02:00
|
|
|
*/
|
2021-01-09 20:10:58 +01:00
|
|
|
private static PluginResourceProvider designTimePluginResourceProvider = null;
|
2020-10-29 07:48:35 +01:00
|
|
|
|
2015-10-22 00:05:29 +02:00
|
|
|
/**
|
|
|
|
* Returns an {@link Image} based on a {@link Bundle} and resource entry path.
|
|
|
|
*
|
2020-10-29 07:48:35 +01:00
|
|
|
* @param symbolicName the symbolic name of the {@link Bundle}.
|
|
|
|
* @param path the path of the resource entry.
|
2015-10-22 00:05:29 +02:00
|
|
|
* @return the {@link Image} stored in the file at the specified path.
|
|
|
|
*/
|
|
|
|
public static Image getPluginImage(String symbolicName, String path) {
|
|
|
|
try {
|
|
|
|
URL url = getPluginImageURL(symbolicName, path);
|
|
|
|
if (url != null) {
|
|
|
|
return getPluginImageFromUrl(url);
|
|
|
|
}
|
2021-01-09 20:10:58 +01:00
|
|
|
} catch (Exception e) {
|
2015-10-22 00:05:29 +02:00
|
|
|
// Ignore any exceptions
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2020-10-29 07:48:35 +01:00
|
|
|
|
2015-10-22 00:05:29 +02:00
|
|
|
/**
|
|
|
|
* Returns an {@link Image} based on given {@link URL}.
|
|
|
|
*/
|
|
|
|
private static Image getPluginImageFromUrl(URL url) {
|
|
|
|
try {
|
2021-01-09 20:10:58 +01:00
|
|
|
String key = url.toExternalForm();
|
|
|
|
Image image = urlImageMap.get(key);
|
|
|
|
if (image == null) {
|
|
|
|
InputStream stream = url.openStream();
|
|
|
|
try {
|
|
|
|
image = getImage(stream);
|
|
|
|
urlImageMap.put(key, image);
|
|
|
|
} finally {
|
|
|
|
stream.close();
|
2015-10-22 00:05:29 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-09 20:10:58 +01:00
|
|
|
return image;
|
|
|
|
} catch (Exception e) {
|
2015-10-22 00:05:29 +02:00
|
|
|
// Ignore any exceptions
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2020-10-29 07:48:35 +01:00
|
|
|
|
2015-10-22 00:05:29 +02:00
|
|
|
/**
|
2020-10-29 07:48:35 +01:00
|
|
|
* Returns an {@link ImageDescriptor} based on a {@link Bundle} and resource
|
|
|
|
* entry path.
|
2015-10-22 00:05:29 +02:00
|
|
|
*
|
2020-10-29 07:48:35 +01:00
|
|
|
* @param symbolicName the symbolic name of the {@link Bundle}.
|
|
|
|
* @param path the path of the resource entry.
|
|
|
|
* @return the {@link ImageDescriptor} based on a {@link Bundle} and resource
|
|
|
|
* entry path.
|
2015-10-22 00:05:29 +02:00
|
|
|
*/
|
|
|
|
public static ImageDescriptor getPluginImageDescriptor(String symbolicName, String path) {
|
|
|
|
try {
|
|
|
|
URL url = getPluginImageURL(symbolicName, path);
|
|
|
|
if (url != null) {
|
|
|
|
return ImageDescriptor.createFromURL(url);
|
|
|
|
}
|
2021-01-09 20:10:58 +01:00
|
|
|
} catch (Exception e) {
|
2015-10-22 00:05:29 +02:00
|
|
|
// Ignore any exceptions
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2020-10-29 07:48:35 +01:00
|
|
|
|
2015-10-22 00:05:29 +02:00
|
|
|
/**
|
|
|
|
* Returns an {@link URL} based on a {@link Bundle} and resource entry path.
|
|
|
|
*/
|
|
|
|
private static URL getPluginImageURL(String symbolicName, String path) {
|
|
|
|
// try runtime plugins
|
2021-01-09 20:10:58 +01:00
|
|
|
Bundle bundle = Platform.getBundle(symbolicName);
|
|
|
|
if (bundle != null) {
|
|
|
|
return bundle.getEntry(path);
|
2015-10-22 00:05:29 +02:00
|
|
|
}
|
|
|
|
// try design time provider
|
2021-01-09 20:10:58 +01:00
|
|
|
if (designTimePluginResourceProvider != null) {
|
|
|
|
return designTimePluginResourceProvider.getEntry(symbolicName, path);
|
2015-10-22 00:05:29 +02:00
|
|
|
}
|
|
|
|
// no such resource
|
|
|
|
return null;
|
|
|
|
}
|
2020-10-29 07:48:35 +01:00
|
|
|
|
2015-10-22 00:05:29 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// General
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
2020-10-29 07:48:35 +01:00
|
|
|
* Dispose of cached objects and their underlying OS resources. This should only
|
|
|
|
* be called when the cached objects are no longer needed (e.g. on application
|
|
|
|
* shutdown).
|
2015-10-22 00:05:29 +02:00
|
|
|
*/
|
|
|
|
public static void dispose() {
|
|
|
|
disposeColors();
|
|
|
|
disposeFonts();
|
|
|
|
disposeImages();
|
|
|
|
}
|
|
|
|
}
|