/*******************************************************************************
* Copyright (c) 2011 Google, Inc.
* 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
*******************************************************************************/
package org.eclipse.wb.swt;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
/**
* Utility class for managing OS resources associated with SWT controls such as colors, fonts, images, etc.
* <p>
* !!! 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)
* <p>
* This class may be freely distributed as part of any application or plugin.
* <p>
* @author scheglov_ke
* @author Dan Rubel
*/
public class SWTResourceManager {
////////////////////////////////////////////////////////////////////////////
//
// Color
//
////////////////////////////////////////////////////////////////////////////
private static Map<RGB, Color> m_colorMap = new HashMap<RGB, Color>();
/**
* Returns the system {@link Color} matching the specific ID.
*
* @param systemColorID
* the ID value for the color
* @return the system {@link Color} matching the specific ID
*/
public static Color getColor(int systemColorID) {
Display display = Display.getCurrent();
return display.getSystemColor(systemColorID);
}
/**
* Returns a {@link Color} given its red, green and blue component values.
*
* @param r
* the red component of the color
* @param g
* the green component of the color
* @param b
* the blue component of the color
* @return the {@link Color} matching the given red, green and blue component values
*/
public static Color getColor(int r, int g, int b) {
return getColor(new RGB(r, g, b));
}
/**
* Returns a {@link Color} given its RGB value.
*
* @param rgb
* the {@link RGB} value of the color
* @return the {@link Color} matching the RGB value
*/
public static Color getColor(RGB rgb) {
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 {@link Color}'s.
*/
public static void disposeColors() {
for (Color color : m_colorMap.values()) {
color.dispose();
}
m_colorMap.clear();
}
////////////////////////////////////////////////////////////////////////////
//
// Image
//
////////////////////////////////////////////////////////////////////////////
/**
* Maps image paths to images.
*/
private static Map<String, Image> m_imageMap = new HashMap<String, Image>();
/**
* Returns an {@link Image} encoded by the specified {@link InputStream}.
*
* @param stream
* the {@link InputStream} encoding the image data
* @return the {@link Image} encoded by the specified input stream
*/
protected static Image getImage(InputStream stream) throws IOException {
try {
Display display = Display.getCurrent();
ImageData data = new ImageData(stream);
if (data.transparentPixel > 0) {
return new Image(display, data, data.getTransparencyMask());
}
return new Image(display, data);
} finally {
stream.close();
}
}
/**
* Returns an {@link Image} stored in the file at the specified path.
*
* @param path
* the path to the image file
* @return the {@link Image} stored in the file at the specified path
*/
public static Image getImage(String path) {
Image image = m_imageMap.get(path);
if (image == null) {
try {
image = getImage(new FileInputStream(path));
m_imageMap.put(path, image);
} catch (Exception e) {
image = getMissingImage();
m_imageMap.put(path, image);
}
}
return image;
}
/**
* Returns an {@link Image} stored in the file at the specified path relative to the specified class.
*
* @param clazz
* the {@link Class} relative to which to find the image
* @param path
* the path to the image file, if starts with <code>'/'</code>
* @return the {@link Image} stored in the file at the specified path
*/
public static Image getImage(Class<?> clazz, String path) {
String key = clazz.getName() + '|' + path;
Image image = m_imageMap.get(key);
if (image == null) {
try {
image = getImage(clazz.getResourceAsStream(path));
m_imageMap.put(key, image);
} catch (Exception e) {
image = getMissingImage();
m_imageMap.put(key, image);
}
}
return image;
}
private static final int MISSING_IMAGE_SIZE = 10;
/**
* @return the small {@link Image} that can be used as placeholder for missing image.
*/
private static Image getMissingImage() {
Image image = new Image(Display.getCurrent(), MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE);
//
GC gc = new GC(image);
gc.setBackground(getColor(SWT.COLOR_RED));
gc.fillRectangle(0, 0, MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE);
gc.dispose();
//
return image;
}
/**
* Style constant for placing decorator image in top left corner of base image.
*/
public static final int TOP_LEFT = 1;
/**
* Style constant for placing decorator image in top right corner of base image.
*/
public static final int TOP_RIGHT = 2;
/**
* Style constant for placing decorator image in bottom left corner of base image.
*/
public static final int BOTTOM_LEFT = 3;
/**
* Style constant for placing decorator image in bottom right corner of base image.
*/
public static final int BOTTOM_RIGHT = 4;
/**
* Internal value.
*/
protected static final int LAST_CORNER_KEY = 5;
/**
* Maps images to decorated images.
*/
@SuppressWarnings("unchecked")
private static Map<Image, Map<Image, Image>>[] m_decoratedImageMap = new Map[LAST_CORNER_KEY];
/**
* 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
* @return {@link Image} The resulting decorated image
*/
public static Image decorateImage(Image baseImage, Image decorator) {
return decorateImage(baseImage, decorator, BOTTOM_RIGHT);
}
/**
* 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) {
throw new IllegalArgumentException("Wrong decorate corner");
}
Map<Image, Map<Image, Image>> cornerDecoratedImageMap = m_decoratedImageMap[corner];
if (cornerDecoratedImageMap == null) {
cornerDecoratedImageMap = new HashMap<Image, Map<Image, Image>>();
m_decoratedImageMap[corner] =
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
本人十余年JAVA从业经验,精通JAVA高可用、分布式、高并发系统架构设计。有志于做JAVA职业规划、技术提升的可与我联系,交个朋友~ 本人十余年JAVA从业经验,精通JAVA高可用、分布式、高并发系统架构设计。有志于做JAVA职业规划、技术提升的可与我联系,交个朋友~ 本人十余年JAVA从业经验,精通JAVA高可用、分布式、高并发系统架构设计。有志于做JAVA职业规划、技术提升的可与我联系,交个朋友~
资源推荐
资源详情
资源评论
收起资源包目录
《JAVA课程设计》--java程序设计课程设计——应用swt和sql server实现商城管理系统.zip (112个子文件)
SWTResourceManager.class 11KB
ShoppingCartWin.class 7KB
DBM.class 6KB
ShopHomepageWin.class 6KB
KeeperListWin.class 6KB
Main.class 6KB
UserListWin.class 5KB
ChangeInfo.class 5KB
Shopping.class 5KB
Enroll.class 4KB
ULogin.class 4KB
BEnroll.class 4KB
BLogin.class 4KB
Findpsw1.class 4KB
UserCentre.class 3KB
PayWin$1.class 3KB
Findpsw2.class 3KB
ListManagementWin.class 3KB
ShopAddWin.class 3KB
ListManagementWin$1.class 3KB
PayWin.class 3KB
ShopAddWin$1.class 3KB
ShopDeleteWin.class 3KB
DeleteWin.class 2KB
ShopDeleteWin$1.class 2KB
DeleteWin$1.class 2KB
BLogin$1.class 2KB
ULogin$1.class 2KB
ChangeInfo$3.class 2KB
ChangeInfo$1.class 2KB
ChangeInfo$2.class 2KB
Main$1.class 2KB
Findpsw2$1.class 2KB
Shopping$1.class 2KB
Enroll$1.class 2KB
BEnroll$1.class 2KB
Main$5.class 2KB
Findpsw1$1.class 2KB
SMS_PRODUCTS.class 1KB
Enroll$2.class 992B
SMS_USER.class 968B
Keeper.class 952B
ShopHomepageWin$5.class 952B
ShopHomepageWin$3.class 943B
BEnroll$2.class 923B
UserCentre$2.class 922B
KeeperListWin$3.class 920B
ShoppingCartWin$3.class 914B
UserCentre$3.class 914B
UserCentre$5.class 912B
ChangeInfo$4.class 900B
UserCentre$4.class 900B
Findpsw1$3.class 892B
Findpsw2$3.class 892B
ShoppingCart.class 890B
Shopping$2.class 890B
BEnroll$3.class 887B
Main$2.class 884B
UserListWin$1.class 884B
ULogin$3.class 883B
BLogin$2.class 882B
UserCentre$1.class 882B
Enroll$4.class 880B
BLogin$3.class 880B
ULogin$4.class 879B
ULogin$2.class 879B
Main$3.class 876B
Main$4.class 874B
Findpsw2$2.class 863B
Findpsw1$2.class 860B
ShopHomepageWin$4.class 840B
ShoppingCartWin$4.class 830B
Enroll$3.class 824B
ShopHomepageWin$2.class 819B
KeeperListWin$2.class 818B
ShopHomepageWin$1.class 813B
KeeperListWin$1.class 793B
ShoppingCartWin$1.class 789B
ShoppingCartWin$2.class 782B
KeeperList.class 762B
UserList.class 756B
.classpath 565B
SWTResourceManager.java 14KB
ChangeInfo.java 7KB
DBM.java 7KB
Main.java 6KB
Enroll.java 5KB
ShoppingCartWin.java 5KB
ShopHomepageWin.java 5KB
Shopping.java 5KB
KeeperListWin.java 4KB
ULogin.java 4KB
BEnroll.java 4KB
BLogin.java 4KB
PayWin.java 4KB
Findpsw2.java 4KB
UserCentre.java 4KB
Findpsw1.java 4KB
ListManagementWin.java 3KB
UserListWin.java 3KB
共 112 条
- 1
- 2
资源评论
季风泯灭的季节
- 粉丝: 601
- 资源: 2920
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 01-spring-boot-overview
- vgg模型-基于深度学习AI算法对猫的动作行为识别-不含数据集图片-含逐行注释和说明文档.zip
- vgg模型-CNN图像分类识别人脸-不含数据集图片-含逐行注释和说明文档.zip
- Detect It Easy v3.03
- shufflenet模型-基于人工智能的卷积网络训练识别鞋类-不含数据集图片-含逐行注释和说明文档.zip
- shufflenet模型-python语言pytorch框架训练识别樱桃新鲜度-不含数据集图片-含逐行注释和说明文档.zip
- 优质资源,让 .NET 开发更简单,更通用,更流行
- resnet模型-基于深度学习对苹果颜色识别-不含数据集图片-含逐行注释和说明文档.zip
- mobilenet模型-基于图像分类算法对食物美食识别-不含数据集图片-含逐行注释和说明文档.zip
- ImmunityDebugger
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功