*** empty log message ***
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / preferences / PreferencesPage.java
1 package net.sourceforge.phpdt.sql.preferences;
2
3 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
4
5 import org.eclipse.jface.preference.ColorFieldEditor;
6 import org.eclipse.jface.preference.IPreferenceStore;
7 import org.eclipse.jface.preference.PreferenceConverter;
8 import org.eclipse.jface.preference.PreferencePage;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.events.SelectionEvent;
11 import org.eclipse.swt.events.SelectionListener;
12 import org.eclipse.swt.graphics.Color;
13 import org.eclipse.swt.graphics.FontData;
14 import org.eclipse.swt.graphics.RGB;
15 import org.eclipse.swt.layout.GridData;
16 import org.eclipse.swt.layout.GridLayout;
17 import org.eclipse.swt.widgets.Button;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.FontDialog;
21 import org.eclipse.swt.widgets.Label;
22 import org.eclipse.ui.IWorkbench;
23 import org.eclipse.ui.IWorkbenchPreferencePage;
24
25 import net.sourceforge.phpdt.sql.editors.ColorManager;
26 import net.sourceforge.phpdt.sql.editors.SQLColorConstants;
27
28 public class PreferencesPage
29   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(
70       store,
71       "phpeclipse.sql.background.color",
72       BACKGROUND);
73     PreferenceConverter.setDefault(store, "phpeclipse.sql.text.color", DEFAULT);
74     PreferenceConverter.setDefault(
75       store,
76       "phpeclipse.sql.keyword.color",
77       KEYWORD);
78     PreferenceConverter.setDefault(
79       store,
80       "phpeclipse.sql.comment.color",
81       COMMENT);
82     PreferenceConverter.setDefault(
83       store,
84       "phpeclipse.sql.string.color",
85       STRING);
86     PreferenceConverter.setDefault(
87       store,
88       "phpeclipse.sql.numeric.color",
89       NUMERIC);
90   }
91
92   protected void performDefaults() {
93     fontData = null;
94     updateFontDisplay();
95     textFlag = false;
96     keywordFlag = true;
97     stringFlag = false;
98     numericFlag = false;
99     commentFlag = false;
100     updateFlags();
101     backgroundColorEditor.loadDefault();
102     textColorEditor.loadDefault();
103     keywordColorEditor.loadDefault();
104     stringColorEditor.loadDefault();
105     commentColorEditor.loadDefault();
106     numericColorEditor.loadDefault();
107   }
108   /** 
109    * Save the preferences to the preference store.
110    */
111   public boolean performOk() {
112     PreferenceConverter.setValue(
113       getPreferenceStore(),
114       "phpeclipse.sql.font",
115       fontData);
116     getPreferenceStore().setValue("phpeclipse.sql.text.bold", textFlag);
117     getPreferenceStore().setValue("phpeclipse.sql.keyword.bold", keywordFlag);
118     getPreferenceStore().setValue("phpeclipse.sql.string.bold", stringFlag);
119     getPreferenceStore().setValue("phpeclipse.sql.comment.bold", commentFlag);
120     getPreferenceStore().setValue("phpeclipse.sql.numeric.bold", numericFlag);
121     backgroundColorEditor.store();
122     textColorEditor.store();
123     keywordColorEditor.store();
124     stringColorEditor.store();
125     commentColorEditor.store();
126     numericColorEditor.store();
127     return super.performOk();
128   }
129   
130   protected Control createContents(Composite parent) {
131     Composite composite = new Composite(parent, SWT.NULL);
132
133     GridLayout innerLayout = new GridLayout();
134     innerLayout.numColumns = 4;
135     composite.setLayout(innerLayout);
136
137     fontData =
138       PreferenceConverter.getFontData(
139         getPreferenceStore(),
140         "phpeclipse.sql.font");
141     textFlag = getPreferenceStore().getBoolean("phpeclipse.sql.text.bold");
142     keywordFlag =
143       getPreferenceStore().getBoolean("phpeclipse.sql.keyword.bold");
144     stringFlag = getPreferenceStore().getBoolean("phpeclipse.sql.string.bold");
145     commentFlag =
146       getPreferenceStore().getBoolean("phpeclipse.sql.comment.bold");
147     numericFlag =
148       getPreferenceStore().getBoolean("phpeclipse.sql.numeric.bold");
149
150     fontDialog =
151       new FontDialog(workbench.getActiveWorkbenchWindow().getShell());
152     Button fontButton = new Button(composite, SWT.PUSH);
153     fontButton.setText("Pick Font");
154     fontButton.addSelectionListener(new SelectionListener() {
155       public void widgetDefaultSelected(SelectionEvent e) {
156       }
157       public void widgetSelected(SelectionEvent e) {
158         if (fontData != null) {
159           fontDialog.setFontData(fontData);
160         }
161         FontData data = fontDialog.open();
162         if (data != null) {
163           fontData = data;
164           updateFontDisplay();
165         }
166       }
167     });
168     Button defaultButton = new Button(composite, SWT.PUSH);
169     defaultButton.setText("Default Font");
170     defaultButton.addSelectionListener(new SelectionListener() {
171       public void widgetDefaultSelected(SelectionEvent e) {
172       }
173       public void widgetSelected(SelectionEvent e) {
174         fontData = null;
175         updateFontDisplay();
176       }
177     });
178
179     fontDisplay = new Label(composite, SWT.NULL);
180     GridData data = new GridData(GridData.FILL_HORIZONTAL);
181     data.grabExcessHorizontalSpace = true;
182     fontDisplay.setLayoutData(data);
183     updateFontDisplay();
184
185     ColorManager manager = new ColorManager();
186
187     Composite comp = new Composite(composite, SWT.NULL);
188     GridData layoutData = new GridData();
189     layoutData.horizontalSpan = 2;
190     comp.setLayoutData(layoutData);
191
192     Color defaultColor = manager.getColor(SQLColorConstants.DEFAULT);
193     backgroundColorEditor =
194       new ColorFieldEditor(
195         "phpeclipse.sql.background.color",
196         "Background Color",
197         comp);
198
199     Composite temp = new Composite(composite, SWT.NULL);
200     temp.setSize(0, 0);
201
202     comp = new Composite(composite, SWT.NULL);
203     layoutData = new GridData();
204     layoutData.horizontalSpan = 2;
205     comp.setLayoutData(layoutData);
206
207     textColorEditor =
208       new ColorFieldEditor(
209         "phpeclipse.sql.text.color",
210         "Default Text Color",
211         comp);
212
213     boldText = new Button(composite, SWT.CHECK);
214     boldText.setSelection(textFlag);
215     boldText.setText("Bold");
216     boldText.addSelectionListener(new SelectionListener() {
217       public void widgetDefaultSelected(SelectionEvent e) {
218       }
219       public void widgetSelected(SelectionEvent e) {
220         textFlag = boldText.getSelection();
221       }
222     });
223
224     comp = new Composite(composite, SWT.NULL);
225     layoutData = new GridData();
226     layoutData.horizontalSpan = 2;
227     comp.setLayoutData(layoutData);
228
229     keywordColorEditor =
230       new ColorFieldEditor(
231         "phpeclipse.sql.keyword.color",
232         "Keyword Text Color",
233         comp);
234
235     boldKeyword = new Button(composite, SWT.CHECK);
236     boldKeyword.setSelection(keywordFlag);
237     boldKeyword.setText("Bold");
238     boldKeyword.addSelectionListener(new SelectionListener() {
239       public void widgetDefaultSelected(SelectionEvent e) {
240       }
241       public void widgetSelected(SelectionEvent e) {
242         keywordFlag = boldKeyword.getSelection();
243       }
244     });
245
246     comp = new Composite(composite, SWT.NULL);
247     layoutData = new GridData();
248     layoutData.horizontalSpan = 2;
249     comp.setLayoutData(layoutData);
250
251     commentColorEditor =
252       new ColorFieldEditor(
253         "phpeclipse.sql.comment.color",
254         "Comment Text Color",
255         comp);
256
257     boldComment = new Button(composite, SWT.CHECK);
258     boldComment.setSelection(commentFlag);
259     boldComment.setText("Bold");
260     boldComment.addSelectionListener(new SelectionListener() {
261       public void widgetDefaultSelected(SelectionEvent e) {
262       }
263       public void widgetSelected(SelectionEvent e) {
264         commentFlag = boldComment.getSelection();
265       }
266     });
267
268     comp = new Composite(composite, SWT.NULL);
269     layoutData = new GridData();
270     layoutData.horizontalSpan = 2;
271     comp.setLayoutData(layoutData);
272
273     stringColorEditor =
274       new ColorFieldEditor(
275         "phpeclipse.sql.string.color",
276         "String Text Color",
277         comp);
278
279     boldString = new Button(composite, SWT.CHECK);
280     boldString.setSelection(stringFlag);
281     boldString.setText("Bold");
282     boldString.addSelectionListener(new SelectionListener() {
283       public void widgetDefaultSelected(SelectionEvent e) {
284       }
285       public void widgetSelected(SelectionEvent e) {
286         stringFlag = boldString.getSelection();
287       }
288     });
289
290     comp = new Composite(composite, SWT.NULL);
291     layoutData = new GridData();
292     layoutData.horizontalSpan = 2;
293     comp.setLayoutData(layoutData);
294
295     numericColorEditor =
296       new ColorFieldEditor(
297         "phpeclipse.sql.numeric.color",
298         "Numeric Text Color",
299         comp);
300
301     boldNumeric = new Button(composite, SWT.CHECK);
302     boldNumeric.setSelection(numericFlag);
303     boldNumeric.setText("Bold");
304     boldNumeric.addSelectionListener(new SelectionListener() {
305       public void widgetDefaultSelected(SelectionEvent e) {
306       }
307       public void widgetSelected(SelectionEvent e) {
308         numericFlag = boldNumeric.getSelection();
309       }
310     });
311
312     backgroundColorEditor.setPreferencePage(this);
313     backgroundColorEditor.setPreferenceStore(getPreferenceStore());
314     backgroundColorEditor.load();
315
316     textColorEditor.setPreferencePage(this);
317     textColorEditor.setPreferenceStore(getPreferenceStore());
318     textColorEditor.load();
319
320     keywordColorEditor.setPreferencePage(this);
321     keywordColorEditor.setPreferenceStore(getPreferenceStore());
322     keywordColorEditor.load();
323
324     commentColorEditor.setPreferencePage(this);
325     commentColorEditor.setPreferenceStore(getPreferenceStore());
326     commentColorEditor.load();
327
328     stringColorEditor.setPreferencePage(this);
329     stringColorEditor.setPreferenceStore(getPreferenceStore());
330     stringColorEditor.load();
331
332     numericColorEditor.setPreferencePage(this);
333     numericColorEditor.setPreferenceStore(getPreferenceStore());
334     numericColorEditor.load();
335
336     return composite;
337   }
338   public void updateFontDisplay() {
339     if (fontData == null) {
340       fontDisplay.setText("Font: default");
341     } else {
342       String style = "regular";
343       if (fontData.getStyle() == SWT.BOLD) {
344         style = "bold";
345       } else if (fontData.getStyle() == SWT.ITALIC) {
346         style = "italic";
347       } else if (fontData.getStyle() == (SWT.BOLD | SWT.ITALIC)) {
348         style = "bold italic";
349       }
350       fontDisplay.setText(
351         "Font: "
352           + fontData.getName()
353           + '-'
354           + style
355           + '-'
356           + fontData.getHeight());
357     }
358   }
359   public void updateFlags() {
360     boldText.setSelection(textFlag);
361     boldKeyword.setSelection(keywordFlag);
362     boldString.setSelection(stringFlag);
363     boldNumeric.setSelection(numericFlag);
364     boldComment.setSelection(commentFlag);
365   }
366 }