Skip to main content
summaryrefslogtreecommitdiffstats
blob: e319167242349ffe9f820c204e9db81240496527 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**********************************************************************
Copyright (c) 2000, 2002 IBM Corp. and others.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Common Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/cpl-v10.html

Contributors:
    IBM Corporation - Initial implementation
**********************************************************************/

package org.eclipse.jface.text;


import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;

import org.eclipse.jface.preference.FontFieldEditor;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;


/**
 * This font field editor implements chaining between a source preference
 * store and a target preference store. Any time the source preference
 * store changes, the change is propagated to the target store. Propagation
 * means that the actual value stored in the source store is set as default
 * value in the target store. If the target store does not contain a value 
 * other than the default value, the new default value is immediately 
 * effective.
 * 
 * @see FontFieldEditor 
 * @since 2.0
 */
public class PropagatingFontFieldEditor extends FontFieldEditor {
	
	/** The editor's parent widget */
	private Composite fParent;
	/** The representation of the default font choice */
	private String fDefaultFontLabel;
	
	/**
	 * Creates a new font field editor with the given parameters.
	 * 
	 * @param name the editor's name
	 * @param labelText the text shown as editor description
	 * @param parent the editor's parent widget
	 * @param defaultFontLabel the label shown in the editor value field when the default value should be taken
	 */
	public PropagatingFontFieldEditor(String name, String labelText, Composite parent, String defaultFontLabel) {
		super(name, labelText, parent);
		fParent= parent;
		fDefaultFontLabel= defaultFontLabel == null ? "" : defaultFontLabel; //$NON-NLS-1$
	}
	
	/*
	 * @see FontFieldEditor#doLoad()
	 */
	protected void doLoad() {
		if (getPreferenceStore().isDefault(getPreferenceName()))
			loadDefault();
		super.doLoad();
		checkForDefault();
	}
	
	/*
	 * @see FontFieldEditor#doLoadDefault()
	 */
	protected void doLoadDefault() {
		super.doLoadDefault();
		checkForDefault();
	}
	
	/**
	 * Checks whether this editor presents the default value "inheritated"
	 * from the workbench rather than its own font.
	 */
	private void checkForDefault() {
		if (presentsDefaultValue()) {
			Control c= getValueControl(fParent);
			if (c instanceof Label)
				((Label) c).setText(fDefaultFontLabel);
		}
	}
	
	/**
	 * Propagates the font set in the source store to the
	 * target store using the given keys.
	 * 
	 * @param source the store from which to read the text font
	 * @param sourceKey the key under which the font can be found
	 * @param target the store to which to propagate the font
	 * @param targetKey the key under which to store the font
	 */
	private static void propagateFont(IPreferenceStore source, String sourceKey, IPreferenceStore target, String targetKey) {
		FontData fd= PreferenceConverter.getFontData(source, sourceKey);
		if (fd != null) {
			boolean isDefault= target.isDefault(targetKey);	// save old state!
			PreferenceConverter.setDefault(target, targetKey, fd);
			if (isDefault) {			
				// restore old state
				target.setToDefault(targetKey);
			}
		}
	}
	
	/**
	 * Starts the propagation of the font preference stored in the source preference
	 * store under the source key to the target preference store using the target 
	 * preference key.
	 * 
	 * @param source the source preference store
	 * @param sourceKey the key to be used in the source preference store
	 * @param target the target preference store
	 * @param targetKey the key to be used in the target preference store
	 */
	public static void startPropagate(final IPreferenceStore source, final String sourceKey, final IPreferenceStore target, final String targetKey) {
		source.addPropertyChangeListener(new IPropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent event) {
				if (sourceKey.equals(event.getProperty()))						
					propagateFont(source, sourceKey, target, targetKey);
			}
		});
		
		propagateFont(source, sourceKey, target, targetKey);
	}
}

Back to the top