KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > preference > FontFieldEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.jface.preference;
12
13 import org.eclipse.jface.dialogs.IDialogConstants;
14 import org.eclipse.jface.resource.JFaceResources;
15 import org.eclipse.jface.resource.StringConverter;
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.DisposeEvent;
19 import org.eclipse.swt.events.DisposeListener;
20 import org.eclipse.swt.events.SelectionAdapter;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.graphics.Font;
23 import org.eclipse.swt.graphics.FontData;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.FontDialog;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Text;
31
32 /**
33  * A field editor for a font type preference.
34  */

35 public class FontFieldEditor extends FieldEditor {
36
37     /**
38      * The change font button, or <code>null</code> if none
39      * (before creation and after disposal).
40      */

41     private Button changeFontButton = null;
42
43     /**
44      * The text for the change font button, or <code>null</code>
45      * if missing.
46      */

47     private String JavaDoc changeButtonText;
48
49     /**
50      * The text for the preview, or <code>null</code> if no preview is desired
51      */

52     private String JavaDoc previewText;
53
54     /**
55      * Font data for the chosen font button, or <code>null</code> if none.
56      */

57     private FontData[] chosenFont;
58
59     /**
60      * The label that displays the selected font, or <code>null</code> if none.
61      */

62     private Label valueControl;
63
64     /**
65      * The previewer, or <code>null</code> if none.
66      */

67     private DefaultPreviewer previewer;
68
69     /**
70      * Internal font previewer implementation.
71      */

72     private static class DefaultPreviewer {
73         private Text text;
74
75         private String JavaDoc string;
76
77         private Font font;
78
79         /**
80          * Constructor for the previewer.
81          * @param s
82          * @param parent
83          */

84         public DefaultPreviewer(String JavaDoc s, Composite parent) {
85             string = s;
86             text = new Text(parent, SWT.READ_ONLY | SWT.BORDER);
87             text.addDisposeListener(new DisposeListener() {
88                 public void widgetDisposed(DisposeEvent e) {
89                     if (font != null) {
90                         font.dispose();
91                     }
92                 }
93             });
94             if (string != null) {
95                 text.setText(string);
96             }
97         }
98
99         /**
100          * @return the control the previewer is using
101          */

102         public Control getControl() {
103             return text;
104         }
105
106         /**
107          * Set the font to display with
108          * @param fontData
109          */

110         public void setFont(FontData[] fontData) {
111             if (font != null) {
112                 font.dispose();
113             }
114             font = new Font(text.getDisplay(), fontData);
115             text.setFont(font);
116         }
117
118         /**
119          * @return the preferred size of the previewer.
120          */

121         public int getPreferredExtent() {
122             return 40;
123         }
124     }
125
126     /**
127      * Creates a new font field editor
128      */

129     protected FontFieldEditor() {
130     }
131
132     /**
133      * Creates a font field editor with an optional preview area.
134      *
135      * @param name the name of the preference this field editor works on
136      * @param labelText the label text of the field editor
137      * @param previewAreaText the text used for the preview window. If it is
138      * <code>null</code> there will be no preview area,
139      * @param parent the parent of the field editor's control
140      */

141     public FontFieldEditor(String JavaDoc name, String JavaDoc labelText,
142             String JavaDoc previewAreaText, Composite parent) {
143         init(name, labelText);
144         previewText = previewAreaText;
145         changeButtonText = JFaceResources.getString("openChange"); //$NON-NLS-1$
146
createControl(parent);
147
148     }
149
150     /**
151      * Creates a font field editor without a preview.
152      *
153      * @param name the name of the preference this field editor works on
154      * @param labelText the label text of the field editor
155      * @param parent the parent of the field editor's control
156      */

157     public FontFieldEditor(String JavaDoc name, String JavaDoc labelText, Composite parent) {
158         this(name, labelText, null, parent);
159
160     }
161
162     /* (non-Javadoc)
163      * Method declared on FieldEditor.
164      */

165     protected void adjustForNumColumns(int numColumns) {
166
167         GridData data = new GridData();
168         if (valueControl.getLayoutData() != null) {
169             data = (GridData) valueControl.getLayoutData();
170         }
171
172         data.horizontalSpan = numColumns - getNumberOfControls() + 1;
173         valueControl.setLayoutData(data);
174     }
175
176     /* (non-Javadoc)
177      * Method declared on FieldEditor.
178      */

179     protected void applyFont() {
180         if (chosenFont != null && previewer != null) {
181             previewer.setFont(chosenFont);
182         }
183     }
184
185     /* (non-Javadoc)
186      * Method declared on FieldEditor.
187      */

188     protected void doFillIntoGrid(Composite parent, int numColumns) {
189         getLabelControl(parent);
190
191         valueControl = getValueControl(parent);
192
193         GridData gd = new GridData(GridData.FILL_HORIZONTAL
194                 | GridData.GRAB_HORIZONTAL);
195         gd.horizontalSpan = numColumns - getNumberOfControls() + 1;
196         valueControl.setLayoutData(gd);
197         if (previewText != null) {
198             previewer = new DefaultPreviewer(previewText, parent);
199             gd = new GridData(GridData.FILL_HORIZONTAL);
200             gd.heightHint = previewer.getPreferredExtent();
201             gd.widthHint = previewer.getPreferredExtent();
202             previewer.getControl().setLayoutData(gd);
203         }
204
205         changeFontButton = getChangeControl(parent);
206         gd = new GridData();
207         int widthHint = convertHorizontalDLUsToPixels(changeFontButton,
208                 IDialogConstants.BUTTON_WIDTH);
209         gd.widthHint = Math.max(widthHint, changeFontButton.computeSize(
210                 SWT.DEFAULT, SWT.DEFAULT, true).x);
211         changeFontButton.setLayoutData(gd);
212
213     }
214
215     /* (non-Javadoc)
216      * Method declared on FieldEditor.
217      */

218     protected void doLoad() {
219         if (changeFontButton == null) {
220             return;
221         }
222         updateFont(PreferenceConverter.getFontDataArray(getPreferenceStore(),
223                 getPreferenceName()));
224     }
225
226     /* (non-Javadoc)
227      * Method declared on FieldEditor.
228      */

229     protected void doLoadDefault() {
230         if (changeFontButton == null) {
231             return;
232         }
233         updateFont(PreferenceConverter.getDefaultFontDataArray(
234                 getPreferenceStore(), getPreferenceName()));
235     }
236
237     /* (non-Javadoc)
238      * Method declared on FieldEditor.
239      */

240     protected void doStore() {
241         if (chosenFont != null) {
242             PreferenceConverter.setValue(getPreferenceStore(),
243                     getPreferenceName(), chosenFont);
244         }
245     }
246
247     /**
248      * Returns the change button for this field editor.
249      *
250      * @param parent The Composite to create the button in if required.
251      * @return the change button
252      */

253     protected Button getChangeControl(Composite parent) {
254         if (changeFontButton == null) {
255             changeFontButton = new Button(parent, SWT.PUSH);
256             if (changeButtonText != null) {
257                 changeFontButton.setText(changeButtonText);
258             }
259             changeFontButton.addSelectionListener(new SelectionAdapter() {
260                 public void widgetSelected(SelectionEvent event) {
261                     FontDialog fontDialog = new FontDialog(changeFontButton
262                             .getShell());
263                     if (chosenFont != null) {
264                         fontDialog.setFontList(chosenFont);
265                     }
266                     FontData font = fontDialog.open();
267                     if (font != null) {
268                         FontData[] oldFont = chosenFont;
269                         if (oldFont == null) {
270                             oldFont = JFaceResources.getDefaultFont()
271                                     .getFontData();
272                         }
273                         setPresentsDefaultValue(false);
274                         FontData[] newData = new FontData[1];
275                         newData[0] = font;
276                         updateFont(newData);
277                         fireValueChanged(VALUE, oldFont[0], font);
278                     }
279
280                 }
281             });
282             changeFontButton.addDisposeListener(new DisposeListener() {
283                 public void widgetDisposed(DisposeEvent event) {
284                     changeFontButton = null;
285                 }
286             });
287             changeFontButton.setFont(parent.getFont());
288             setButtonLayoutData(changeFontButton);
289         } else {
290             checkParent(changeFontButton, parent);
291         }
292         return changeFontButton;
293     }
294
295     /* (non-Javadoc)
296      * Method declared on FieldEditor.
297      */

298     public int getNumberOfControls() {
299         if (previewer == null) {
300             return 3;
301         }
302         
303         return 4;
304     }
305
306     /**
307      * Returns the preferred preview height.
308      *
309      * @return the height, or <code>-1</code> if no previewer
310      * is installed
311      */

312     public int getPreferredPreviewHeight() {
313         if (previewer == null) {
314             return -1;
315         }
316         return previewer.getPreferredExtent();
317     }
318
319     /**
320      * Returns the preview control for this field editor.
321      *
322      * @return the preview control
323      */

324     public Control getPreviewControl() {
325         if (previewer == null) {
326             return null;
327         }
328
329         return previewer.getControl();
330     }
331
332     /**
333      * Returns the value control for this field editor. The value control
334      * displays the currently selected font name.
335      * @param parent The Composite to create the viewer in if required
336      * @return the value control
337      */

338     protected Label getValueControl(Composite parent) {
339         if (valueControl == null) {
340             valueControl = new Label(parent, SWT.LEFT);
341             valueControl.setFont(parent.getFont());
342             valueControl.addDisposeListener(new DisposeListener() {
343                 public void widgetDisposed(DisposeEvent event) {
344                     valueControl = null;
345                 }
346             });
347         } else {
348             checkParent(valueControl, parent);
349         }
350         return valueControl;
351     }
352
353     /**
354      * Sets the text of the change button.
355      *
356      * @param text the new text
357      */

358     public void setChangeButtonText(String JavaDoc text) {
359         Assert.isNotNull(text);
360         changeButtonText = text;
361         if (changeFontButton != null) {
362             changeFontButton.setText(text);
363         }
364     }
365
366     /**
367      * Updates the change font button and the previewer to reflect the
368      * newly selected font.
369      * @param font The FontData[] to update with.
370      */

371     private void updateFont(FontData font[]) {
372         FontData[] bestFont = JFaceResources.getFontRegistry().filterData(
373                 font, valueControl.getDisplay());
374
375         //if we have nothing valid do as best we can
376
if (bestFont == null) {
377             bestFont = getDefaultFontData();
378         }
379
380         //Now cache this value in the receiver
381
this.chosenFont = bestFont;
382
383         if (valueControl != null) {
384             valueControl.setText(StringConverter.asString(chosenFont[0]));
385         }
386         if (previewer != null) {
387             previewer.setFont(bestFont);
388         }
389     }
390
391     /**
392      * Store the default preference for the field
393      * being edited
394      */

395     protected void setToDefault() {
396         FontData[] defaultFontData = PreferenceConverter
397                 .getDefaultFontDataArray(getPreferenceStore(),
398                         getPreferenceName());
399         PreferenceConverter.setValue(getPreferenceStore(), getPreferenceName(),
400                 defaultFontData);
401     }
402
403     /**
404      * Get the system default font data.
405      * @return FontData[]
406      */

407     private FontData[] getDefaultFontData() {
408         return valueControl.getDisplay().getSystemFont().getFontData();
409     }
410
411     /*
412      * @see FieldEditor.setEnabled(boolean,Composite).
413      */

414     public void setEnabled(boolean enabled, Composite parent) {
415         super.setEnabled(enabled, parent);
416         getChangeControl(parent).setEnabled(enabled);
417         getValueControl(parent).setEnabled(enabled);
418     }
419
420 }
421
Popular Tags