?? swtresourcemanager.java
字號:
* @param baseImage Image The base image that should be decorated
* @param decorator Image The image to decorate the base image
* @param corner The corner to place decorator image
* @return Image The resulting decorated image
*/
public static Image decorateImage(Image baseImage, Image decorator, int corner) {
HashMap decoratedMap = (HashMap) m_ImageToDecoratorMap.get(baseImage);
if (decoratedMap == null) {
decoratedMap = new HashMap();
m_ImageToDecoratorMap.put(baseImage, decoratedMap);
}
Image result = (Image) decoratedMap.get(decorator);
if (result == null) {
Rectangle bid = baseImage.getBounds();
Rectangle did = decorator.getBounds();
result = new Image(Display.getCurrent(), bid.width, bid.height);
GC gc = new GC(result);
gc.drawImage(baseImage, 0, 0);
//
if (corner == TOP_LEFT) {
gc.drawImage(decorator, 0, 0);
} else if (corner == TOP_RIGHT) {
gc.drawImage(decorator, bid.width - did.width - 1, 0);
} else if (corner == BOTTOM_LEFT) {
gc.drawImage(decorator, 0, bid.height - did.height - 1);
} else if (corner == BOTTOM_RIGHT) {
gc.drawImage(decorator, bid.width - did.width - 1, bid.height - did.height - 1);
}
//
gc.dispose();
decoratedMap.put(decorator, result);
}
return result;
}
/**
* Dispose all of the cached images
*/
public static void disposeImages() {
for (Iterator I = m_ClassImageMap.values().iterator(); I.hasNext();)
((Image) I.next()).dispose();
m_ClassImageMap.clear();
//
for (Iterator I = m_ImageToDecoratorMap.values().iterator(); I.hasNext();) {
HashMap decoratedMap = (HashMap) I.next();
for (Iterator J = decoratedMap.values().iterator(); J.hasNext();) {
Image image = (Image) J.next();
image.dispose();
}
}
}
/**
* Dispose cached images in specified section
* @param section the section do dispose
*/
public static void disposeImages(String section) {
for (Iterator I = m_ClassImageMap.keySet().iterator(); I.hasNext();) {
String key = (String) I.next();
if (!key.startsWith(section + "|"))
continue;
Image image = (Image) m_ClassImageMap.get(key);
image.dispose();
I.remove();
}
}
//////////////////////////////
// Font support
//////////////////////////////
/**
* Maps font names to fonts
*/
private static HashMap m_FontMap = new HashMap();
/**
* Maps fonts to their bold versions
*/
private static HashMap m_FontToBoldFontMap = new HashMap();
/**
* Returns a font based on its name, height and style
* @param name String The name of the font
* @param height int The height of the font
* @param style int The style of the font
* @return Font The font matching the name, height and style
*/
public static Font getFont(String name, int height, int style) {
return getFont(name, height, style, false, false);
}
/**
* Returns a font based on its name, height and style.
* Windows-specific strikeout and underline flags are also supported.
* @param name String The name of the font
* @param height int The height of the font
* @param style int The style of the font
* @param strikeout boolean The strikeout flag (warning: Windows only)
* @param underline boolean The underline flag (warning: Windows only)
* @return Font The font matching the name, height, style, strikeout and underline
*/
public static Font getFont(String name, int size, int style, boolean strikeout, boolean underline) {
String fontName = name + "|" + size + "|" + style + "|" + strikeout + "|" + underline;
Font font = (Font) m_FontMap.get(fontName);
if (font == null) {
FontData fontData = new FontData(name, size, style);
if (strikeout || underline) {
try {
Class logFontClass = Class.forName("org.eclipse.swt.internal.win32.LOGFONT");
Object logFont = FontData.class.getField("data").get(fontData);
if (logFont != null && logFontClass != null) {
if (strikeout) {
logFontClass.getField("lfStrikeOut").set(logFont, new Byte((byte) 1));
}
if (underline) {
logFontClass.getField("lfUnderline").set(logFont, new Byte((byte) 1));
}
}
} catch (Throwable e) {
System.err.println(
"Unable to set underline or strikeout" + " (probably on a non-Windows platform). " + e);
}
}
font = new Font(Display.getCurrent(), fontData);
m_FontMap.put(fontName, font);
}
return font;
}
/**
* Return a bold version of the give font
* @param baseFont Font The font for whoch a bold version is desired
* @return Font The bold version of the give font
*/
public static Font getBoldFont(Font baseFont) {
Font font = (Font) m_FontToBoldFontMap.get(baseFont);
if (font == null) {
FontData fontDatas[] = baseFont.getFontData();
FontData data = fontDatas[0];
font = new Font(Display.getCurrent(), data.getName(), data.getHeight(), SWT.BOLD);
m_FontToBoldFontMap.put(baseFont, font);
}
return font;
}
/**
* Dispose all of the cached fonts
*/
public static void disposeFonts() {
for (Iterator iter = m_FontMap.values().iterator(); iter.hasNext();)
((Font) iter.next()).dispose();
m_FontMap.clear();
}
//////////////////////////////
// CoolBar support
//////////////////////////////
/**
* Fix the layout of the specified CoolBar
* @param bar CoolBar The CoolBar that shgoud be fixed
*/
public static void fixCoolBarSize(CoolBar bar) {
CoolItem[] items = bar.getItems();
// ensure that each item has control (at least empty one)
for (int i = 0; i < items.length; i++) {
CoolItem item = items[i];
if (item.getControl() == null)
item.setControl(new Canvas(bar, SWT.NONE) {
public Point computeSize(int wHint, int hHint, boolean changed) {
return new Point(20, 20);
}
});
}
// compute size for each item
for (int i = 0; i < items.length; i++) {
CoolItem item = items[i];
Control control = item.getControl();
control.pack();
Point size = control.getSize();
item.setSize(item.computeSize(size.x, size.y));
}
}
//////////////////////////////
// Cursor support
//////////////////////////////
/**
* Maps IDs to cursors
*/
private static HashMap m_IdToCursorMap = new HashMap();
/**
* Returns the system cursor matching the specific ID
* @param id int The ID value for the cursor
* @return Cursor The system cursor matching the specific ID
*/
public static Cursor getCursor(int id) {
Integer key = new Integer(id);
Cursor cursor = (Cursor) m_IdToCursorMap.get(key);
if (cursor == null) {
cursor = new Cursor(Display.getDefault(), id);
m_IdToCursorMap.put(key, cursor);
}
return cursor;
}
/**
* Dispose all of the cached cursors
*/
public static void disposeCursors() {
for (Iterator iter = m_IdToCursorMap.values().iterator(); iter.hasNext();)
((Cursor) iter.next()).dispose();
m_IdToCursorMap.clear();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -