Debian Codegraph @

plugins/org.eclipse.emf.edit.ui/src/org/eclipse/emf/edit/ui/provider/ExtendedFontRegistry.java

raw
1 /**
2 * Copyright (c) 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v2.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v20.html
7 *
8 * Contributors:
9 * IBM - Initial API and implementation
10 */
11 package org.eclipse.emf.edit.ui.provider;
12
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashMap;
17
18 import org.eclipse.emf.common.util.URI;
19 import org.eclipse.emf.edit.provider.IItemFontProvider;
20 import org.eclipse.emf.edit.ui.EMFEditUIPlugin;
21 import org.eclipse.jface.resource.DeviceResourceException;
22 import org.eclipse.jface.resource.FontDescriptor;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.graphics.Font;
25 import org.eclipse.swt.graphics.FontData;
26 import org.eclipse.swt.widgets.Display;
27
28
29 /**
30 * A font registry for turning a font description into an actual font object.
31 * @see IItemFontProvider
32 */
33 public class ExtendedFontRegistry
34 {
35 public static final ExtendedFontRegistry INSTANCE = new ExtendedFontRegistry();
36
37 protected Display display;
38 protected HashMap<Collection<?>, Font> table = new HashMap<Collection<?>, Font>(10);
39
40 public ExtendedFontRegistry()
41 {
42 display = Display.getCurrent();
43 hookDisplayDispose(display);
44 }
45
46 public ExtendedFontRegistry(Display display)
47 {
48 this.display = display;
49 hookDisplayDispose(display);
50 }
51
52 public Font getFont(Font baseFont, Object object)
53 {
54 if (object instanceof Font)
55 {
56 return (Font)object;
57 }
58 else
59 {
60 Collection<Object> key = new ArrayList<Object>(2);
61 key.add(baseFont);
62 key.add(object);
63
64 Font result = table.get(key);
65 if (result == null)
66 {
67 if (object instanceof FontDescriptor)
68 {
69 FontDescriptor fontDescriptor = (FontDescriptor)object;
70 try
71 {
72 result = fontDescriptor.createFont(display);
73 }
74 catch (DeviceResourceException exception)
75 {
76 EMFEditUIPlugin.INSTANCE.log(exception);
77 }
78 }
79 else if (object instanceof URI)
80 {
81 URI fontURI = (URI)object;
82 if (!"font".equals(fontURI.scheme()))
83 {
84 throw new IllegalArgumentException("Only 'font' scheme is recognized" + fontURI);
85 }
86 String fontNameSpecification = fontURI.authority();
87 if ("".equals(fontNameSpecification))
88 {
89 fontNameSpecification = null;
90 }
91 String heightSpecification = fontURI.segment(0);
92 boolean delta;
93 int height;
94 if (heightSpecification.startsWith("+"))
95 {
96 delta = true;
97 height = Integer.parseInt(heightSpecification.substring(1));
98 }
99 else if ("".equals(heightSpecification))
100 {
101 delta = true;
102 height = 0;
103 }
104 else
105 {
106 height = Integer.parseInt(heightSpecification);
107 delta = height < 0;
108 }
109
110 String styleSpecification = fontURI.segment(1);
111 int style =
112 "bold".equals(styleSpecification) ?
113 SWT.BOLD :
114 "italic".equals(styleSpecification) ?
115 SWT.ITALIC :
116 "italic+bold".equals(styleSpecification) || "bold+italic".equals(styleSpecification) ?
117 SWT.ITALIC | SWT.BOLD :
118 "normal".equals(styleSpecification) ?
119 SWT.NORMAL :
120 -1;
121
122 FontData [] baseFontData = baseFont.getFontData();
123 FontData [] fontData = new FontData[baseFontData.length];
124
125 for (int i = 0; i < baseFontData.length; ++i)
126 {
127 fontData[i] =
128 new FontData
129 (fontNameSpecification == null ? baseFontData[i].getName() : fontNameSpecification,
130 delta ? baseFontData[i].getHeight() + height : height,
131 style == -1 ? baseFontData[i].getStyle() : style);
132 }
133
134 try
135 {
136 result = FontDescriptor.createFrom(fontData).createFont(display);
137 }
138 catch (DeviceResourceException exception)
139 {
140 EMFEditUIPlugin.INSTANCE.log(exception);
141 }
142 }
143
144 if (result != null)
145 {
146 table.put(key, result);
147 }
148 }
149 return result;
150 }
151 }
152
153 protected void handleDisplayDispose()
154 {
155 for (Font image : table.values())
156 {
157 image.dispose();
158 }
159 table = null;
160 }
161
162 protected void hookDisplayDispose(Display display)
163 {
164 display.disposeExec
165 (new Runnable()
166 {
167 public void run()
168 {
169 handleDisplayDispose();
170 }
171 });
172 }
173 }