using Runnable to comply to the SWT threading model.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / preferences / SQLEditorPreferences.java
1 package com.quantum.preferences;
2
3
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;
9
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;
29
30 public class SQLEditorPreferences extends PreferencePage
31         implements IWorkbenchPreferencePage {
32         FontDialog fontDialog;
33         ColorFieldEditor backgroundColorEditor;
34         
35         ColorFieldEditor textColorEditor;
36         boolean textFlag;
37         ColorFieldEditor keywordColorEditor;
38         boolean keywordFlag;
39         ColorFieldEditor stringColorEditor;
40         boolean stringFlag;
41         ColorFieldEditor numericColorEditor;
42         boolean numericFlag;
43         ColorFieldEditor commentColorEditor;
44         boolean commentFlag;
45         
46         Button boldText;
47         Button boldKeyword;
48         Button boldString;
49         Button boldNumeric;
50         Button boldComment;
51         
52         IWorkbench workbench;
53         FontData fontData;
54         Label fontDisplay;
55         public void init(IWorkbench workbench) {
56                 //Initialize the preference store
57                 this.workbench = workbench;
58                 setPreferenceStore(QuantumPlugin.getDefault().getPreferenceStore());
59                 initializeColorDefaults(getPreferenceStore());
60         }
61
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$
81         }
82
83         protected void performDefaults() {
84                 fontData = null;
85                 updateFontDisplay();
86                 textFlag = false;
87                 keywordFlag = true;
88                 stringFlag = false;
89                 numericFlag = false;
90                 commentFlag = false;
91                 updateFlags();
92                 backgroundColorEditor.loadDefault();
93                 textColorEditor.loadDefault();
94                 keywordColorEditor.loadDefault();
95                 stringColorEditor.loadDefault();
96                 commentColorEditor.loadDefault();
97                 numericColorEditor.loadDefault();
98         }
99         /** 
100          * Save the preferences to the preference store.
101          */
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();
116         }
117         protected Control createContents(Composite parent) {
118                 Composite main = new Composite(parent, SWT.NULL);
119                 
120                 GridLayout innerLayout = new GridLayout();
121                 innerLayout.numColumns = 4;
122                 main.setLayout(innerLayout);
123
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$
130                 
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) {
136                         }
137                         public void widgetSelected(SelectionEvent e) {
138                                 if (fontData != null) {
139                     VersioningHelper.setFont(fontDialog, new FontData[] { fontData} );
140                                 }
141                                 FontData data = fontDialog.open();
142                                 if (data != null) {
143                                         fontData = data;
144                                         updateFontDisplay();
145                                 }
146                         }
147                 });
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) {
152                         }
153                         public void widgetSelected(SelectionEvent e) {
154                                 FontData[] temp =  JFaceResources.getTextFont().getFontData();
155                                 fontData = temp == null || temp.length == 0 ? null : temp[0];
156                                 updateFontDisplay();
157                         }
158                 });
159
160                 fontDisplay = new Label(main, SWT.NULL);
161                 GridData data = new GridData(GridData.FILL_HORIZONTAL);
162                 data.grabExcessHorizontalSpace = true;
163                 fontDisplay.setLayoutData(data);
164                 updateFontDisplay();
165
166                 ColorManager manager = new ColorManager();
167
168                 Composite comp = new Composite(main, SWT.NULL);
169                 GridData layoutData = new GridData();
170                 layoutData.horizontalSpan = 2;
171                 comp.setLayoutData(layoutData);
172                 
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$
178                                 comp);                          
179
180                 Composite temp = new Composite(main, SWT.NULL);
181                 temp.setSize(0, 0);
182                 
183                 comp = new Composite(main, SWT.NULL);
184                 layoutData = new GridData();
185                 layoutData.horizontalSpan = 2;
186                 comp.setLayoutData(layoutData);
187                 
188                 textColorEditor =
189                         new ColorFieldEditor(
190                                 "quantum.text.color", //$NON-NLS-1$
191                                 Messages.getString("PreferencesPage.DefaultTextColor"), //$NON-NLS-1$
192                                 comp);                          
193
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) {
199                         }
200                         public void widgetSelected(SelectionEvent e) {
201                                 textFlag = boldText.getSelection();
202                         }
203                 });
204
205                 comp = new Composite(main, SWT.NULL);
206                 layoutData = new GridData();
207                 layoutData.horizontalSpan = 2;
208                 comp.setLayoutData(layoutData);
209
210                 keywordColorEditor =
211                         new ColorFieldEditor(
212                                 Messages.getString("PreferencesPage.quantum.keyword.color_26"), //$NON-NLS-1$
213                                 Messages.getString("PreferencesPage.KeywordTextColor"), //$NON-NLS-1$
214                                 comp);
215
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) {
221                         }
222                         public void widgetSelected(SelectionEvent e) {
223                                 keywordFlag = boldKeyword.getSelection();
224                         }
225                 });
226
227                 comp = new Composite(main, SWT.NULL);
228                 layoutData = new GridData();
229                 layoutData.horizontalSpan = 2;
230                 comp.setLayoutData(layoutData);
231
232                 commentColorEditor =
233                         new ColorFieldEditor(
234                                 "quantum.comment.color", //$NON-NLS-1$
235                                 Messages.getString("PreferencesPage.CommentTextColor"), //$NON-NLS-1$
236                                 comp);
237
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) {
243                         }
244                         public void widgetSelected(SelectionEvent e) {
245                                 commentFlag = boldComment.getSelection();
246                         }
247                 });
248
249                 comp = new Composite(main, SWT.NULL);
250                 layoutData = new GridData();
251                 layoutData.horizontalSpan = 2;
252                 comp.setLayoutData(layoutData);
253
254                 stringColorEditor =
255                         new ColorFieldEditor(
256                                 "quantum.string.color", //$NON-NLS-1$
257                                 Messages.getString("PreferencesPage.StringTextColor"), //$NON-NLS-1$
258                                 comp);
259
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) {
265                         }
266                         public void widgetSelected(SelectionEvent e) {
267                                 stringFlag = boldString.getSelection();
268                         }
269                 });
270
271                 comp = new Composite(main, SWT.NULL);
272                 layoutData = new GridData();
273                 layoutData.horizontalSpan = 2;
274                 comp.setLayoutData(layoutData);
275
276                 numericColorEditor =
277                         new ColorFieldEditor(
278                                 "quantum.numeric.color", //$NON-NLS-1$
279                                 Messages.getString("PreferencesPage.NumericTextColor"), //$NON-NLS-1$
280                                 comp);
281
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) {
287                         }
288                         public void widgetSelected(SelectionEvent e) {
289                                 numericFlag = boldNumeric.getSelection();
290                         }
291                 });
292
293                 backgroundColorEditor.setPreferencePage(this);
294                 backgroundColorEditor.setPreferenceStore(getPreferenceStore());
295                 backgroundColorEditor.load();
296                 
297                 textColorEditor.setPreferencePage(this);
298                 textColorEditor.setPreferenceStore(getPreferenceStore());
299                 textColorEditor.load();
300                 
301                 keywordColorEditor.setPreferencePage(this);
302                 keywordColorEditor.setPreferenceStore(getPreferenceStore());
303                 keywordColorEditor.load();
304                 
305                 commentColorEditor.setPreferencePage(this);
306                 commentColorEditor.setPreferenceStore(getPreferenceStore());
307                 commentColorEditor.load();
308                 
309                 stringColorEditor.setPreferencePage(this);
310                 stringColorEditor.setPreferenceStore(getPreferenceStore());
311                 stringColorEditor.load();
312
313                 numericColorEditor.setPreferencePage(this);
314                 numericColorEditor.setPreferenceStore(getPreferenceStore());
315                 numericColorEditor.load();
316
317                 return main;
318         }
319         public void updateFontDisplay() {
320                 if (fontData == null) {
321                         fontDisplay.setText(Messages.getString("PreferencesPage.Font_Default")); //$NON-NLS-1$
322                 } else {
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$
330                         }
331                         fontDisplay.setText(Messages.getString("PreferencesPage.FontPrompt") + fontData.getName() + '-' + style + '-' + fontData.getHeight()); //$NON-NLS-1$
332                 }
333         }
334         public void updateFlags() {
335                 boldText.setSelection(textFlag);
336                 boldKeyword.setSelection(keywordFlag);
337                 boldString.setSelection(stringFlag);
338                 boldNumeric.setSelection(numericFlag);
339                 boldComment.setSelection(commentFlag);
340         }
341 }