intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.css.ui / src / net / sourceforge / phpeclipse / css / ui / internal / preferences / CssEditorSyntaxConfigurationBlock.java
1 /*
2  * Copyright (c) 2003-2004 Christopher Lenz and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     Christopher Lenz - initial API and implementation
10  * 
11  * $Id: CssEditorSyntaxConfigurationBlock.java,v 1.1 2004-09-02 18:11:50 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.css.ui.internal.preferences;
15
16 import net.sourceforge.phpeclipse.css.ui.internal.CssUIMessages;
17 import net.sourceforge.phpeclipse.css.ui.internal.CssUIPreferences;
18
19 import org.eclipse.jface.preference.ColorSelector;
20 import org.eclipse.jface.preference.IPreferenceStore;
21 import org.eclipse.jface.preference.PreferenceConverter;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.events.SelectionListener;
25 import org.eclipse.swt.graphics.RGB;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.layout.RowLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Group;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.List;
35
36 /**
37  * 
38  */
39 final class CssEditorSyntaxConfigurationBlock
40         extends AbstractConfigurationBlock {
41
42         // Instance Variables ------------------------------------------------------
43
44         private Button fBackgroundDefaultRadioButton;
45         private Button fBackgroundCustomRadioButton;
46         private ColorSelector fBackgroundColorSelector;
47         private List fColorList;
48         private final String[][] fColorListModel = new String[][] {
49                 { getString("commentColor"), //$NON-NLS-1$
50                   CssUIPreferences.EDITOR_COMMENT_COLOR,
51                   CssUIPreferences.EDITOR_COMMENT_BOLD },
52                 { getString("stringColor"), //$NON-NLS-1$
53                   CssUIPreferences.EDITOR_STRING_COLOR,
54                   CssUIPreferences.EDITOR_STRING_BOLD },
55                 { getString("propertyNameColor"), //$NON-NLS-1$
56                   CssUIPreferences.EDITOR_PROPERTY_COLOR,
57                   CssUIPreferences.EDITOR_PROPERTY_BOLD },
58                 { getString("atKeywordColor"), //$NON-NLS-1$
59                   CssUIPreferences.EDITOR_AT_KEYWORD_COLOR,
60                   CssUIPreferences.EDITOR_AT_KEYWORD_BOLD },
61                 { getString("pseudoClassColor"), //$NON-NLS-1$
62                   CssUIPreferences.EDITOR_PSEUDO_CLASS_COLOR,
63                   CssUIPreferences.EDITOR_PSEUDO_CLASS_BOLD },
64                 { getString("defaultColor"), //$NON-NLS-1$
65                   CssUIPreferences.EDITOR_DEFAULT_COLOR,
66                   CssUIPreferences.EDITOR_DEFAULT_BOLD },
67         };
68         private ColorSelector fColorSelector;
69         private Button fBoldCheckbox;
70
71         // Constructors ------------------------------------------------------------
72
73         public CssEditorSyntaxConfigurationBlock(IPreferenceStore store) {
74                 super(store);
75         }
76
77         // Public Methods ----------------------------------------------------------
78
79         public Control createControl(Composite parent) {
80
81                 Composite composite = new Composite(parent, SWT.NONE);
82                 composite.setLayout(new GridLayout());
83
84                 Group backgroundComposite = new Group(composite, SWT.SHADOW_ETCHED_IN);
85                 backgroundComposite.setLayout(new RowLayout());
86                 backgroundComposite.setText(getString("backgroundColor")); //$NON-NLS-1$
87                 SelectionListener backgroundSelectionListener =
88                         new SelectionListener() {
89                                 public void widgetSelected(SelectionEvent e) {                          
90                                         boolean custom =
91                                                 fBackgroundCustomRadioButton.getSelection();
92                                         fBackgroundColorSelector.setEnabled(custom);
93                                         getPreferenceStore().setValue(
94                                                 CssUIPreferences.EDITOR_BACKGROUND_DEFAULT_COLOR,
95                                                 !custom);
96                                 }
97                                 public void widgetDefaultSelected(SelectionEvent e) { }
98                         };
99                 fBackgroundDefaultRadioButton = new Button(backgroundComposite,
100                         SWT.RADIO | SWT.LEFT);
101                 fBackgroundDefaultRadioButton.setText(getString(
102                         "backgroundColorSystemDefault")); //$NON-NLS-1$
103                 fBackgroundDefaultRadioButton.addSelectionListener(
104                         backgroundSelectionListener);
105                 fBackgroundCustomRadioButton = new Button(backgroundComposite,
106                         SWT.RADIO | SWT.LEFT);
107                 fBackgroundCustomRadioButton.setText(getString(
108                         "backgroundColorCustom")); //$NON-NLS-1$
109                 fBackgroundCustomRadioButton.addSelectionListener(
110                         backgroundSelectionListener);
111
112                 fBackgroundColorSelector = new ColorSelector(backgroundComposite);
113                 Button backgroundColorButton = fBackgroundColorSelector.getButton();
114                 backgroundColorButton.addSelectionListener(new SelectionListener() {
115                         public void widgetDefaultSelected(SelectionEvent e) { }
116                         public void widgetSelected(SelectionEvent e) {
117                                 PreferenceConverter.setValue(getPreferenceStore(),
118                                         CssUIPreferences.EDITOR_BACKGROUND_COLOR,
119                                         fBackgroundColorSelector.getColorValue());
120                         }
121                 });
122
123                 Label label = new Label(composite, SWT.LEFT);
124                 label.setText(getString("foregroundColor")); //$NON-NLS-1$
125                 label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
126
127                 Composite editorComposite = new Composite(composite, SWT.NONE);
128                 GridLayout layout = new GridLayout();
129                 layout.numColumns = 2;
130                 layout.marginHeight = 0;
131                 layout.marginWidth = 0;
132                 editorComposite.setLayout(layout);
133                 GridData gridData = new GridData(GridData.FILL_BOTH);
134                 editorComposite.setLayoutData(gridData);          
135
136                 fColorList = new List(editorComposite,
137                         SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER);
138                 gridData = new GridData(GridData.FILL_BOTH);
139                 gridData.heightHint = convertHeightInCharsToPixels(composite, 5);
140                 fColorList.setLayoutData(gridData);
141                 fColorList.addSelectionListener(new SelectionListener() {
142                         public void widgetDefaultSelected(SelectionEvent e) {
143                         }
144                         public void widgetSelected(SelectionEvent e) {
145                                 handleSyntaxColorListSelection();
146                         }
147                 });
148
149                 Composite stylesComposite = new Composite(editorComposite, SWT.NONE);
150                 layout = new GridLayout();
151                 layout.marginHeight = 0;
152                 layout.marginWidth = 0;
153                 layout.numColumns = 2;
154                 stylesComposite.setLayout(layout);
155                 stylesComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
156                 
157                 label = new Label(stylesComposite, SWT.LEFT);
158                 label.setText(getString("color")); //$NON-NLS-1$
159                 gridData = new GridData();
160                 gridData.horizontalAlignment = GridData.BEGINNING;
161                 label.setLayoutData(gridData);
162
163                 fColorSelector = new ColorSelector(stylesComposite);
164                 Button foregroundColorButton = fColorSelector.getButton();
165                 gridData = new GridData(GridData.FILL_HORIZONTAL);
166                 gridData.horizontalAlignment = GridData.BEGINNING;
167                 foregroundColorButton.setLayoutData(gridData);
168                 foregroundColorButton.addSelectionListener(new SelectionListener() {
169                         public void widgetDefaultSelected(SelectionEvent e) { }
170                         public void widgetSelected(SelectionEvent e) {
171                                 int i = fColorList.getSelectionIndex();
172                                 String key = fColorListModel[i][1];
173                                 PreferenceConverter.setValue(getPreferenceStore(), key,
174                                         fColorSelector.getColorValue());
175                         }
176                 });
177                 
178                 fBoldCheckbox = new Button(stylesComposite, SWT.CHECK);
179                 fBoldCheckbox.setText(getString("bold")); //$NON-NLS-1$
180                 gridData = new GridData(GridData.FILL_HORIZONTAL);
181                 gridData.horizontalAlignment = GridData.BEGINNING;
182                 gridData.horizontalSpan = 2;
183                 fBoldCheckbox.setLayoutData(gridData);
184                 fBoldCheckbox.addSelectionListener(new SelectionListener() {
185                         public void widgetDefaultSelected(SelectionEvent e) { }
186                         public void widgetSelected(SelectionEvent e) {
187                                 int i = fColorList.getSelectionIndex();
188                                 String key = fColorListModel[i][2];
189                                 getPreferenceStore().setValue(key,
190                                         fBoldCheckbox.getSelection());
191                         }
192                 });
193
194                 Control previewer =
195                         new SyntaxPreviewer(composite, getPreferenceStore()).getControl();
196                 gridData = new GridData(GridData.FILL_BOTH);
197                 gridData.widthHint = convertWidthInCharsToPixels(composite, 20);
198                 gridData.heightHint = convertHeightInCharsToPixels(composite, 5);
199                 previewer.setLayoutData(gridData);
200
201                 initialize();
202
203                 return composite;
204         }
205
206         /**
207          * @see AbstractConfigurationBlock#initializeFields()
208          */
209         public void initializeFields() {
210                 super.initializeFields();
211                 RGB rgb = PreferenceConverter.getColor(getPreferenceStore(),
212                         CssUIPreferences.EDITOR_BACKGROUND_COLOR);
213                 fBackgroundColorSelector.setColorValue(rgb);
214                 boolean defaultBackgroud = getPreferenceStore().getBoolean(
215                         CssUIPreferences.EDITOR_BACKGROUND_DEFAULT_COLOR);
216                 fBackgroundDefaultRadioButton.setSelection(defaultBackgroud);
217                 fBackgroundCustomRadioButton.setSelection(!defaultBackgroud);
218                 fBackgroundColorSelector.setEnabled(!defaultBackgroud);
219         }
220
221         // Event Handlers ----------------------------------------------------------
222
223         void handleSyntaxColorListSelection() {
224                 int i = fColorList.getSelectionIndex();
225                 String key = fColorListModel[i][1];
226                 RGB rgb = PreferenceConverter.getColor(getPreferenceStore(), key);
227                 fColorSelector.setColorValue(rgb);
228                 key = fColorListModel[i][2];
229                 fBoldCheckbox.setSelection(getPreferenceStore().getBoolean(key));
230         }
231
232         // Private Methods ---------------------------------------------------------
233
234         private static String getString(String key) {
235                 return CssUIMessages.getString(
236                         "CssEditorPreferencePage.syntax." + key); //$NON-NLS-1$
237         }
238
239         private void initialize() {
240                 for (int i = 0; i < fColorListModel.length; i++) {
241                         fColorList.add(fColorListModel[i][0]);
242                 }
243                 fColorList.getDisplay().asyncExec(new Runnable() {
244                         public void run() {
245                                 if ((fColorList != null)
246                                  && !fColorList.isDisposed()) {
247                                         fColorList.select(0);
248                                         handleSyntaxColorListSelection();
249                                 }
250                         }
251                 });
252                 initializeFields();
253         }
254
255 }