SQL Plugin copied from Quantum plugin and refactored for PHPEclipse
[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 net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
5
6 import org.eclipse.jface.preference.ColorFieldEditor;
7 import org.eclipse.jface.preference.IPreferenceStore;
8 import org.eclipse.jface.preference.PreferenceConverter;
9 import org.eclipse.jface.preference.PreferencePage;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.events.SelectionEvent;
12 import org.eclipse.swt.events.SelectionListener;
13 import org.eclipse.swt.graphics.Color;
14 import org.eclipse.swt.graphics.FontData;
15 import org.eclipse.swt.graphics.RGB;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.layout.GridLayout;
18 import org.eclipse.swt.widgets.Button;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.FontDialog;
22 import org.eclipse.swt.widgets.Label;
23 import org.eclipse.ui.IWorkbench;
24 import org.eclipse.ui.IWorkbenchPreferencePage;
25
26 import net.sourceforge.phpdt.sql.editors.ColorManager;
27 import net.sourceforge.phpdt.sql.editors.SQLColorConstants;
28
29 public class PreferencesPage 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(PHPEclipseSQLPlugin.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 IDENTIFIER = new RGB(0, 0, 0);
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);
71                 PreferenceConverter.setDefault(store,
72                         "quantum.text.color", DEFAULT);
73                 PreferenceConverter.setDefault(store,
74                         "quantum.keyword.color", KEYWORD);
75                 PreferenceConverter.setDefault(store,
76                         "quantum.comment.color", COMMENT);
77                 PreferenceConverter.setDefault(store,
78                         "quantum.string.color", STRING);
79                 PreferenceConverter.setDefault(store,
80                         "quantum.numeric.color", NUMERIC);
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);
104                 getPreferenceStore().setValue("quantum.text.bold", textFlag);
105                 getPreferenceStore().setValue("quantum.keyword.bold", keywordFlag);
106                 getPreferenceStore().setValue("quantum.string.bold", stringFlag);
107                 getPreferenceStore().setValue("quantum.comment.bold", commentFlag);
108                 getPreferenceStore().setValue("quantum.numeric.bold", numericFlag);
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");
125                 textFlag = getPreferenceStore().getBoolean("quantum.text.bold");
126                 keywordFlag = getPreferenceStore().getBoolean("quantum.keyword.bold");
127                 stringFlag = getPreferenceStore().getBoolean("quantum.string.bold");
128                 commentFlag = getPreferenceStore().getBoolean("quantum.comment.bold");
129                 numericFlag = getPreferenceStore().getBoolean("quantum.numeric.bold");
130                 
131                 fontDialog = new FontDialog(workbench.getActiveWorkbenchWindow().getShell());
132                 Button fontButton = new Button(main, SWT.PUSH);
133                 fontButton.setText("Pick Font");
134                 fontButton.addSelectionListener(new SelectionListener() {
135                         public void widgetDefaultSelected(SelectionEvent e) {
136                         }
137                         public void widgetSelected(SelectionEvent e) {
138                                 if (fontData != null) {
139                                         fontDialog.setFontData(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("Default Font");
150                 defaultButton.addSelectionListener(new SelectionListener() {
151                         public void widgetDefaultSelected(SelectionEvent e) {
152                         }
153                         public void widgetSelected(SelectionEvent e) {
154                                 fontData = null;
155                                 updateFontDisplay();
156                         }
157                 });
158
159                 fontDisplay = new Label(main, SWT.NULL);
160                 GridData data = new GridData(GridData.FILL_HORIZONTAL);
161                 data.grabExcessHorizontalSpace = true;
162                 fontDisplay.setLayoutData(data);
163                 updateFontDisplay();
164
165                 ColorManager manager = new ColorManager();
166
167                 Composite comp = new Composite(main, SWT.NULL);
168                 GridData layoutData = new GridData();
169                 layoutData.horizontalSpan = 2;
170                 comp.setLayoutData(layoutData);
171                 
172                 Color defaultColor = manager.getColor(SQLColorConstants.DEFAULT);
173                 backgroundColorEditor =
174                         new ColorFieldEditor(
175                                 "quantum.background.color",
176                                 "Background Color",
177                                 comp);                          
178
179                 Composite temp = new Composite(main, SWT.NULL);
180                 temp.setSize(0, 0);
181                 
182                 comp = new Composite(main, SWT.NULL);
183                 layoutData = new GridData();
184                 layoutData.horizontalSpan = 2;
185                 comp.setLayoutData(layoutData);
186                 
187                 textColorEditor =
188                         new ColorFieldEditor(
189                                 "quantum.text.color",
190                                 "Default Text Color",
191                                 comp);                          
192
193                 boldText = new Button(main, SWT.CHECK);
194                 boldText.setSelection(textFlag);
195                 boldText.setText("Bold");
196                 boldText.addSelectionListener(new SelectionListener() {
197                         public void widgetDefaultSelected(SelectionEvent e) {
198                         }
199                         public void widgetSelected(SelectionEvent e) {
200                                 textFlag = boldText.getSelection();
201                         }
202                 });
203
204                 comp = new Composite(main, SWT.NULL);
205                 layoutData = new GridData();
206                 layoutData.horizontalSpan = 2;
207                 comp.setLayoutData(layoutData);
208
209                 keywordColorEditor =
210                         new ColorFieldEditor(
211                                 "quantum.keyword.color",
212                                 "Keyword Text Color",
213                                 comp);
214
215                 boldKeyword = new Button(main, SWT.CHECK);
216                 boldKeyword.setSelection(keywordFlag);
217                 boldKeyword.setText("Bold");
218                 boldKeyword.addSelectionListener(new SelectionListener() {
219                         public void widgetDefaultSelected(SelectionEvent e) {
220                         }
221                         public void widgetSelected(SelectionEvent e) {
222                                 keywordFlag = boldKeyword.getSelection();
223                         }
224                 });
225
226                 comp = new Composite(main, SWT.NULL);
227                 layoutData = new GridData();
228                 layoutData.horizontalSpan = 2;
229                 comp.setLayoutData(layoutData);
230
231                 commentColorEditor =
232                         new ColorFieldEditor(
233                                 "quantum.comment.color",
234                                 "Comment Text Color",
235                                 comp);
236
237                 boldComment = new Button(main, SWT.CHECK);
238                 boldComment.setSelection(commentFlag);
239                 boldComment.setText("Bold");
240                 boldComment.addSelectionListener(new SelectionListener() {
241                         public void widgetDefaultSelected(SelectionEvent e) {
242                         }
243                         public void widgetSelected(SelectionEvent e) {
244                                 commentFlag = boldComment.getSelection();
245                         }
246                 });
247
248                 comp = new Composite(main, SWT.NULL);
249                 layoutData = new GridData();
250                 layoutData.horizontalSpan = 2;
251                 comp.setLayoutData(layoutData);
252
253                 stringColorEditor =
254                         new ColorFieldEditor(
255                                 "quantum.string.color",
256                                 "String Text Color",
257                                 comp);
258
259                 boldString = new Button(main, SWT.CHECK);
260                 boldString.setSelection(stringFlag);
261                 boldString.setText("Bold");
262                 boldString.addSelectionListener(new SelectionListener() {
263                         public void widgetDefaultSelected(SelectionEvent e) {
264                         }
265                         public void widgetSelected(SelectionEvent e) {
266                                 stringFlag = boldString.getSelection();
267                         }
268                 });
269
270                 comp = new Composite(main, SWT.NULL);
271                 layoutData = new GridData();
272                 layoutData.horizontalSpan = 2;
273                 comp.setLayoutData(layoutData);
274
275                 numericColorEditor =
276                         new ColorFieldEditor(
277                                 "quantum.numeric.color",
278                                 "Numeric Text Color",
279                                 comp);
280
281                 boldNumeric = new Button(main, SWT.CHECK);
282                 boldNumeric.setSelection(numericFlag);
283                 boldNumeric.setText("Bold");
284                 boldNumeric.addSelectionListener(new SelectionListener() {
285                         public void widgetDefaultSelected(SelectionEvent e) {
286                         }
287                         public void widgetSelected(SelectionEvent e) {
288                                 numericFlag = boldNumeric.getSelection();
289                         }
290                 });
291
292                 backgroundColorEditor.setPreferencePage(this);
293                 backgroundColorEditor.setPreferenceStore(getPreferenceStore());
294                 backgroundColorEditor.load();
295                 
296                 textColorEditor.setPreferencePage(this);
297                 textColorEditor.setPreferenceStore(getPreferenceStore());
298                 textColorEditor.load();
299                 
300                 keywordColorEditor.setPreferencePage(this);
301                 keywordColorEditor.setPreferenceStore(getPreferenceStore());
302                 keywordColorEditor.load();
303                 
304                 commentColorEditor.setPreferencePage(this);
305                 commentColorEditor.setPreferenceStore(getPreferenceStore());
306                 commentColorEditor.load();
307                 
308                 stringColorEditor.setPreferencePage(this);
309                 stringColorEditor.setPreferenceStore(getPreferenceStore());
310                 stringColorEditor.load();
311
312                 numericColorEditor.setPreferencePage(this);
313                 numericColorEditor.setPreferenceStore(getPreferenceStore());
314                 numericColorEditor.load();
315
316                 return main;
317         }
318         public void updateFontDisplay() {
319                 if (fontData == null) {
320                         fontDisplay.setText("Font: default");
321                 } else {
322                         String style = "regular";
323                         if (fontData.getStyle() == SWT.BOLD) {
324                                 style = "bold";
325                         } else if (fontData.getStyle() == SWT.ITALIC) {
326                                 style = "italic";
327                         } else if (fontData.getStyle() == (SWT.BOLD | SWT.ITALIC)) {
328                                 style = "bold italic";
329                         }
330                         fontDisplay.setText("Font: " + fontData.getName() + '-' + style + '-' + fontData.getHeight());
331                 }
332         }
333         public void updateFlags() {
334                 boldText.setSelection(textFlag);
335                 boldKeyword.setSelection(keywordFlag);
336                 boldString.setSelection(stringFlag);
337                 boldNumeric.setSelection(numericFlag);
338                 boldComment.setSelection(commentFlag);
339         }
340 }