1 11 12 package org.eclipse.jface.text; 13 14 15 import org.eclipse.swt.graphics.FontData; 16 import org.eclipse.swt.widgets.Composite; 17 import org.eclipse.swt.widgets.Control; 18 import org.eclipse.swt.widgets.Label; 19 20 import org.eclipse.jface.preference.FontFieldEditor; 21 import org.eclipse.jface.preference.IPreferenceStore; 22 import org.eclipse.jface.preference.PreferenceConverter; 23 import org.eclipse.jface.util.IPropertyChangeListener; 24 import org.eclipse.jface.util.PropertyChangeEvent; 25 26 27 40 public class PropagatingFontFieldEditor extends FontFieldEditor { 41 42 43 private Composite fParent; 44 45 private String fDefaultFontLabel; 46 47 55 public PropagatingFontFieldEditor(String name, String labelText, Composite parent, String defaultFontLabel) { 56 super(name, labelText, parent); 57 fParent= parent; 58 fDefaultFontLabel= defaultFontLabel == null ? "" : defaultFontLabel; } 60 61 64 protected void doLoad() { 65 if (getPreferenceStore().isDefault(getPreferenceName())) 66 loadDefault(); 67 super.doLoad(); 68 checkForDefault(); 69 } 70 71 74 protected void doLoadDefault() { 75 super.doLoadDefault(); 76 checkForDefault(); 77 } 78 79 83 private void checkForDefault() { 84 if (presentsDefaultValue()) { 85 Control c= getValueControl(fParent); 86 if (c instanceof Label) 87 ((Label) c).setText(fDefaultFontLabel); 88 } 89 } 90 91 100 private static void propagateFont(IPreferenceStore source, String sourceKey, IPreferenceStore target, String targetKey) { 101 FontData fd= PreferenceConverter.getFontData(source, sourceKey); 102 if (fd != null) { 103 boolean isDefault= target.isDefault(targetKey); PreferenceConverter.setDefault(target, targetKey, fd); 105 if (isDefault) { 106 target.setToDefault(targetKey); 108 } 109 } 110 } 111 112 122 public static void startPropagate(final IPreferenceStore source, final String sourceKey, final IPreferenceStore target, final String targetKey) { 123 source.addPropertyChangeListener(new IPropertyChangeListener() { 124 public void propertyChange(PropertyChangeEvent event) { 125 if (sourceKey.equals(event.getProperty())) 126 propagateFont(source, sourceKey, target, targetKey); 127 } 128 }); 129 130 propagateFont(source, sourceKey, target, targetKey); 131 } 132 } 133 134 | Popular Tags |