11 package org.eclipse.wb.swt;
13 import java.io.FileInputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.util.HashMap;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Color;
21 import org.eclipse.swt.graphics.Cursor;
22 import org.eclipse.swt.graphics.Font;
23 import org.eclipse.swt.graphics.FontData;
24 import org.eclipse.swt.graphics.GC;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.graphics.ImageData;
27 import org.eclipse.swt.graphics.RGB;
28 import org.eclipse.swt.graphics.Rectangle;
29 import org.eclipse.swt.widgets.Display;
49 private static Map<RGB, Color> m_colorMap =
new HashMap<RGB, Color>();
57 public static Color
getColor(
int systemColorID) {
58 Display display = Display.getCurrent();
59 return display.getSystemColor(systemColorID);
72 public static Color
getColor(
int r,
int g,
int b) {
83 Color color = m_colorMap.get(rgb);
85 Display display = Display.getCurrent();
86 color =
new Color(display, rgb);
87 m_colorMap.put(rgb, color);
95 for (Color color : m_colorMap.values()) {
108 private static Map<String, Image> m_imageMap =
new HashMap<String, Image>();
116 protected static Image
getImage(InputStream stream)
throws IOException {
118 Display display = Display.getCurrent();
119 ImageData data =
new ImageData(stream);
120 if (data.transparentPixel > 0) {
121 return new Image(display, data, data.getTransparencyMask());
123 return new Image(display, data);
136 Image image = m_imageMap.get(path);
139 image =
getImage(
new FileInputStream(path));
140 m_imageMap.put(path, image);
141 }
catch (Exception e) {
142 image = getMissingImage();
143 m_imageMap.put(path, image);
157 public static Image
getImage(Class<?> clazz, String path) {
158 String key = clazz.getName() +
'|' + path;
159 Image image = m_imageMap.get(key);
162 image =
getImage(clazz.getResourceAsStream(path));
163 m_imageMap.put(key, image);
164 }
catch (Exception e) {
165 image = getMissingImage();
166 m_imageMap.put(key, image);
171 private static final int MISSING_IMAGE_SIZE = 10;
175 private static Image getMissingImage() {
176 Image image =
new Image(Display.getCurrent(), MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE);
178 GC gc =
new GC(image);
179 gc.setBackground(
getColor(SWT.COLOR_RED));
180 gc.fillRectangle(0, 0, MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE);
208 @SuppressWarnings(
"unchecked")
209 private static Map<Image, Map<Image, Image>>[] m_decoratedImageMap = new Map[
LAST_CORNER_KEY];
233 public static Image
decorateImage(
final Image baseImage,
final Image decorator,
final int corner) {
235 throw new IllegalArgumentException(
"Wrong decorate corner");
237 Map<Image, Map<Image, Image>> cornerDecoratedImageMap = m_decoratedImageMap[corner];
238 if (cornerDecoratedImageMap ==
null) {
239 cornerDecoratedImageMap =
new HashMap<Image, Map<Image, Image>>();
240 m_decoratedImageMap[corner] = cornerDecoratedImageMap;
242 Map<Image, Image> decoratedMap = cornerDecoratedImageMap.get(baseImage);
243 if (decoratedMap ==
null) {
244 decoratedMap =
new HashMap<Image, Image>();
245 cornerDecoratedImageMap.put(baseImage, decoratedMap);
248 Image result = decoratedMap.get(decorator);
249 if (result ==
null) {
250 Rectangle bib = baseImage.getBounds();
251 Rectangle dib = decorator.getBounds();
253 result =
new Image(Display.getCurrent(), bib.width, bib.height);
255 GC gc =
new GC(result);
256 gc.drawImage(baseImage, 0, 0);
258 gc.drawImage(decorator, 0, 0);
260 gc.drawImage(decorator, bib.width - dib.width, 0);
262 gc.drawImage(decorator, 0, bib.height - dib.height);
264 gc.drawImage(decorator, bib.width - dib.width, bib.height - dib.height);
268 decoratedMap.put(decorator, result);
278 for (Image image : m_imageMap.values()) {
284 for (
int i = 0; i < m_decoratedImageMap.length; i++) {
285 Map<Image, Map<Image, Image>> cornerDecoratedImageMap = m_decoratedImageMap[i];
286 if (cornerDecoratedImageMap !=
null) {
287 for (Map<Image, Image> decoratedMap : cornerDecoratedImageMap.values()) {
288 for (Image image : decoratedMap.values()) {
291 decoratedMap.clear();
293 cornerDecoratedImageMap.clear();
305 private static Map<String, Font> m_fontMap =
new HashMap<String, Font>();
309 private static Map<Font, Font> m_fontToBoldFontMap =
new HashMap<Font, Font>();
321 public static Font
getFont(String name,
int height,
int style) {
322 return getFont(name, height, style,
false,
false);
340 public static Font
getFont(String name,
int size,
int style,
boolean strikeout,
boolean underline) {
341 String fontName = name +
'|' + size +
'|' + style +
'|' + strikeout +
'|' + underline;
342 Font font = m_fontMap.get(fontName);
344 FontData fontData =
new FontData(name, size, style);
345 if (strikeout || underline) {
347 Class<?> logFontClass = Class.forName(
"org.eclipse.swt.internal.win32.LOGFONT");
348 Object logFont = FontData.class.getField(
"data").get(fontData);
349 if (logFont !=
null && logFontClass !=
null) {
351 logFontClass.getField(
"lfStrikeOut").set(logFont, Byte.valueOf((
byte) 1));
354 logFontClass.getField(
"lfUnderline").set(logFont, Byte.valueOf((
byte) 1));
357 }
catch (Throwable e) {
358 System.err.println(
"Unable to set underline or strikeout" +
" (probably on a non-Windows platform). " + e);
361 font =
new Font(Display.getCurrent(), fontData);
362 m_fontMap.put(fontName, font);
374 Font font = m_fontToBoldFontMap.get(baseFont);
376 FontData fontDatas[] = baseFont.getFontData();
377 FontData data = fontDatas[0];
378 font =
new Font(Display.getCurrent(), data.getName(), data.getHeight(), SWT.BOLD);
379 m_fontToBoldFontMap.put(baseFont, font);
388 for (Font font : m_fontMap.values()) {
393 for (Font font : m_fontToBoldFontMap.values()) {
396 m_fontToBoldFontMap.clear();
406 private static Map<Integer, Cursor> m_idToCursorMap =
new HashMap<Integer, Cursor>();
415 Integer key = Integer.valueOf(
id);
416 Cursor cursor = m_idToCursorMap.get(key);
417 if (cursor ==
null) {
418 cursor =
new Cursor(Display.getDefault(),
id);
419 m_idToCursorMap.put(key, cursor);
427 for (Cursor cursor : m_idToCursorMap.values()) {
430 m_idToCursorMap.clear();
static final int TOP_RIGHT
static Font getFont(String name, int height, int style)
static Image decorateImage(Image baseImage, Image decorator)
static void disposeImages()
static Color getColor(int systemColorID)
static Image decorateImage(final Image baseImage, final Image decorator, final int corner)
static final int BOTTOM_LEFT
static Cursor getCursor(int id)
static Font getFont(String name, int size, int style, boolean strikeout, boolean underline)
static Image getImage(String path)
static final int LAST_CORNER_KEY
static Image getImage(Class<?> clazz, String path)
static void disposeFonts()
static void disposeColors()
static Color getColor(RGB rgb)
static void disposeCursors()
static final int BOTTOM_RIGHT
static Image getImage(InputStream stream)
static Font getBoldFont(Font baseFont)
static final int TOP_LEFT
static Color getColor(int r, int g, int b)