1 package com.quantum.preferences;
3 import com.quantum.Messages;
4 import com.quantum.PluginPreferences;
5 import com.quantum.QuantumPlugin;
6 import com.quantum.util.versioning.VersioningHelper;
8 import org.eclipse.jface.preference.ColorFieldEditor;
9 import org.eclipse.jface.preference.PreferenceConverter;
10 import org.eclipse.jface.preference.PreferencePage;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.events.SelectionEvent;
13 import org.eclipse.swt.events.SelectionListener;
14 import org.eclipse.swt.graphics.FontData;
15 import org.eclipse.swt.layout.GridData;
16 import org.eclipse.swt.layout.GridLayout;
17 import org.eclipse.swt.widgets.Button;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.FontDialog;
21 import org.eclipse.swt.widgets.Group;
22 import org.eclipse.swt.widgets.Label;
23 import org.eclipse.ui.IWorkbench;
24 import org.eclipse.ui.IWorkbenchPreferencePage;
28 * A preference page for font and colour preferences for the SQL
31 * @author Tom Schneider
33 public class SQLEditorPreferences extends PreferencePage
34 implements IWorkbenchPreferencePage {
36 private FontDialog fontDialog;
38 private ColorFieldEditor backgroundColorEditor;
39 private ColorFieldEditor textColorEditor;
40 private ColorFieldEditor keywordColorEditor;
41 private ColorFieldEditor stringColorEditor;
42 private ColorFieldEditor numericColorEditor;
43 private ColorFieldEditor commentColorEditor;
45 private boolean textFlag;
46 private boolean keywordFlag;
47 private boolean stringFlag;
48 private boolean numericFlag;
49 private boolean commentFlag;
51 private Button boldText;
52 private Button boldKeyword;
53 private Button boldString;
54 private Button boldNumeric;
55 private Button boldComment;
57 private FontData fontData;
58 private Label fontDisplay;
60 public void init(IWorkbench workbench) {
61 setPreferenceStore(QuantumPlugin.getDefault().getPreferenceStore());
64 protected void performDefaults() {
65 fontData = PluginPreferences.getDefaultFont();
67 this.textFlag = false;
68 this.keywordFlag = true;
69 this.stringFlag = false;
70 this.numericFlag = false;
71 this.commentFlag = false;
73 backgroundColorEditor.loadDefault();
74 textColorEditor.loadDefault();
75 keywordColorEditor.loadDefault();
76 stringColorEditor.loadDefault();
77 commentColorEditor.loadDefault();
78 numericColorEditor.loadDefault();
81 * Save the preferences to the preference store.
83 public boolean performOk() {
84 PreferenceConverter.setValue(getPreferenceStore(), "quantum.font", fontData); //$NON-NLS-1$
85 getPreferenceStore().setValue("quantum.text.bold", textFlag); //$NON-NLS-1$
86 getPreferenceStore().setValue("quantum.keyword.bold", keywordFlag); //$NON-NLS-1$
87 getPreferenceStore().setValue("quantum.string.bold", stringFlag); //$NON-NLS-1$
88 getPreferenceStore().setValue("quantum.comment.bold", commentFlag); //$NON-NLS-1$
89 getPreferenceStore().setValue("quantum.numeric.bold", numericFlag); //$NON-NLS-1$
90 backgroundColorEditor.store();
91 textColorEditor.store();
92 keywordColorEditor.store();
93 stringColorEditor.store();
94 commentColorEditor.store();
95 numericColorEditor.store();
96 return super.performOk();
99 protected Control createContents(Composite parent) {
100 Composite main = new Composite(parent, SWT.NULL);
101 main.setLayout(new GridLayout());
103 createFontSelectionArea(main);
104 Label label = new Label(main, SWT.NONE);
107 createColorSelectionArea(main);
115 private void createColorSelectionArea(Composite parent) {
116 Composite main = new Composite(parent, SWT.NULL);
118 this.textFlag = getPreferenceStore().getBoolean("quantum.text.bold"); //$NON-NLS-1$
119 this.keywordFlag = getPreferenceStore().getBoolean("quantum.keyword.bold"); //$NON-NLS-1$
120 this.stringFlag = getPreferenceStore().getBoolean("quantum.string.bold"); //$NON-NLS-1$
121 this.commentFlag = getPreferenceStore().getBoolean("quantum.comment.bold"); //$NON-NLS-1$
122 this.numericFlag = getPreferenceStore().getBoolean("quantum.numeric.bold"); //$NON-NLS-1$
125 GridLayout layout = new GridLayout();
127 // the colour preference chooser really, really wants to take up a whole
128 // row, so we need to pretend that our row is only two cells wide, then
129 // pull a switcheroo later...
130 layout.numColumns = 2;
131 main.setLayout(layout);
133 backgroundColorEditor =
134 new ColorFieldEditor(
135 PluginPreferences.BACKGROUND_COLOR,
136 Messages.getString(getClass(), "backgroundColor"), //$NON-NLS-1$
139 Label emptyLabel = new Label(main, SWT.NULL);
140 emptyLabel.setText("");
142 createEmptyRow(main);
144 this.textColorEditor =
145 new ColorFieldEditor(
146 PluginPreferences.TEXT_COLOR,
147 Messages.getString(getClass(), "defaultTextColor"), //$NON-NLS-1$
150 this.boldText = new Button(main, SWT.CHECK);
151 this.boldText.setSelection(textFlag);
152 this.boldText.setText(Messages.getString(getClass(), "bold")); //$NON-NLS-1$
153 this.boldText.addSelectionListener(new SelectionListener() {
154 public void widgetDefaultSelected(SelectionEvent e) {
156 public void widgetSelected(SelectionEvent e) {
157 textFlag = boldText.getSelection();
162 new ColorFieldEditor(
163 PluginPreferences.KEYWORD_COLOR,
164 Messages.getString(getClass(), "keywordTextColor"), //$NON-NLS-1$
167 boldKeyword = new Button(main, SWT.CHECK);
168 boldKeyword.setSelection(keywordFlag);
169 boldKeyword.setText(Messages.getString(getClass(), "bold")); //$NON-NLS-1$
170 boldKeyword.addSelectionListener(new SelectionListener() {
171 public void widgetDefaultSelected(SelectionEvent e) {
173 public void widgetSelected(SelectionEvent e) {
174 keywordFlag = boldKeyword.getSelection();
179 new ColorFieldEditor(
180 PluginPreferences.COMMENT_COLOR,
181 Messages.getString(getClass(), "commentTextColor"), //$NON-NLS-1$
184 boldComment = new Button(main, SWT.CHECK);
185 boldComment.setSelection(commentFlag);
186 boldComment.setText(Messages.getString(getClass(), "bold")); //$NON-NLS-1$
187 boldComment.addSelectionListener(new SelectionListener() {
188 public void widgetDefaultSelected(SelectionEvent e) {
190 public void widgetSelected(SelectionEvent e) {
191 commentFlag = boldComment.getSelection();
196 new ColorFieldEditor(
197 PluginPreferences.STRING_COLOR,
198 Messages.getString(getClass(), "stringTextColor"), //$NON-NLS-1$
201 boldString = new Button(main, SWT.CHECK);
202 boldString.setSelection(stringFlag);
203 boldString.setText(Messages.getString(getClass(), "bold")); //$NON-NLS-1$
204 boldString.addSelectionListener(new SelectionListener() {
205 public void widgetDefaultSelected(SelectionEvent e) {
207 public void widgetSelected(SelectionEvent e) {
208 stringFlag = boldString.getSelection();
213 new ColorFieldEditor(
214 PluginPreferences.NUMERIC_COLOR,
215 Messages.getString(getClass(), "numericTextColor"), //$NON-NLS-1$
218 boldNumeric = new Button(main, SWT.CHECK);
219 boldNumeric.setSelection(numericFlag);
220 boldNumeric.setText(Messages.getString(getClass(), "bold")); //$NON-NLS-1$
221 boldNumeric.addSelectionListener(new SelectionListener() {
222 public void widgetDefaultSelected(SelectionEvent e) {
224 public void widgetSelected(SelectionEvent e) {
225 numericFlag = boldNumeric.getSelection();
229 backgroundColorEditor.setPreferencePage(this);
230 backgroundColorEditor.setPreferenceStore(getPreferenceStore());
231 backgroundColorEditor.load();
233 textColorEditor.setPreferencePage(this);
234 textColorEditor.setPreferenceStore(getPreferenceStore());
235 textColorEditor.load();
237 keywordColorEditor.setPreferencePage(this);
238 keywordColorEditor.setPreferenceStore(getPreferenceStore());
239 keywordColorEditor.load();
241 commentColorEditor.setPreferencePage(this);
242 commentColorEditor.setPreferenceStore(getPreferenceStore());
243 commentColorEditor.load();
245 stringColorEditor.setPreferencePage(this);
246 stringColorEditor.setPreferenceStore(getPreferenceStore());
247 stringColorEditor.load();
249 numericColorEditor.setPreferencePage(this);
250 numericColorEditor.setPreferenceStore(getPreferenceStore());
251 numericColorEditor.load();
253 // now for the switcheroo...
254 // reset the number of columns to 3
255 GridLayout innerLayout = new GridLayout();
256 innerLayout.numColumns = 3;
257 main.setLayout(innerLayout);
263 private void createFontSelectionArea(Composite main) {
264 Group group = new Group(main, SWT.NONE);
265 group.setText(Messages.getString(getClass(), "font"));
266 GridLayout innerLayout = new GridLayout();
267 innerLayout.numColumns = 2;
268 group.setLayout(innerLayout);
269 group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
271 fontDisplay = new Label(group, SWT.NULL);
272 GridData data = new GridData(GridData.FILL_HORIZONTAL);
273 data.horizontalSpan = 2;
274 fontDisplay.setLayoutData(data);
276 fontData = PreferenceConverter.getFontData(getPreferenceStore(), "quantum.font"); //$NON-NLS-1$
277 fontDialog = new FontDialog(getShell());
278 Button fontButton = new Button(group, SWT.PUSH);
279 fontButton.setText(Messages.getString(getClass(), "pickFont")); //$NON-NLS-1$
280 fontButton.addSelectionListener(new SelectionListener() {
281 public void widgetDefaultSelected(SelectionEvent e) {
283 public void widgetSelected(SelectionEvent e) {
284 if (fontData != null) {
285 VersioningHelper.setFont(fontDialog, new FontData[] { fontData} );
287 FontData data = fontDialog.open();
294 Button defaultButton = new Button(group, SWT.PUSH);
295 defaultButton.setText(Messages.getString(getClass(), "defaultFont")); //$NON-NLS-1$
296 defaultButton.addSelectionListener(new SelectionListener() {
297 public void widgetDefaultSelected(SelectionEvent e) {
299 public void widgetSelected(SelectionEvent e) {
300 fontData = PluginPreferences.getDefaultFont();
311 private void createEmptyRow(Composite main) {
312 Label emptyLabel = new Label(main, SWT.NULL);
313 emptyLabel.setText("");
314 GridData gridData = new GridData();
315 gridData.horizontalSpan = 3;
316 emptyLabel.setLayoutData(gridData);
319 protected void updateFontDisplay() {
320 if (fontData == null) {
321 fontDisplay.setText(Messages.getString(getClass(), "default")); //$NON-NLS-1$
323 Object[] parameters = new Object[] {
324 fontData.getName(), new Integer(fontData.getHeight()) };
325 String style = Messages.getString(getClass(), "regularFont", parameters); //$NON-NLS-1$
326 if (fontData.getStyle() == SWT.BOLD) {
327 style = Messages.getString(getClass(), "boldFont", parameters); //$NON-NLS-1$
328 } else if (fontData.getStyle() == SWT.ITALIC) {
329 style = Messages.getString(getClass(), "italicFont", parameters); //$NON-NLS-1$
330 } else if (fontData.getStyle() == (SWT.BOLD | SWT.ITALIC)) {
331 style = Messages.getString(getClass(), "boldItalicFont", parameters); //$NON-NLS-1$
333 fontDisplay.setText(style); //$NON-NLS-1$
336 protected void updateFlags() {
337 this.boldText.setSelection(this.textFlag);
338 this.boldKeyword.setSelection(this.keywordFlag);
339 this.boldString.setSelection(this.stringFlag);
340 this.boldNumeric.setSelection(this.numericFlag);
341 this.boldComment.setSelection(this.commentFlag);