|
1 /******************************************************************************* |
|
2 * Copyright (c) 2004, 2008 Tasktop Technologies and others. |
|
3 * All rights reserved. This program and the accompanying materials |
|
4 * are made available under the terms of the Eclipse Public License v1.0 |
|
5 * which accompanies this distribution, and is available at |
|
6 * http://www.eclipse.org/legal/epl-v10.html |
|
7 * |
|
8 * Contributors: |
|
9 * Tasktop Technologies - initial API and implementation |
|
10 *******************************************************************************/ |
|
11 |
|
12 package com.nokia.carbide.remoteconnections.internal.ui.mylyn; |
|
13 |
|
14 import java.lang.reflect.Field; |
|
15 |
|
16 import org.eclipse.jface.resource.FontRegistry; |
|
17 import org.eclipse.jface.resource.JFaceResources; |
|
18 import org.eclipse.swt.SWT; |
|
19 import org.eclipse.swt.graphics.Font; |
|
20 import org.eclipse.swt.graphics.FontData; |
|
21 import org.eclipse.swt.widgets.Display; |
|
22 |
|
23 /** |
|
24 * @author Mik Kersten |
|
25 * @since 3.0 |
|
26 */ |
|
27 public class CommonFonts { |
|
28 |
|
29 public static Font BOLD; |
|
30 |
|
31 public static Font ITALIC; |
|
32 |
|
33 public static Font BOLD_ITALIC; |
|
34 |
|
35 public static Font STRIKETHROUGH = null; |
|
36 |
|
37 public static boolean HAS_STRIKETHROUGH; |
|
38 |
|
39 static { |
|
40 if (Display.getCurrent() != null) { |
|
41 init(); |
|
42 } else { |
|
43 Display.getDefault().asyncExec(new Runnable() { |
|
44 public void run() { |
|
45 init(); |
|
46 } |
|
47 }); |
|
48 } |
|
49 } |
|
50 |
|
51 private static void init() { |
|
52 BOLD = JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT); |
|
53 ITALIC = JFaceResources.getFontRegistry().getItalic(JFaceResources.DEFAULT_FONT); |
|
54 BOLD_ITALIC = new Font(Display.getCurrent(), getModifiedFontData(ITALIC.getFontData(), SWT.BOLD | SWT.ITALIC)); |
|
55 |
|
56 Font defaultFont = JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT); |
|
57 FontData[] defaultData = defaultFont.getFontData(); |
|
58 if (defaultData != null && defaultData.length == 1) { |
|
59 FontData data = new FontData(defaultData[0].getName(), defaultData[0].getHeight(), |
|
60 defaultData[0].getStyle()); |
|
61 |
|
62 if ("win32".equals(SWT.getPlatform())) { //$NON-NLS-1$ |
|
63 // NOTE: Windows only, for: data.data.lfStrikeOut = 1; |
|
64 try { |
|
65 Field dataField = data.getClass().getDeclaredField("data"); //$NON-NLS-1$ |
|
66 Object dataObject = dataField.get(data); |
|
67 Class<?> clazz = dataObject.getClass().getSuperclass(); |
|
68 Field strikeOutFiled = clazz.getDeclaredField("lfStrikeOut"); //$NON-NLS-1$ |
|
69 strikeOutFiled.set(dataObject, (byte) 1); |
|
70 CommonFonts.STRIKETHROUGH = new Font(Display.getCurrent(), data); |
|
71 } catch (Throwable t) { |
|
72 // ignore |
|
73 } |
|
74 } |
|
75 } |
|
76 if (CommonFonts.STRIKETHROUGH == null) { |
|
77 CommonFonts.HAS_STRIKETHROUGH = false; |
|
78 CommonFonts.STRIKETHROUGH = defaultFont; |
|
79 } else { |
|
80 CommonFonts.HAS_STRIKETHROUGH = true; |
|
81 } |
|
82 } |
|
83 |
|
84 /** |
|
85 * NOTE: disposal of JFaceResources fonts handled by registry. |
|
86 */ |
|
87 public static void dispose() { |
|
88 if (CommonFonts.STRIKETHROUGH != null && !CommonFonts.STRIKETHROUGH.isDisposed()) { |
|
89 CommonFonts.STRIKETHROUGH.dispose(); |
|
90 CommonFonts.BOLD_ITALIC.dispose(); |
|
91 } |
|
92 } |
|
93 |
|
94 /** |
|
95 * Copied from {@link FontRegistry} |
|
96 */ |
|
97 private static FontData[] getModifiedFontData(FontData[] baseData, int style) { |
|
98 FontData[] styleData = new FontData[baseData.length]; |
|
99 for (int i = 0; i < styleData.length; i++) { |
|
100 FontData base = baseData[i]; |
|
101 styleData[i] = new FontData(base.getName(), base.getHeight(), base.getStyle() | style); |
|
102 } |
|
103 |
|
104 return styleData; |
|
105 } |
|
106 } |