KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > PropagatingFontFieldEditor


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  *******************************************************************************/

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 /**
28  * This font field editor implements chaining between a source preference
29  * store and a target preference store. Any time the source preference
30  * store changes, the change is propagated to the target store. Propagation
31  * means that the actual value stored in the source store is set as default
32  * value in the target store. If the target store does not contain a value
33  * other than the default value, the new default value is immediately
34  * effective.
35  *
36  * @see FontFieldEditor
37  * @since 2.0
38  * @deprecated since 3.0 not longer in use, no longer supported
39  */

40 public class PropagatingFontFieldEditor extends FontFieldEditor {
41
42     /** The editor's parent widget */
43     private Composite fParent;
44     /** The representation of the default font choice */
45     private String JavaDoc fDefaultFontLabel;
46
47     /**
48      * Creates a new font field editor with the given parameters.
49      *
50      * @param name the editor's name
51      * @param labelText the text shown as editor description
52      * @param parent the editor's parent widget
53      * @param defaultFontLabel the label shown in the editor value field when the default value should be taken
54      */

55     public PropagatingFontFieldEditor(String JavaDoc name, String JavaDoc labelText, Composite parent, String JavaDoc defaultFontLabel) {
56         super(name, labelText, parent);
57         fParent= parent;
58         fDefaultFontLabel= defaultFontLabel == null ? "" : defaultFontLabel; //$NON-NLS-1$
59
}
60
61     /*
62      * @see FontFieldEditor#doLoad()
63      */

64     protected void doLoad() {
65         if (getPreferenceStore().isDefault(getPreferenceName()))
66             loadDefault();
67         super.doLoad();
68         checkForDefault();
69     }
70
71     /*
72      * @see FontFieldEditor#doLoadDefault()
73      */

74     protected void doLoadDefault() {
75         super.doLoadDefault();
76         checkForDefault();
77     }
78
79     /**
80      * Checks whether this editor presents the default value "inherited"
81      * from the workbench rather than its own font.
82      */

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     /**
92      * Propagates the font set in the source store to the
93      * target store using the given keys.
94      *
95      * @param source the store from which to read the text font
96      * @param sourceKey the key under which the font can be found
97      * @param target the store to which to propagate the font
98      * @param targetKey the key under which to store the font
99      */

100     private static void propagateFont(IPreferenceStore source, String JavaDoc sourceKey, IPreferenceStore target, String JavaDoc targetKey) {
101         FontData fd= PreferenceConverter.getFontData(source, sourceKey);
102         if (fd != null) {
103             boolean isDefault= target.isDefault(targetKey); // save old state!
104
PreferenceConverter.setDefault(target, targetKey, fd);
105             if (isDefault) {
106                 // restore old state
107
target.setToDefault(targetKey);
108             }
109         }
110     }
111
112     /**
113      * Starts the propagation of the font preference stored in the source preference
114      * store under the source key to the target preference store using the target
115      * preference key.
116      *
117      * @param source the source preference store
118      * @param sourceKey the key to be used in the source preference store
119      * @param target the target preference store
120      * @param targetKey the key to be used in the target preference store
121      */

122     public static void startPropagate(final IPreferenceStore source, final String JavaDoc sourceKey, final IPreferenceStore target, final String JavaDoc 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