]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.wiki.ui/src/org/simantics/wiki/ui/editor/WikiFontUtil.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.wiki.ui / src / org / simantics / wiki / ui / editor / WikiFontUtil.java
1 package org.simantics.wiki.ui.editor;
2
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;
15
16 /**
17  * @author Tuukka Lehtonen
18  */
19 public final class WikiFontUtil {
20
21     /**
22      * ID for wiki source text font.
23      */
24     public static final String SOURCE_FONT_ID = "org.simantics.wiki.sourcefont";
25
26     /**
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
30      */
31     public static void attachControlFontToRegistry(
32             final ResourceManager resourceManager,
33             final Control control,
34             String fontRegistryId)
35     {
36         final FontRegistry fontRegistry = FontHelper.getCurrentThemeFontRegistry();
37         final IPropertyChangeListener fontRegistryListener = new IPropertyChangeListener() {
38             @Override
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));
43             }
44         };
45         fontRegistry.addListener(fontRegistryListener);
46         FontDescriptor fd = fontRegistry.getDescriptor(fontRegistryId);
47         control.setFont((Font) resourceManager.get(fd));
48         control.addListener(SWT.Dispose, new Listener() {
49             @Override
50             public void handleEvent(Event event) {
51                 fontRegistry.removeListener(fontRegistryListener);
52             }
53         });
54     }
55
56 }