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