comparison dwtx/jface/text/PropagatingFontFieldEditor.d @ 129:eb30df5ca28b

Added JFace Text sources
author Frank Benoit <benoit@tionex.de>
date Sat, 23 Aug 2008 19:10:48 +0200
parents
children c4fb132a086c
comparison
equal deleted inserted replaced
128:8df1d4193877 129:eb30df5ca28b
1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 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 v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13
14 module dwtx.jface.text.PropagatingFontFieldEditor;
15
16 import dwt.dwthelper.utils;
17
18
19
20
21 import dwt.graphics.FontData;
22 import dwt.widgets.Composite;
23 import dwt.widgets.Control;
24 import dwt.widgets.Label;
25 import dwtx.jface.preference.FontFieldEditor;
26 import dwtx.jface.preference.IPreferenceStore;
27 import dwtx.jface.preference.PreferenceConverter;
28 import dwtx.jface.util.IPropertyChangeListener;
29 import dwtx.jface.util.PropertyChangeEvent;
30
31
32 /**
33 * This font field editor implements chaining between a source preference
34 * store and a target preference store. Any time the source preference
35 * store changes, the change is propagated to the target store. Propagation
36 * means that the actual value stored in the source store is set as default
37 * value in the target store. If the target store does not contain a value
38 * other than the default value, the new default value is immediately
39 * effective.
40 *
41 * @see FontFieldEditor
42 * @since 2.0
43 * @deprecated since 3.0 not longer in use, no longer supported
44 */
45 public class PropagatingFontFieldEditor : FontFieldEditor {
46
47 /** The editor's parent widget */
48 private Composite fParent;
49 /** The representation of the default font choice */
50 private String fDefaultFontLabel;
51
52 /**
53 * Creates a new font field editor with the given parameters.
54 *
55 * @param name the editor's name
56 * @param labelText the text shown as editor description
57 * @param parent the editor's parent widget
58 * @param defaultFontLabel the label shown in the editor value field when the default value should be taken
59 */
60 public PropagatingFontFieldEditor(String name, String labelText, Composite parent, String defaultFontLabel) {
61 super(name, labelText, parent);
62 fParent= parent;
63 fDefaultFontLabel= defaultFontLabel is null ? "" : defaultFontLabel; //$NON-NLS-1$
64 }
65
66 /*
67 * @see FontFieldEditor#doLoad()
68 */
69 protected void doLoad() {
70 if (getPreferenceStore().isDefault(getPreferenceName()))
71 loadDefault();
72 super.doLoad();
73 checkForDefault();
74 }
75
76 /*
77 * @see FontFieldEditor#doLoadDefault()
78 */
79 protected void doLoadDefault() {
80 super.doLoadDefault();
81 checkForDefault();
82 }
83
84 /**
85 * Checks whether this editor presents the default value "inherited"
86 * from the workbench rather than its own font.
87 */
88 private void checkForDefault() {
89 if (presentsDefaultValue()) {
90 Control c= getValueControl(fParent);
91 if (c instanceof Label)
92 ((Label) c).setText(fDefaultFontLabel);
93 }
94 }
95
96 /**
97 * Propagates the font set in the source store to the
98 * target store using the given keys.
99 *
100 * @param source the store from which to read the text font
101 * @param sourceKey the key under which the font can be found
102 * @param target the store to which to propagate the font
103 * @param targetKey the key under which to store the font
104 */
105 private static void propagateFont(IPreferenceStore source, String sourceKey, IPreferenceStore target, String targetKey) {
106 FontData fd= PreferenceConverter.getFontData(source, sourceKey);
107 if (fd !is null) {
108 bool isDefault= target.isDefault(targetKey); // save old state!
109 PreferenceConverter.setDefault(target, targetKey, fd);
110 if (isDefault) {
111 // restore old state
112 target.setToDefault(targetKey);
113 }
114 }
115 }
116
117 /**
118 * Starts the propagation of the font preference stored in the source preference
119 * store under the source key to the target preference store using the target
120 * preference key.
121 *
122 * @param source the source preference store
123 * @param sourceKey the key to be used in the source preference store
124 * @param target the target preference store
125 * @param targetKey the key to be used in the target preference store
126 */
127 public static void startPropagate(final IPreferenceStore source, final String sourceKey, final IPreferenceStore target, final String targetKey) {
128 source.addPropertyChangeListener(new IPropertyChangeListener() {
129 public void propertyChange(PropertyChangeEvent event) {
130 if (sourceKey.equals(event.getProperty()))
131 propagateFont(source, sourceKey, target, targetKey);
132 }
133 });
134
135 propagateFont(source, sourceKey, target, targetKey);
136 }
137 }
138