3.x compatibility
[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.swt.SWT;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.events.SelectionListener;
17 import org.eclipse.swt.graphics.FontData;
18 import org.eclipse.swt.graphics.RGB;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.FontDialog;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.ui.IWorkbench;
27 import org.eclipse.ui.IWorkbenchPreferencePage;
28
29 public class SQLEditorPreferences extends PreferencePage
30         implements IWorkbenchPreferencePage {
31         FontDialog fontDialog;
32         ColorFieldEditor backgroundColorEditor;
33         
34         ColorFieldEditor textColorEditor;
35         boolean textFlag;
36         ColorFieldEditor keywordColorEditor;
37         boolean keywordFlag;
38         ColorFieldEditor stringColorEditor;
39         boolean stringFlag;
40         ColorFieldEditor numericColorEditor;
41         boolean numericFlag;
42         ColorFieldEditor commentColorEditor;
43         boolean commentFlag;
44         
45         Button boldText;
46         Button boldKeyword;
47         Button boldString;
48         Button boldNumeric;
49         Button boldComment;
50         
51         IWorkbench workbench;
52         FontData fontData;
53         Label fontDisplay;
54         public void init(IWorkbench workbench) {
55                 //Initialize the preference store
56                 this.workbench = workbench;
57                 setPreferenceStore(QuantumPlugin.getDefault().getPreferenceStore());
58                 initializeColorDefaults(getPreferenceStore());
59         }
60
61         private void initializeColorDefaults(IPreferenceStore store) {
62                 RGB BACKGROUND = new RGB(255, 255, 255);
63                 RGB COMMENT = new RGB(88, 148, 64);
64                 RGB KEYWORD = new RGB(126, 0, 75);
65                 RGB STRING = new RGB(0, 0, 255);
66                 RGB NUMERIC = new RGB(255, 0, 0);
67                 RGB DEFAULT = new RGB(0, 0, 0);
68                 PreferenceConverter.setDefault(store,
69                         "quantum.background.color", BACKGROUND); //$NON-NLS-1$
70                 PreferenceConverter.setDefault(store,
71                         "quantum.text.color", DEFAULT); //$NON-NLS-1$
72                 PreferenceConverter.setDefault(store,
73                         "quantum.keyword.color", KEYWORD); //$NON-NLS-1$
74                 PreferenceConverter.setDefault(store,
75                         "quantum.comment.color", COMMENT); //$NON-NLS-1$
76                 PreferenceConverter.setDefault(store,
77                         "quantum.string.color", STRING); //$NON-NLS-1$
78                 PreferenceConverter.setDefault(store,
79                         "quantum.numeric.color", NUMERIC); //$NON-NLS-1$
80         }
81
82         protected void performDefaults() {
83                 fontData = null;
84                 updateFontDisplay();
85                 textFlag = false;
86                 keywordFlag = true;
87                 stringFlag = false;
88                 numericFlag = false;
89                 commentFlag = false;
90                 updateFlags();
91                 backgroundColorEditor.loadDefault();
92                 textColorEditor.loadDefault();
93                 keywordColorEditor.loadDefault();
94                 stringColorEditor.loadDefault();
95                 commentColorEditor.loadDefault();
96                 numericColorEditor.loadDefault();
97         }
98         /** 
99          * Save the preferences to the preference store.
100          */
101         public boolean performOk() {
102                 PreferenceConverter.setValue(getPreferenceStore(), "quantum.font", fontData); //$NON-NLS-1$
103                 getPreferenceStore().setValue("quantum.text.bold", textFlag); //$NON-NLS-1$
104                 getPreferenceStore().setValue("quantum.keyword.bold", keywordFlag); //$NON-NLS-1$
105                 getPreferenceStore().setValue("quantum.string.bold", stringFlag); //$NON-NLS-1$
106                 getPreferenceStore().setValue("quantum.comment.bold", commentFlag); //$NON-NLS-1$
107                 getPreferenceStore().setValue("quantum.numeric.bold", numericFlag); //$NON-NLS-1$
108                 backgroundColorEditor.store();
109                 textColorEditor.store();
110                 keywordColorEditor.store();
111                 stringColorEditor.store();
112                 commentColorEditor.store();
113                 numericColorEditor.store();
114                 return super.performOk();
115         }
116         protected Control createContents(Composite parent) {
117                 Composite main = new Composite(parent, SWT.NULL);
118                 
119                 GridLayout innerLayout = new GridLayout();
120                 innerLayout.numColumns = 4;
121                 main.setLayout(innerLayout);
122
123                 fontData = PreferenceConverter.getFontData(getPreferenceStore(), "quantum.font"); //$NON-NLS-1$
124                 textFlag = getPreferenceStore().getBoolean("quantum.text.bold"); //$NON-NLS-1$
125                 keywordFlag = getPreferenceStore().getBoolean("quantum.keyword.bold"); //$NON-NLS-1$
126                 stringFlag = getPreferenceStore().getBoolean("quantum.string.bold"); //$NON-NLS-1$
127                 commentFlag = getPreferenceStore().getBoolean("quantum.comment.bold"); //$NON-NLS-1$
128                 numericFlag = getPreferenceStore().getBoolean("quantum.numeric.bold"); //$NON-NLS-1$
129                 
130                 fontDialog = new FontDialog(workbench.getActiveWorkbenchWindow().getShell());
131                 Button fontButton = new Button(main, SWT.PUSH);
132                 fontButton.setText(Messages.getString("PreferencesPage.PickFont")); //$NON-NLS-1$
133                 fontButton.addSelectionListener(new SelectionListener() {
134                         public void widgetDefaultSelected(SelectionEvent e) {
135                         }
136                         public void widgetSelected(SelectionEvent e) {
137                                 if (fontData != null) {
138                     VersioningHelper.setFont(fontDialog, new FontData[] { fontData} );
139                                 }
140                                 FontData data = fontDialog.open();
141                                 if (data != null) {
142                                         fontData = data;
143                                         updateFontDisplay();
144                                 }
145                         }
146                 });
147                 Button defaultButton = new Button(main, SWT.PUSH);
148                 defaultButton.setText(Messages.getString("PreferencesPage.DefaultFont")); //$NON-NLS-1$
149                 defaultButton.addSelectionListener(new SelectionListener() {
150                         public void widgetDefaultSelected(SelectionEvent e) {
151                         }
152                         public void widgetSelected(SelectionEvent e) {
153                                 fontData = null;
154                                 updateFontDisplay();
155                         }
156                 });
157
158                 fontDisplay = new Label(main, SWT.NULL);
159                 GridData data = new GridData(GridData.FILL_HORIZONTAL);
160                 data.grabExcessHorizontalSpace = true;
161                 fontDisplay.setLayoutData(data);
162                 updateFontDisplay();
163
164                 ColorManager manager = new ColorManager();
165
166                 Composite comp = new Composite(main, SWT.NULL);
167                 GridData layoutData = new GridData();
168                 layoutData.horizontalSpan = 2;
169                 comp.setLayoutData(layoutData);
170                 
171                 manager.getColor(SQLColorConstants.DEFAULT);
172                 backgroundColorEditor =
173                         new ColorFieldEditor(
174                                 "quantum.background.color", //$NON-NLS-1$
175                                 Messages.getString("PreferencesPage.BackgroundColor"), //$NON-NLS-1$
176                                 comp);                          
177
178                 Composite temp = new Composite(main, SWT.NULL);
179                 temp.setSize(0, 0);
180                 
181                 comp = new Composite(main, SWT.NULL);
182                 layoutData = new GridData();
183                 layoutData.horizontalSpan = 2;
184                 comp.setLayoutData(layoutData);
185                 
186                 textColorEditor =
187                         new ColorFieldEditor(
188                                 "quantum.text.color", //$NON-NLS-1$
189                                 Messages.getString("PreferencesPage.DefaultTextColor"), //$NON-NLS-1$
190                                 comp);                          
191
192                 boldText = new Button(main, SWT.CHECK);
193                 boldText.setSelection(textFlag);
194                 boldText.setText(Messages.getString("PreferencesPage.Bold")); //$NON-NLS-1$
195                 boldText.addSelectionListener(new SelectionListener() {
196                         public void widgetDefaultSelected(SelectionEvent e) {
197                         }
198                         public void widgetSelected(SelectionEvent e) {
199                                 textFlag = boldText.getSelection();
200                         }
201                 });
202
203                 comp = new Composite(main, SWT.NULL);
204                 layoutData = new GridData();
205                 layoutData.horizontalSpan = 2;
206                 comp.setLayoutData(layoutData);
207
208                 keywordColorEditor =
209                         new ColorFieldEditor(
210                                 Messages.getString("PreferencesPage.quantum.keyword.color_26"), //$NON-NLS-1$
211                                 Messages.getString("PreferencesPage.KeywordTextColor"), //$NON-NLS-1$
212                                 comp);
213
214                 boldKeyword = new Button(main, SWT.CHECK);
215                 boldKeyword.setSelection(keywordFlag);
216                 boldKeyword.setText(Messages.getString("PreferencesPage.Bold")); //$NON-NLS-1$
217                 boldKeyword.addSelectionListener(new SelectionListener() {
218                         public void widgetDefaultSelected(SelectionEvent e) {
219                         }
220                         public void widgetSelected(SelectionEvent e) {
221                                 keywordFlag = boldKeyword.getSelection();
222                         }
223                 });
224
225                 comp = new Composite(main, SWT.NULL);
226                 layoutData = new GridData();
227                 layoutData.horizontalSpan = 2;
228                 comp.setLayoutData(layoutData);
229
230                 commentColorEditor =
231                         new ColorFieldEditor(
232                                 "quantum.comment.color", //$NON-NLS-1$
233                                 Messages.getString("PreferencesPage.CommentTextColor"), //$NON-NLS-1$
234                                 comp);
235
236                 boldComment = new Button(main, SWT.CHECK);
237                 boldComment.setSelection(commentFlag);
238                 boldComment.setText(Messages.getString("PreferencesPage.Bold")); //$NON-NLS-1$
239                 boldComment.addSelectionListener(new SelectionListener() {
240                         public void widgetDefaultSelected(SelectionEvent e) {
241                         }
242                         public void widgetSelected(SelectionEvent e) {
243                                 commentFlag = boldComment.getSelection();
244                         }
245                 });
246
247                 comp = new Composite(main, SWT.NULL);
248                 layoutData = new GridData();
249                 layoutData.horizontalSpan = 2;
250                 comp.setLayoutData(layoutData);
251
252                 stringColorEditor =
253                         new ColorFieldEditor(
254                                 "quantum.string.color", //$NON-NLS-1$
255                                 Messages.getString("PreferencesPage.StringTextColor"), //$NON-NLS-1$
256                                 comp);
257
258                 boldString = new Button(main, SWT.CHECK);
259                 boldString.setSelection(stringFlag);
260                 boldString.setText(Messages.getString("PreferencesPage.Bold")); //$NON-NLS-1$
261                 boldString.addSelectionListener(new SelectionListener() {
262                         public void widgetDefaultSelected(SelectionEvent e) {
263                         }
264                         public void widgetSelected(SelectionEvent e) {
265                                 stringFlag = boldString.getSelection();
266                         }
267                 });
268
269                 comp = new Composite(main, SWT.NULL);
270                 layoutData = new GridData();
271                 layoutData.horizontalSpan = 2;
272                 comp.setLayoutData(layoutData);
273
274                 numericColorEditor =
275                         new ColorFieldEditor(
276                                 "quantum.numeric.color", //$NON-NLS-1$
277                                 Messages.getString("PreferencesPage.NumericTextColor"), //$NON-NLS-1$
278                                 comp);
279
280                 boldNumeric = new Button(main, SWT.CHECK);
281                 boldNumeric.setSelection(numericFlag);
282                 boldNumeric.setText(Messages.getString("PreferencesPage.Bold")); //$NON-NLS-1$
283                 boldNumeric.addSelectionListener(new SelectionListener() {
284                         public void widgetDefaultSelected(SelectionEvent e) {
285                         }
286                         public void widgetSelected(SelectionEvent e) {
287                                 numericFlag = boldNumeric.getSelection();
288                         }
289                 });
290
291                 backgroundColorEditor.setPreferencePage(this);
292                 backgroundColorEditor.setPreferenceStore(getPreferenceStore());
293                 backgroundColorEditor.load();
294                 
295                 textColorEditor.setPreferencePage(this);
296                 textColorEditor.setPreferenceStore(getPreferenceStore());
297                 textColorEditor.load();
298                 
299                 keywordColorEditor.setPreferencePage(this);
300                 keywordColorEditor.setPreferenceStore(getPreferenceStore());
301                 keywordColorEditor.load();
302                 
303                 commentColorEditor.setPreferencePage(this);
304                 commentColorEditor.setPreferenceStore(getPreferenceStore());
305                 commentColorEditor.load();
306                 
307                 stringColorEditor.setPreferencePage(this);
308                 stringColorEditor.setPreferenceStore(getPreferenceStore());
309                 stringColorEditor.load();
310
311                 numericColorEditor.setPreferencePage(this);
312                 numericColorEditor.setPreferenceStore(getPreferenceStore());
313                 numericColorEditor.load();
314
315                 return main;
316         }
317         public void updateFontDisplay() {
318                 if (fontData == null) {
319                         fontDisplay.setText(Messages.getString("PreferencesPage.Font_Default")); //$NON-NLS-1$
320                 } else {
321                         String style = Messages.getString("PreferencesPage.regular"); //$NON-NLS-1$
322                         if (fontData.getStyle() == SWT.BOLD) {
323                                 style = Messages.getString("PreferencesPage.bold"); //$NON-NLS-1$
324                         } else if (fontData.getStyle() == SWT.ITALIC) {
325                                 style = Messages.getString("PreferencesPage.italic"); //$NON-NLS-1$
326                         } else if (fontData.getStyle() == (SWT.BOLD | SWT.ITALIC)) {
327                                 style = Messages.getString("PreferencesPage.boldItalic"); //$NON-NLS-1$
328                         }
329                         fontDisplay.setText(Messages.getString("PreferencesPage.FontPrompt") + fontData.getName() + '-' + style + '-' + fontData.getHeight()); //$NON-NLS-1$
330                 }
331         }
332         public void updateFlags() {
333                 boldText.setSelection(textFlag);
334                 boldKeyword.setSelection(keywordFlag);
335                 boldString.setSelection(stringFlag);
336                 boldNumeric.setSelection(numericFlag);
337                 boldComment.setSelection(commentFlag);
338         }
339 }