1 package com.quantum.preferences;
4 import com.quantum.Messages;
5 import com.quantum.QuantumPlugin;
6 import com.quantum.editors.ColorManager;
7 import com.quantum.editors.SQLColorConstants;
8 import com.quantum.util.versioning.VersioningHelper;
10 import org.eclipse.jface.preference.ColorFieldEditor;
11 import org.eclipse.jface.preference.IPreferenceStore;
12 import org.eclipse.jface.preference.PreferenceConverter;
13 import org.eclipse.jface.preference.PreferencePage;
14 import org.eclipse.jface.resource.JFaceResources;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.SelectionEvent;
17 import org.eclipse.swt.events.SelectionListener;
18 import org.eclipse.swt.graphics.FontData;
19 import org.eclipse.swt.graphics.RGB;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.FontDialog;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.ui.IWorkbench;
28 import org.eclipse.ui.IWorkbenchPreferencePage;
30 public class SQLEditorPreferences extends PreferencePage
31 implements IWorkbenchPreferencePage {
32 FontDialog fontDialog;
33 ColorFieldEditor backgroundColorEditor;
35 ColorFieldEditor textColorEditor;
37 ColorFieldEditor keywordColorEditor;
39 ColorFieldEditor stringColorEditor;
41 ColorFieldEditor numericColorEditor;
43 ColorFieldEditor commentColorEditor;
55 public void init(IWorkbench workbench) {
56 //Initialize the preference store
57 this.workbench = workbench;
58 setPreferenceStore(QuantumPlugin.getDefault().getPreferenceStore());
59 initializeColorDefaults(getPreferenceStore());
62 private void initializeColorDefaults(IPreferenceStore store) {
63 RGB BACKGROUND = new RGB(255, 255, 255);
64 RGB COMMENT = new RGB(88, 148, 64);
65 RGB KEYWORD = new RGB(126, 0, 75);
66 RGB STRING = new RGB(0, 0, 255);
67 RGB NUMERIC = new RGB(255, 0, 0);
68 RGB DEFAULT = new RGB(0, 0, 0);
69 PreferenceConverter.setDefault(store,
70 "quantum.background.color", BACKGROUND); //$NON-NLS-1$
71 PreferenceConverter.setDefault(store,
72 "quantum.text.color", DEFAULT); //$NON-NLS-1$
73 PreferenceConverter.setDefault(store,
74 "quantum.keyword.color", KEYWORD); //$NON-NLS-1$
75 PreferenceConverter.setDefault(store,
76 "quantum.comment.color", COMMENT); //$NON-NLS-1$
77 PreferenceConverter.setDefault(store,
78 "quantum.string.color", STRING); //$NON-NLS-1$
79 PreferenceConverter.setDefault(store,
80 "quantum.numeric.color", NUMERIC); //$NON-NLS-1$
83 protected void performDefaults() {
92 backgroundColorEditor.loadDefault();
93 textColorEditor.loadDefault();
94 keywordColorEditor.loadDefault();
95 stringColorEditor.loadDefault();
96 commentColorEditor.loadDefault();
97 numericColorEditor.loadDefault();
100 * Save the preferences to the preference store.
102 public boolean performOk() {
103 PreferenceConverter.setValue(getPreferenceStore(), "quantum.font", fontData); //$NON-NLS-1$
104 getPreferenceStore().setValue("quantum.text.bold", textFlag); //$NON-NLS-1$
105 getPreferenceStore().setValue("quantum.keyword.bold", keywordFlag); //$NON-NLS-1$
106 getPreferenceStore().setValue("quantum.string.bold", stringFlag); //$NON-NLS-1$
107 getPreferenceStore().setValue("quantum.comment.bold", commentFlag); //$NON-NLS-1$
108 getPreferenceStore().setValue("quantum.numeric.bold", numericFlag); //$NON-NLS-1$
109 backgroundColorEditor.store();
110 textColorEditor.store();
111 keywordColorEditor.store();
112 stringColorEditor.store();
113 commentColorEditor.store();
114 numericColorEditor.store();
115 return super.performOk();
117 protected Control createContents(Composite parent) {
118 Composite main = new Composite(parent, SWT.NULL);
120 GridLayout innerLayout = new GridLayout();
121 innerLayout.numColumns = 4;
122 main.setLayout(innerLayout);
124 fontData = PreferenceConverter.getFontData(getPreferenceStore(), "quantum.font"); //$NON-NLS-1$
125 textFlag = getPreferenceStore().getBoolean("quantum.text.bold"); //$NON-NLS-1$
126 keywordFlag = getPreferenceStore().getBoolean("quantum.keyword.bold"); //$NON-NLS-1$
127 stringFlag = getPreferenceStore().getBoolean("quantum.string.bold"); //$NON-NLS-1$
128 commentFlag = getPreferenceStore().getBoolean("quantum.comment.bold"); //$NON-NLS-1$
129 numericFlag = getPreferenceStore().getBoolean("quantum.numeric.bold"); //$NON-NLS-1$
131 fontDialog = new FontDialog(workbench.getActiveWorkbenchWindow().getShell());
132 Button fontButton = new Button(main, SWT.PUSH);
133 fontButton.setText(Messages.getString("PreferencesPage.PickFont")); //$NON-NLS-1$
134 fontButton.addSelectionListener(new SelectionListener() {
135 public void widgetDefaultSelected(SelectionEvent e) {
137 public void widgetSelected(SelectionEvent e) {
138 if (fontData != null) {
139 VersioningHelper.setFont(fontDialog, new FontData[] { fontData} );
141 FontData data = fontDialog.open();
148 Button defaultButton = new Button(main, SWT.PUSH);
149 defaultButton.setText(Messages.getString("PreferencesPage.DefaultFont")); //$NON-NLS-1$
150 defaultButton.addSelectionListener(new SelectionListener() {
151 public void widgetDefaultSelected(SelectionEvent e) {
153 public void widgetSelected(SelectionEvent e) {
154 FontData[] temp = JFaceResources.getTextFont().getFontData();
155 fontData = temp == null || temp.length == 0 ? null : temp[0];
160 fontDisplay = new Label(main, SWT.NULL);
161 GridData data = new GridData(GridData.FILL_HORIZONTAL);
162 data.grabExcessHorizontalSpace = true;
163 fontDisplay.setLayoutData(data);
166 ColorManager manager = new ColorManager();
168 Composite comp = new Composite(main, SWT.NULL);
169 GridData layoutData = new GridData();
170 layoutData.horizontalSpan = 2;
171 comp.setLayoutData(layoutData);
173 manager.getColor(SQLColorConstants.DEFAULT);
174 backgroundColorEditor =
175 new ColorFieldEditor(
176 "quantum.background.color", //$NON-NLS-1$
177 Messages.getString("PreferencesPage.BackgroundColor"), //$NON-NLS-1$
180 Composite temp = new Composite(main, SWT.NULL);
183 comp = new Composite(main, SWT.NULL);
184 layoutData = new GridData();
185 layoutData.horizontalSpan = 2;
186 comp.setLayoutData(layoutData);
189 new ColorFieldEditor(
190 "quantum.text.color", //$NON-NLS-1$
191 Messages.getString("PreferencesPage.DefaultTextColor"), //$NON-NLS-1$
194 boldText = new Button(main, SWT.CHECK);
195 boldText.setSelection(textFlag);
196 boldText.setText(Messages.getString("PreferencesPage.Bold")); //$NON-NLS-1$
197 boldText.addSelectionListener(new SelectionListener() {
198 public void widgetDefaultSelected(SelectionEvent e) {
200 public void widgetSelected(SelectionEvent e) {
201 textFlag = boldText.getSelection();
205 comp = new Composite(main, SWT.NULL);
206 layoutData = new GridData();
207 layoutData.horizontalSpan = 2;
208 comp.setLayoutData(layoutData);
211 new ColorFieldEditor(
212 Messages.getString("PreferencesPage.quantum.keyword.color_26"), //$NON-NLS-1$
213 Messages.getString("PreferencesPage.KeywordTextColor"), //$NON-NLS-1$
216 boldKeyword = new Button(main, SWT.CHECK);
217 boldKeyword.setSelection(keywordFlag);
218 boldKeyword.setText(Messages.getString("PreferencesPage.Bold")); //$NON-NLS-1$
219 boldKeyword.addSelectionListener(new SelectionListener() {
220 public void widgetDefaultSelected(SelectionEvent e) {
222 public void widgetSelected(SelectionEvent e) {
223 keywordFlag = boldKeyword.getSelection();
227 comp = new Composite(main, SWT.NULL);
228 layoutData = new GridData();
229 layoutData.horizontalSpan = 2;
230 comp.setLayoutData(layoutData);
233 new ColorFieldEditor(
234 "quantum.comment.color", //$NON-NLS-1$
235 Messages.getString("PreferencesPage.CommentTextColor"), //$NON-NLS-1$
238 boldComment = new Button(main, SWT.CHECK);
239 boldComment.setSelection(commentFlag);
240 boldComment.setText(Messages.getString("PreferencesPage.Bold")); //$NON-NLS-1$
241 boldComment.addSelectionListener(new SelectionListener() {
242 public void widgetDefaultSelected(SelectionEvent e) {
244 public void widgetSelected(SelectionEvent e) {
245 commentFlag = boldComment.getSelection();
249 comp = new Composite(main, SWT.NULL);
250 layoutData = new GridData();
251 layoutData.horizontalSpan = 2;
252 comp.setLayoutData(layoutData);
255 new ColorFieldEditor(
256 "quantum.string.color", //$NON-NLS-1$
257 Messages.getString("PreferencesPage.StringTextColor"), //$NON-NLS-1$
260 boldString = new Button(main, SWT.CHECK);
261 boldString.setSelection(stringFlag);
262 boldString.setText(Messages.getString("PreferencesPage.Bold")); //$NON-NLS-1$
263 boldString.addSelectionListener(new SelectionListener() {
264 public void widgetDefaultSelected(SelectionEvent e) {
266 public void widgetSelected(SelectionEvent e) {
267 stringFlag = boldString.getSelection();
271 comp = new Composite(main, SWT.NULL);
272 layoutData = new GridData();
273 layoutData.horizontalSpan = 2;
274 comp.setLayoutData(layoutData);
277 new ColorFieldEditor(
278 "quantum.numeric.color", //$NON-NLS-1$
279 Messages.getString("PreferencesPage.NumericTextColor"), //$NON-NLS-1$
282 boldNumeric = new Button(main, SWT.CHECK);
283 boldNumeric.setSelection(numericFlag);
284 boldNumeric.setText(Messages.getString("PreferencesPage.Bold")); //$NON-NLS-1$
285 boldNumeric.addSelectionListener(new SelectionListener() {
286 public void widgetDefaultSelected(SelectionEvent e) {
288 public void widgetSelected(SelectionEvent e) {
289 numericFlag = boldNumeric.getSelection();
293 backgroundColorEditor.setPreferencePage(this);
294 backgroundColorEditor.setPreferenceStore(getPreferenceStore());
295 backgroundColorEditor.load();
297 textColorEditor.setPreferencePage(this);
298 textColorEditor.setPreferenceStore(getPreferenceStore());
299 textColorEditor.load();
301 keywordColorEditor.setPreferencePage(this);
302 keywordColorEditor.setPreferenceStore(getPreferenceStore());
303 keywordColorEditor.load();
305 commentColorEditor.setPreferencePage(this);
306 commentColorEditor.setPreferenceStore(getPreferenceStore());
307 commentColorEditor.load();
309 stringColorEditor.setPreferencePage(this);
310 stringColorEditor.setPreferenceStore(getPreferenceStore());
311 stringColorEditor.load();
313 numericColorEditor.setPreferencePage(this);
314 numericColorEditor.setPreferenceStore(getPreferenceStore());
315 numericColorEditor.load();
319 public void updateFontDisplay() {
320 if (fontData == null) {
321 fontDisplay.setText(Messages.getString("PreferencesPage.Font_Default")); //$NON-NLS-1$
323 String style = Messages.getString("PreferencesPage.regular"); //$NON-NLS-1$
324 if (fontData.getStyle() == SWT.BOLD) {
325 style = Messages.getString("PreferencesPage.bold"); //$NON-NLS-1$
326 } else if (fontData.getStyle() == SWT.ITALIC) {
327 style = Messages.getString("PreferencesPage.italic"); //$NON-NLS-1$
328 } else if (fontData.getStyle() == (SWT.BOLD | SWT.ITALIC)) {
329 style = Messages.getString("PreferencesPage.boldItalic"); //$NON-NLS-1$
331 fontDisplay.setText(Messages.getString("PreferencesPage.FontPrompt") + fontData.getName() + '-' + style + '-' + fontData.getHeight()); //$NON-NLS-1$
334 public void updateFlags() {
335 boldText.setSelection(textFlag);
336 boldKeyword.setSelection(keywordFlag);
337 boldString.setSelection(stringFlag);
338 boldNumeric.setSelection(numericFlag);
339 boldComment.setSelection(commentFlag);