blob: 55af90088ff3528f33ea4437354cca813aa94a5a [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2016 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.wst.jsdt.js.node.internal.ui;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;
import org.eclipse.wst.jsdt.js.node.NodePlugin;
/**
* Utility class to handle image resources.
*/
public class ImageResource {
private static ImageRegistry imageRegistry;
private static Map<String, ImageDescriptor> imageDescriptors;
private static URL ICON_BASE_URL;
public static final String IMG_NODEJS = "node_16.png"; //$NON-NLS-1$
public static final String IMG_ARGUMENTS = "variable_tab.gif"; //$NON-NLS-1$
static {
try {
String pathSuffix = "icons/"; //$NON-NLS-1$
ICON_BASE_URL = NodePlugin.getDefault().getBundle()
.getEntry(pathSuffix);
} catch (Exception e) {
NodePlugin.logError(e , "Images error: " + e.getMessage()); //$NON-NLS-1$
}
}
/**
* Return the image with the given key.
*
* @param key
* java.lang.String
* @return org.eclipse.swt.graphics.Image
*/
public static Image getImage(String key) {
return getImage(key, null);
}
/**
* Return the image with the given key.
*
* @param key
* java.lang.String
* @return org.eclipse.swt.graphics.Image
*/
public static Image getImage(String key, String keyIfImageNull) {
if (imageRegistry == null)
initializeImageRegistry();
Image image = imageRegistry.get(key);
if (image == null) {
if (keyIfImageNull != null) {
return getImage(keyIfImageNull, null);
}
imageRegistry.put(key, ImageDescriptor.getMissingImageDescriptor());
image = imageRegistry.get(key);
}
return image;
}
/**
* Return the image descriptor with the given key.
*
* @param key
* java.lang.String
* @return org.eclipse.jface.resource.ImageDescriptor
*/
public static ImageDescriptor getImageDescriptor(String key) {
if (imageRegistry == null)
initializeImageRegistry();
ImageDescriptor id = imageDescriptors.get(key);
if (id != null)
return id;
return ImageDescriptor.getMissingImageDescriptor();
}
/**
* Initialize the image resources.
*/
protected static void initializeImageRegistry() {
imageRegistry = NodePlugin.getDefault().getImageRegistry();
imageDescriptors = new HashMap<String, ImageDescriptor>();
registerImage(IMG_NODEJS, IMG_NODEJS);
registerImage(IMG_ARGUMENTS, IMG_ARGUMENTS);
}
/**
* Register an image with the registry.
*
* @param key
* java.lang.String
* @param partialURL
* java.lang.String
*/
public static void registerImage(String key, String partialURL) {
try {
ImageDescriptor id = ImageDescriptor.createFromURL(new URL(
ICON_BASE_URL, partialURL));
registerImageDescriptor(key, id);
} catch (Exception e) {
NodePlugin.logError(e , "Error registering image" + e.getMessage()); //$NON-NLS-1$
}
}
public static void registerImageDescriptor(String key, ImageDescriptor id) {
if (imageRegistry == null)
initializeImageRegistry();
imageRegistry.put(key, id);
imageDescriptors.put(key, id);
}
}