3 new apache icons; thanks to Anderson Forteleza
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / preferences / SQLEditorPreferences.java
1 package com.quantum.preferences;
2
3 import com.quantum.Messages;
4 import com.quantum.PluginPreferences;
5 import com.quantum.QuantumPlugin;
6 import com.quantum.util.versioning.VersioningHelper;
7
8 import org.eclipse.jface.preference.ColorFieldEditor;
9 import org.eclipse.jface.preference.PreferenceConverter;
10 import org.eclipse.jface.preference.PreferencePage;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.events.SelectionEvent;
13 import org.eclipse.swt.events.SelectionListener;
14 import org.eclipse.swt.graphics.FontData;
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.Group;
22 import org.eclipse.swt.widgets.Label;
23 import org.eclipse.ui.IWorkbench;
24 import org.eclipse.ui.IWorkbenchPreferencePage;
25
26
27 /**
28  * A preference page for font and colour preferences for the SQL
29  * Editor.  
30  * 
31  * @author Tom Schneider
32  */
33 public class SQLEditorPreferences extends PreferencePage 
34                 implements IWorkbenchPreferencePage {
35
36     private FontDialog fontDialog;
37
38     private ColorFieldEditor backgroundColorEditor;
39     private ColorFieldEditor textColorEditor;
40     private ColorFieldEditor keywordColorEditor;
41     private ColorFieldEditor stringColorEditor;
42     private ColorFieldEditor numericColorEditor;
43     private ColorFieldEditor commentColorEditor;
44
45     private boolean textFlag;
46     private boolean keywordFlag;
47     private boolean stringFlag;
48     private boolean numericFlag;
49     private boolean commentFlag;
50         
51     private Button boldText;
52     private Button boldKeyword;
53     private Button boldString;
54     private Button boldNumeric;
55     private Button boldComment;
56         
57         private FontData fontData;
58         private Label fontDisplay;
59         
60         public void init(IWorkbench workbench) {
61                 setPreferenceStore(QuantumPlugin.getDefault().getPreferenceStore());
62         }
63
64         protected void performDefaults() {
65                 fontData = PluginPreferences.getDefaultFont();
66                 updateFontDisplay();
67                 this.textFlag = false;
68                 this.keywordFlag = true;
69                 this.stringFlag = false;
70                 this.numericFlag = false;
71                 this.commentFlag = false;
72                 updateFlags();
73                 backgroundColorEditor.loadDefault();
74                 textColorEditor.loadDefault();
75                 keywordColorEditor.loadDefault();
76                 stringColorEditor.loadDefault();
77                 commentColorEditor.loadDefault();
78                 numericColorEditor.loadDefault();
79         }
80         /** 
81          * Save the preferences to the preference store.
82          */
83         public boolean performOk() {
84                 PreferenceConverter.setValue(getPreferenceStore(), "quantum.font", fontData); //$NON-NLS-1$
85                 getPreferenceStore().setValue("quantum.text.bold", textFlag); //$NON-NLS-1$
86                 getPreferenceStore().setValue("quantum.keyword.bold", keywordFlag); //$NON-NLS-1$
87                 getPreferenceStore().setValue("quantum.string.bold", stringFlag); //$NON-NLS-1$
88                 getPreferenceStore().setValue("quantum.comment.bold", commentFlag); //$NON-NLS-1$
89                 getPreferenceStore().setValue("quantum.numeric.bold", numericFlag); //$NON-NLS-1$
90                 backgroundColorEditor.store();
91                 textColorEditor.store();
92                 keywordColorEditor.store();
93                 stringColorEditor.store();
94                 commentColorEditor.store();
95                 numericColorEditor.store();
96                 return super.performOk();
97         }
98         
99         protected Control createContents(Composite parent) {
100                 Composite main = new Composite(parent, SWT.NULL);
101                 main.setLayout(new GridLayout());
102                 
103         createFontSelectionArea(main);
104         Label label = new Label(main, SWT.NONE);
105         label.setText("");
106         
107                 createColorSelectionArea(main);
108
109                 return main;
110         }
111         /**
112      * @param main
113      * @return
114      */
115     private void createColorSelectionArea(Composite parent) {
116         Composite main = new Composite(parent, SWT.NULL);
117
118                 this.textFlag = getPreferenceStore().getBoolean("quantum.text.bold"); //$NON-NLS-1$
119                 this.keywordFlag = getPreferenceStore().getBoolean("quantum.keyword.bold"); //$NON-NLS-1$
120                 this.stringFlag = getPreferenceStore().getBoolean("quantum.string.bold"); //$NON-NLS-1$
121                 this.commentFlag = getPreferenceStore().getBoolean("quantum.comment.bold"); //$NON-NLS-1$
122                 this.numericFlag = getPreferenceStore().getBoolean("quantum.numeric.bold"); //$NON-NLS-1$
123                 
124         
125         GridLayout layout = new GridLayout();
126                 
127                 // the colour preference chooser really, really wants to take up a whole
128                 // row, so we need to pretend that our row is only two cells wide, then
129                 // pull a switcheroo later...
130                 layout.numColumns = 2;
131                 main.setLayout(layout);
132                 
133                 backgroundColorEditor =
134                         new ColorFieldEditor(
135                                 PluginPreferences.BACKGROUND_COLOR,
136                                 Messages.getString(getClass(), "backgroundColor"), //$NON-NLS-1$
137                                 main);  
138
139                 Label emptyLabel = new Label(main, SWT.NULL);
140                 emptyLabel.setText("");
141                 
142                 createEmptyRow(main);
143                 
144                 this.textColorEditor =
145                         new ColorFieldEditor(
146                                 PluginPreferences.TEXT_COLOR,
147                                 Messages.getString(getClass(), "defaultTextColor"), //$NON-NLS-1$
148                                 main);                          
149
150                 this.boldText = new Button(main, SWT.CHECK);
151                 this.boldText.setSelection(textFlag);
152                 this.boldText.setText(Messages.getString(getClass(), "bold")); //$NON-NLS-1$
153                 this.boldText.addSelectionListener(new SelectionListener() {
154                         public void widgetDefaultSelected(SelectionEvent e) {
155                         }
156                         public void widgetSelected(SelectionEvent e) {
157                                 textFlag = boldText.getSelection();
158                         }
159                 });
160
161                 keywordColorEditor =
162                         new ColorFieldEditor(
163                                 PluginPreferences.KEYWORD_COLOR,
164                                 Messages.getString(getClass(), "keywordTextColor"), //$NON-NLS-1$
165                                 main);
166
167                 boldKeyword = new Button(main, SWT.CHECK);
168                 boldKeyword.setSelection(keywordFlag);
169                 boldKeyword.setText(Messages.getString(getClass(), "bold")); //$NON-NLS-1$
170                 boldKeyword.addSelectionListener(new SelectionListener() {
171                         public void widgetDefaultSelected(SelectionEvent e) {
172                         }
173                         public void widgetSelected(SelectionEvent e) {
174                                 keywordFlag = boldKeyword.getSelection();
175                         }
176                 });
177
178                 commentColorEditor =
179                         new ColorFieldEditor(
180                                 PluginPreferences.COMMENT_COLOR,
181                                 Messages.getString(getClass(), "commentTextColor"), //$NON-NLS-1$
182                                 main);
183
184                 boldComment = new Button(main, SWT.CHECK);
185                 boldComment.setSelection(commentFlag);
186                 boldComment.setText(Messages.getString(getClass(), "bold")); //$NON-NLS-1$
187                 boldComment.addSelectionListener(new SelectionListener() {
188                         public void widgetDefaultSelected(SelectionEvent e) {
189                         }
190                         public void widgetSelected(SelectionEvent e) {
191                                 commentFlag = boldComment.getSelection();
192                         }
193                 });
194
195                 stringColorEditor =
196                         new ColorFieldEditor(
197                                 PluginPreferences.STRING_COLOR,
198                                 Messages.getString(getClass(), "stringTextColor"), //$NON-NLS-1$
199                                 main);
200
201                 boldString = new Button(main, SWT.CHECK);
202                 boldString.setSelection(stringFlag);
203                 boldString.setText(Messages.getString(getClass(), "bold")); //$NON-NLS-1$
204                 boldString.addSelectionListener(new SelectionListener() {
205                         public void widgetDefaultSelected(SelectionEvent e) {
206                         }
207                         public void widgetSelected(SelectionEvent e) {
208                                 stringFlag = boldString.getSelection();
209                         }
210                 });
211
212                 numericColorEditor =
213                         new ColorFieldEditor(
214                                 PluginPreferences.NUMERIC_COLOR,
215                                 Messages.getString(getClass(), "numericTextColor"), //$NON-NLS-1$
216                                 main);
217
218                 boldNumeric = new Button(main, SWT.CHECK);
219                 boldNumeric.setSelection(numericFlag);
220                 boldNumeric.setText(Messages.getString(getClass(), "bold")); //$NON-NLS-1$
221                 boldNumeric.addSelectionListener(new SelectionListener() {
222                         public void widgetDefaultSelected(SelectionEvent e) {
223                         }
224                         public void widgetSelected(SelectionEvent e) {
225                                 numericFlag = boldNumeric.getSelection();
226                         }
227                 });
228
229                 backgroundColorEditor.setPreferencePage(this);
230                 backgroundColorEditor.setPreferenceStore(getPreferenceStore());
231                 backgroundColorEditor.load();
232                 
233                 textColorEditor.setPreferencePage(this);
234                 textColorEditor.setPreferenceStore(getPreferenceStore());
235                 textColorEditor.load();
236                 
237                 keywordColorEditor.setPreferencePage(this);
238                 keywordColorEditor.setPreferenceStore(getPreferenceStore());
239                 keywordColorEditor.load();
240                 
241                 commentColorEditor.setPreferencePage(this);
242                 commentColorEditor.setPreferenceStore(getPreferenceStore());
243                 commentColorEditor.load();
244                 
245                 stringColorEditor.setPreferencePage(this);
246                 stringColorEditor.setPreferenceStore(getPreferenceStore());
247                 stringColorEditor.load();
248
249                 numericColorEditor.setPreferencePage(this);
250                 numericColorEditor.setPreferenceStore(getPreferenceStore());
251                 numericColorEditor.load();
252                 
253                 // now for the switcheroo...
254                 // reset the number of columns to 3
255                 GridLayout innerLayout = new GridLayout();
256                 innerLayout.numColumns = 3;
257                 main.setLayout(innerLayout);
258     }
259
260     /**
261      * @param main
262      */
263     private void createFontSelectionArea(Composite main) {
264         Group group = new Group(main, SWT.NONE);
265         group.setText(Messages.getString(getClass(), "font"));
266         GridLayout innerLayout = new GridLayout();
267                 innerLayout.numColumns = 2;
268                 group.setLayout(innerLayout);
269                 group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
270
271                 fontDisplay = new Label(group, SWT.NULL);
272                 GridData data = new GridData(GridData.FILL_HORIZONTAL);
273                 data.horizontalSpan = 2;
274                 fontDisplay.setLayoutData(data);
275                 
276                 fontData = PreferenceConverter.getFontData(getPreferenceStore(), "quantum.font"); //$NON-NLS-1$
277                 fontDialog = new FontDialog(getShell());
278                 Button fontButton = new Button(group, SWT.PUSH);
279                 fontButton.setText(Messages.getString(getClass(), "pickFont")); //$NON-NLS-1$
280                 fontButton.addSelectionListener(new SelectionListener() {
281                         public void widgetDefaultSelected(SelectionEvent e) {
282                         }
283                         public void widgetSelected(SelectionEvent e) {
284                                 if (fontData != null) {
285                     VersioningHelper.setFont(fontDialog, new FontData[] { fontData} );
286                                 }
287                                 FontData data = fontDialog.open();
288                                 if (data != null) {
289                                         fontData = data;
290                                         updateFontDisplay();
291                                 }
292                         }
293                 });
294                 Button defaultButton = new Button(group, SWT.PUSH);
295                 defaultButton.setText(Messages.getString(getClass(), "defaultFont")); //$NON-NLS-1$
296                 defaultButton.addSelectionListener(new SelectionListener() {
297                         public void widgetDefaultSelected(SelectionEvent e) {
298                         }
299                         public void widgetSelected(SelectionEvent e) {
300                                 fontData = PluginPreferences.getDefaultFont();
301                                 updateFontDisplay();
302                         }
303                 });
304
305                 updateFontDisplay();
306     }
307
308     /**
309      * @param main
310      */
311     private void createEmptyRow(Composite main) {
312         Label emptyLabel = new Label(main, SWT.NULL);
313                 emptyLabel.setText("");
314                 GridData gridData = new GridData();
315                 gridData.horizontalSpan = 3;
316                 emptyLabel.setLayoutData(gridData);
317     }
318
319     protected void updateFontDisplay() {
320                 if (fontData == null) {
321                         fontDisplay.setText(Messages.getString(getClass(), "default")); //$NON-NLS-1$
322                 } else {
323                         Object[] parameters = new Object[] { 
324                                         fontData.getName(), new Integer(fontData.getHeight()) };
325                         String style = Messages.getString(getClass(), "regularFont", parameters); //$NON-NLS-1$
326                         if (fontData.getStyle() == SWT.BOLD) {
327                                 style = Messages.getString(getClass(), "boldFont", parameters); //$NON-NLS-1$
328                         } else if (fontData.getStyle() == SWT.ITALIC) {
329                                 style = Messages.getString(getClass(), "italicFont", parameters); //$NON-NLS-1$
330                         } else if (fontData.getStyle() == (SWT.BOLD | SWT.ITALIC)) {
331                                 style = Messages.getString(getClass(), "boldItalicFont", parameters); //$NON-NLS-1$
332                         }
333                         fontDisplay.setText(style); //$NON-NLS-1$
334                 }
335         }
336         protected void updateFlags() {
337                 this.boldText.setSelection(this.textFlag);
338                 this.boldKeyword.setSelection(this.keywordFlag);
339                 this.boldString.setSelection(this.stringFlag);
340                 this.boldNumeric.setSelection(this.numericFlag);
341                 this.boldComment.setSelection(this.commentFlag);
342         }
343 }