📄 resourcemanager.java
字号:
package com.swtdesigner;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
/**
* Utility class for managing OS resources associated with SWT controls such as
* colors, fonts, images, etc.
*
* !!! IMPORTANT !!! 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 (e.g. on
* application shutdown)
*
* Note: if you need to use this class in an environment without JFace, you will
* need to create a stub for the org.eclipse.jface.resource.ImageDescriptor class.
* The following should work fine:
*
* package org.eclipse.jface.resource;
* import java.net.URL;
* public abstract class ImageDescriptor {
* protected ImageDescriptor() {
* }
* public static ImageDescriptor createFromFile(Class location, String filename) {
* return null;
* }
* public static ImageDescriptor createFromURL(URL url) {
* return null;
* }
* public Image createImage() {
* return null;
* }
* }
*
* This class may be freely distributed as part of any application or plugin.
* <p>
* Copyright (c) 2003 - 2005, Instantiations, Inc. <br>All Rights Reserved
*
* @version $Revision: 1.14 $
* @author scheglov_ke
* @author Dan Rubel
*/
public class ResourceManager {
/**
* 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)
*/
public static void dispose() {
disposeColors();
disposeFonts();
disposeImages();
disposeCursors();
}
//////////////////////////////
// Color support
//////////////////////////////
/**
* Maps RGB values to colors
*/
private static HashMap m_ColorMap = new HashMap();
/**
* Returns the system color matching the specific ID
* @param systemColorID int The ID value for the color
* @return Color The system color matching the specific ID
*/
public static Color getColor(int systemColorID) {
Display display = Display.getCurrent();
return display.getSystemColor(systemColorID);
}
/**
* Returns a color given its red, green and blue component values
* @param r int The red component of the color
* @param g int The green component of the color
* @param b int The blue component of the color
* @return Color The color matching the given red, green and blue componet values
*/
public static Color getColor(int r, int g, int b) {
return getColor(new RGB(r, g, b));
}
/**
* Returns a color given its RGB value
* @param rgb RGB The RGB value of the color
* @return Color The color matching the RGB value
*/
public static Color getColor(RGB rgb) {
Color color = (Color) m_ColorMap.get(rgb);
if (color == null) {
Display display = Display.getCurrent();
color = new Color(display, rgb);
m_ColorMap.put(rgb, color);
}
return color;
}
/**
* Dispose of all the cached colors
*/
public static void disposeColors() {
for (Iterator iter = m_ColorMap.values().iterator(); iter.hasNext();)
((Color) iter.next()).dispose();
m_ColorMap.clear();
}
//////////////////////////////
// Image support
//////////////////////////////
/**
* Maps image names to images
*/
private static HashMap m_ClassImageMap = new HashMap();
/**
* Maps image descriptors to images
*/
private static HashMap m_DescriptorImageMap = new HashMap();
/**
* Maps images to image decorators
*/
private static HashMap m_ImageToDecoratorMap = new HashMap();
/**
* Returns an image encoded by the specified input stream
* @param is InputStream The input stream encoding the image data
* @return Image The image encoded by the specified input stream
*/
private static Image getImage(InputStream is) {
Display display = Display.getCurrent();
ImageData data = new ImageData(is);
if (data.transparentPixel > 0)
return new Image(display, data, data.getTransparencyMask());
return new Image(display, data);
}
/**
* Returns an image stored in the file at the specified path
* @param path String The path to the image file
* @return Image The image stored in the file at the specified path
*/
public static Image getImage(String path) {
return getImage("default", path);
}
/**
* Returns an image stored in the file at the specified path
* @param section The section to which belongs specified image
* @param path String The path to the image file
* @return Image The image stored in the file at the specified path
*/
public static Image getImage(String section, String path) {
String key = section + "|" + ResourceManager.class.getName() + "|" + path;
Image image = (Image) m_ClassImageMap.get(key);
if (image == null) {
try {
FileInputStream fis = new FileInputStream(path);
image = getImage(fis);
m_ClassImageMap.put(key, image);
fis.close();
} catch (Exception e) {
return null;
}
}
return image;
}
/**
* Returns an image stored in the file at the specified path relative to the specified class
* @param clazz Class The class relative to which to find the image
* @param path String The path to the image file
* @return Image The image stored in the file at the specified path
*/
public static Image getImage(Class clazz, String path) {
String key = clazz.getName() + "|" + path;
Image image = (Image) m_ClassImageMap.get(key);
if (image == null) {
if (path.length() > 0 && path.charAt(0) == '/') {
String newPath = path.substring(1, path.length());
image = getImage(new BufferedInputStream(clazz.getClassLoader().getResourceAsStream(newPath)));
} else {
image = getImage(clazz.getResourceAsStream(path));
}
m_ClassImageMap.put(key, image);
}
return image;
}
/**
* Returns an image descriptor stored in the file at the specified path relative to the specified class
* @param clazz Class The class relative to which to find the image descriptor
* @param path String The path to the image file
* @return ImageDescriptor The image descriptor stored in the file at the specified path
*/
public static ImageDescriptor getImageDescriptor(Class clazz, String path) {
return ImageDescriptor.createFromFile(clazz, path);
}
/**
* Returns an image descriptor stored in the file at the specified path
* @param path String The path to the image file
* @return ImageDescriptor The image descriptor stored in the file at the specified path
*/
public static ImageDescriptor getImageDescriptor(String path) {
try {
return ImageDescriptor.createFromURL((new File(path)).toURL());
} catch (MalformedURLException e) {
return null;
}
}
/**
* Returns an image based on the specified image descriptor
* @param descriptor ImageDescriptor The image descriptor for the image
* @return Image The image based on the specified image descriptor
*/
public static Image getImage(ImageDescriptor descriptor) {
if (descriptor == null)
return null;
Image image = (Image) m_DescriptorImageMap.get(descriptor);
if (image == null) {
image = descriptor.createImage();
m_DescriptorImageMap.put(descriptor, image);
}
return image;
}
/**
* Returns an image composed of a base image decorated by another image
* @param baseImage Image The base image that should be decorated
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -