1 package org.simantics.wiki.ui.editor;
3 import org.eclipse.jface.resource.FontDescriptor;
4 import org.eclipse.jface.resource.FontRegistry;
5 import org.eclipse.jface.resource.ResourceManager;
6 import org.eclipse.jface.util.IPropertyChangeListener;
7 import org.eclipse.jface.util.PropertyChangeEvent;
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.graphics.Font;
10 import org.eclipse.swt.graphics.FontData;
11 import org.eclipse.swt.widgets.Control;
12 import org.eclipse.swt.widgets.Event;
13 import org.eclipse.swt.widgets.Listener;
14 import org.simantics.g2d.utils.FontHelper;
17 * @author Tuukka Lehtonen
19 public final class WikiFontUtil {
22 * ID for wiki source text font.
24 public static final String SOURCE_FONT_ID = "org.simantics.wiki.sourcefont";
27 * @param resourceManager a resource manager for handling font allocations
28 * @param control the control to attach the font to
29 * @param fontRegistryId registry id of font to use for control
31 public static void attachControlFontToRegistry(
32 final ResourceManager resourceManager,
33 final Control control,
34 String fontRegistryId)
36 final FontRegistry fontRegistry = FontHelper.getCurrentThemeFontRegistry();
37 final IPropertyChangeListener fontRegistryListener = new IPropertyChangeListener() {
39 public void propertyChange(PropertyChangeEvent event) {
40 FontData[] fdn = ((FontData[]) event.getNewValue());
41 FontDescriptor fd = FontDescriptor.createFrom(fdn);
42 control.setFont((Font) resourceManager.get(fd));
45 fontRegistry.addListener(fontRegistryListener);
46 FontDescriptor fd = fontRegistry.getDescriptor(fontRegistryId);
47 control.setFont((Font) resourceManager.get(fd));
48 control.addListener(SWT.Dispose, new Listener() {
50 public void handleEvent(Event event) {
51 fontRegistry.removeListener(fontRegistryListener);