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
9 * Christopher Lenz - initial API and implementation
11 * $Id: CssEditorSyntaxConfigurationBlock.java,v 1.1 2004-09-02 18:11:50 jsurfer Exp $
14 package net.sourceforge.phpeclipse.css.ui.internal.preferences;
16 import net.sourceforge.phpeclipse.css.ui.internal.CssUIMessages;
17 import net.sourceforge.phpeclipse.css.ui.internal.CssUIPreferences;
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;
39 final class CssEditorSyntaxConfigurationBlock
40 extends AbstractConfigurationBlock {
42 // Instance Variables ------------------------------------------------------
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 },
68 private ColorSelector fColorSelector;
69 private Button fBoldCheckbox;
71 // Constructors ------------------------------------------------------------
73 public CssEditorSyntaxConfigurationBlock(IPreferenceStore store) {
77 // Public Methods ----------------------------------------------------------
79 public Control createControl(Composite parent) {
81 Composite composite = new Composite(parent, SWT.NONE);
82 composite.setLayout(new GridLayout());
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) {
91 fBackgroundCustomRadioButton.getSelection();
92 fBackgroundColorSelector.setEnabled(custom);
93 getPreferenceStore().setValue(
94 CssUIPreferences.EDITOR_BACKGROUND_DEFAULT_COLOR,
97 public void widgetDefaultSelected(SelectionEvent e) { }
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);
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());
123 Label label = new Label(composite, SWT.LEFT);
124 label.setText(getString("foregroundColor")); //$NON-NLS-1$
125 label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
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);
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) {
144 public void widgetSelected(SelectionEvent e) {
145 handleSyntaxColorListSelection();
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));
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);
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());
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());
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);
207 * @see AbstractConfigurationBlock#initializeFields()
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);
221 // Event Handlers ----------------------------------------------------------
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));
232 // Private Methods ---------------------------------------------------------
234 private static String getString(String key) {
235 return CssUIMessages.getString(
236 "CssEditorPreferencePage.syntax." + key); //$NON-NLS-1$
239 private void initialize() {
240 for (int i = 0; i < fColorListModel.length; i++) {
241 fColorList.add(fColorListModel[i][0]);
243 fColorList.getDisplay().asyncExec(new Runnable() {
245 if ((fColorList != null)
246 && !fColorList.isDisposed()) {
247 fColorList.select(0);
248 handleSyntaxColorListSelection();