1 package net.sourceforge.phpeclipse;
3 import java.util.HashMap;
4 import java.util.Iterator;
7 import net.sourceforge.phpeclipse.preferences.ColorEditor;
8 import net.sourceforge.phpeclipse.preferences.OverlayPreferenceStore;
9 import net.sourceforge.phpeclipse.preferences.PHPPreferencesMessages;
10 import org.eclipse.core.runtime.IStatus;
11 import org.eclipse.jface.preference.BooleanFieldEditor;
12 import org.eclipse.jface.preference.FileFieldEditor;
13 import org.eclipse.jface.preference.IPreferenceStore;
14 import org.eclipse.jface.preference.PreferenceConverter;
15 import org.eclipse.jface.preference.PreferencePage;
16 import org.eclipse.jface.text.source.ISourceViewer;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.custom.StyledText;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.events.SelectionListener;
21 import org.eclipse.swt.graphics.Color;
22 import org.eclipse.swt.graphics.RGB;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Display;
29 import org.eclipse.swt.widgets.Group;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.List;
32 import org.eclipse.ui.IWorkbench;
33 import org.eclipse.ui.IWorkbenchPreferencePage;
36 * The preference page for setting the PHP Editor options.
38 public class PHPSyntaxEditorPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
40 public final OverlayPreferenceStore.OverlayKey[] Keys =
41 new OverlayPreferenceStore.OverlayKey[] {
42 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_MULTILINE_COMMENT),
43 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_MULTILINE_COMMENT_BOLD),
44 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_MULTILINE_COMMENT_ITALIC),
45 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_MULTILINE_COMMENT_UNDERLINE),
46 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_SINGLELINE_COMMENT),
47 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_SINGLELINE_COMMENT_BOLD),
48 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_SINGLELINE_COMMENT_ITALIC),
49 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_SINGLELINE_COMMENT_UNDERLINE),
50 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_KEYWORD),
51 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_KEYWORD_BOLD),
52 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_KEYWORD_ITALIC),
53 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_KEYWORD_UNDERLINE),
54 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_VARIABLE),
55 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_VARIABLE_BOLD),
56 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_VARIABLE_ITALIC),
57 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_VARIABLE_UNDERLINE),
58 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_TYPE),
59 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_TYPE_BOLD),
60 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_TYPE_ITALIC),
61 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_TYPE_UNDERLINE),
62 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_CONSTANT),
63 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_CONSTANT_BOLD),
64 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_CONSTANT_ITALIC),
65 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_CONSTANT_UNDERLINE),
66 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_FUNCTIONNAME),
67 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_FUNCTIONNAME_BOLD),
68 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_FUNCTIONNAME_ITALIC),
69 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_FUNCTIONNAME_UNDERLINE),
70 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_STRING),
71 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_STRING_BOLD),
72 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_STRING_ITALIC),
73 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_STRING_UNDERLINE),
74 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_DEFAULT),
75 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_DEFAULT_BOLD),
76 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_DEFAULT_ITALIC),
77 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_DEFAULT_UNDERLINE),
78 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_USERDEF_XMLFILE)};
80 private final String[][] SyntaxColorListModel =
83 PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.multiLineComment"),
84 IPreferenceConstants.PHP_MULTILINE_COMMENT },
86 PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.singleLineComment"),
87 IPreferenceConstants.PHP_SINGLELINE_COMMENT },
89 PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.keywords"), IPreferenceConstants.PHP_KEYWORD }, {
90 PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.variables"), IPreferenceConstants.PHP_VARIABLE }, {
91 PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.types"), IPreferenceConstants.PHP_TYPE }, {
92 PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.functions"), IPreferenceConstants.PHP_FUNCTIONNAME }, {
93 PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.constants"), IPreferenceConstants.PHP_CONSTANT }, {
94 PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.strings"), IPreferenceConstants.PHP_STRING }, {
95 PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.others"), IPreferenceConstants.PHP_DEFAULT }
98 private OverlayPreferenceStore OverlayStore;
100 private Map ColorButtons = new HashMap();
101 private SelectionListener ColorButtonListener = new SelectionListener() {
102 public void widgetDefaultSelected(SelectionEvent e) {
104 public void widgetSelected(SelectionEvent e) {
105 ColorEditor editor = (ColorEditor) e.widget.getData();
106 PreferenceConverter.setValue(OverlayStore, (String) ColorButtons.get(editor), editor.getColorValue());
110 private Map CheckBoxes = new HashMap();
111 private SelectionListener CheckBoxListener = new SelectionListener() {
112 public void widgetDefaultSelected(SelectionEvent e) {
114 public void widgetSelected(SelectionEvent e) {
115 Button button = (Button) e.widget;
116 OverlayStore.setValue((String) CheckBoxes.get(button), button.getSelection());
120 private List SyntaxColorList;
121 private ColorEditor SyntaxForegroundColorEditor;
122 private Button BoldCheckBox;
123 private Button ItalicCheckBox;
124 private Button UnderlineCheckBox;
125 private FileFieldEditor userdefPHPSyntaxFileFFE;
127 public PHPSyntaxEditorPreferencePage() {
128 setDescription(PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.description")); //$NON-NLS-1$
129 setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore());
130 OverlayStore = new OverlayPreferenceStore(getPreferenceStore(), Keys);
133 public void init(IWorkbench workbench) {
136 public void createControl(Composite parent) {
137 super.createControl(parent);
140 private void handleSyntaxColorListSelection() {
141 int i = SyntaxColorList.getSelectionIndex();
142 String key = SyntaxColorListModel[i][1];
143 RGB rgb = PreferenceConverter.getColor(OverlayStore, key);
144 SyntaxForegroundColorEditor.setColorValue(rgb);
145 BoldCheckBox.setSelection(OverlayStore.getBoolean(key + "_bold"));
146 ItalicCheckBox.setSelection(OverlayStore.getBoolean(key + "_italic"));
147 UnderlineCheckBox.setSelection(OverlayStore.getBoolean(key + "_underline"));
150 private Control createSyntaxPage(Composite parent) {
152 Composite colorComposite = new Composite(parent, SWT.NULL);
153 colorComposite.setLayout(new GridLayout());
155 Label label = new Label(colorComposite, SWT.LEFT);
156 label.setText(PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.syntax")); //$NON-NLS-1$
157 label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
159 Composite editorComposite = new Composite(colorComposite, SWT.NONE);
160 GridLayout layout = new GridLayout();
161 layout.numColumns = 2;
162 layout.marginHeight = 0;
163 layout.marginWidth = 0;
164 editorComposite.setLayout(layout);
165 GridData gd = new GridData(GridData.FILL_BOTH);
166 editorComposite.setLayoutData(gd);
168 SyntaxColorList = new List(editorComposite, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER);
169 gd = new GridData(GridData.FILL_BOTH);
170 gd.heightHint = convertHeightInCharsToPixels(5);
171 SyntaxColorList.setLayoutData(gd);
173 Composite stylesComposite = new Composite(editorComposite, SWT.NONE);
174 layout = new GridLayout();
175 layout.marginHeight = 0;
176 layout.marginWidth = 0;
177 layout.numColumns = 2;
178 stylesComposite.setLayout(layout);
179 stylesComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
181 label = new Label(stylesComposite, SWT.LEFT);
182 label.setText(PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.color")); //$NON-NLS-1$
184 gd.horizontalAlignment = GridData.BEGINNING;
185 label.setLayoutData(gd);
187 SyntaxForegroundColorEditor = new ColorEditor(stylesComposite);
188 Button foregroundColorButton = SyntaxForegroundColorEditor.getButton();
189 gd = new GridData(GridData.FILL_HORIZONTAL);
190 gd.horizontalAlignment = GridData.BEGINNING;
191 foregroundColorButton.setLayoutData(gd);
193 BoldCheckBox = new Button(stylesComposite, SWT.CHECK);
194 BoldCheckBox.setText(PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.bold")); //$NON-NLS-1$
195 gd = new GridData(GridData.FILL_HORIZONTAL);
196 gd.horizontalAlignment = GridData.BEGINNING;
197 gd.horizontalSpan = 2;
198 BoldCheckBox.setLayoutData(gd);
200 ItalicCheckBox = new Button(stylesComposite, SWT.CHECK);
201 ItalicCheckBox.setText(PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.italic")); //$NON-NLS-1$
202 ItalicCheckBox.setEnabled(false);
203 gd = new GridData(GridData.FILL_HORIZONTAL);
204 gd.horizontalAlignment = GridData.BEGINNING;
205 gd.horizontalSpan = 2;
206 ItalicCheckBox.setLayoutData(gd);
208 UnderlineCheckBox = new Button(stylesComposite, SWT.CHECK);
209 UnderlineCheckBox.setText(PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.underline")); //$NON-NLS-1$
210 UnderlineCheckBox.setEnabled(false);
211 gd = new GridData(GridData.FILL_HORIZONTAL);
212 gd.horizontalAlignment = GridData.BEGINNING;
213 gd.horizontalSpan = 2;
214 UnderlineCheckBox.setLayoutData(gd);
216 Composite customSyntaxComposite = new Composite(colorComposite, SWT.NONE);
217 layout = new GridLayout();
218 layout.marginHeight = 0;
219 layout.marginWidth = 0;
220 layout.numColumns = 3;
221 stylesComposite.setLayout(layout);
222 stylesComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
224 userdefPHPSyntaxFileFFE =
226 IPreferenceConstants.PHP_USERDEF_XMLFILE,
227 PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.syntaxdialog"),
229 userdefPHPSyntaxFileFFE.setPreferencePage(this);
230 userdefPHPSyntaxFileFFE.setPreferenceStore(getPreferenceStore());
231 userdefPHPSyntaxFileFFE.load();
233 SyntaxColorList.addSelectionListener(new SelectionListener() {
234 public void widgetDefaultSelected(SelectionEvent e) {
237 public void widgetSelected(SelectionEvent e) {
238 handleSyntaxColorListSelection();
242 foregroundColorButton.addSelectionListener(new SelectionListener() {
243 public void widgetDefaultSelected(SelectionEvent e) {
246 public void widgetSelected(SelectionEvent e) {
247 int i = SyntaxColorList.getSelectionIndex();
248 String key = SyntaxColorListModel[i][1];
250 PreferenceConverter.setValue(OverlayStore, key, SyntaxForegroundColorEditor.getColorValue());
253 BoldCheckBox.addSelectionListener(new SelectionListener() {
254 public void widgetDefaultSelected(SelectionEvent e) {
257 public void widgetSelected(SelectionEvent e) {
258 int i = SyntaxColorList.getSelectionIndex();
259 String key = SyntaxColorListModel[i][1];
260 OverlayStore.setValue(key + "_bold", BoldCheckBox.getSelection());
264 ItalicCheckBox.addSelectionListener(new SelectionListener() {
265 public void widgetDefaultSelected(SelectionEvent e) {
268 public void widgetSelected(SelectionEvent e) {
269 int i = SyntaxColorList.getSelectionIndex();
270 String key = SyntaxColorListModel[i][1];
271 OverlayStore.setValue(key + "_italic", ItalicCheckBox.getSelection());
275 UnderlineCheckBox.addSelectionListener(new SelectionListener() {
276 public void widgetDefaultSelected(SelectionEvent e) {
279 public void widgetSelected(SelectionEvent e) {
280 int i = SyntaxColorList.getSelectionIndex();
281 String key = SyntaxColorListModel[i][1];
282 OverlayStore.setValue(key + "_underline", UnderlineCheckBox.getSelection());
285 return colorComposite;
288 private void initializeViewerColors(ISourceViewer viewer) {
290 IPreferenceStore store = OverlayStore;
293 StyledText styledText = viewer.getTextWidget();
297 private Color createColor(IPreferenceStore store, String key, Display display) {
299 if (store.contains(key)) {
300 if (store.isDefault(key))
301 rgb = PreferenceConverter.getDefaultColor(store, key);
303 rgb = PreferenceConverter.getColor(store, key);
305 return new Color(display, rgb);
310 private static void setEnabled(Control control, boolean enable) {
311 control.setEnabled(enable);
312 if (control instanceof Composite) {
313 Composite composite = (Composite) control;
314 Control[] children = composite.getChildren();
315 for (int i = 0; i < children.length; i++)
316 setEnabled(children[i], enable);
320 private static void indent(Control control) {
321 GridData gridData = new GridData();
322 gridData.horizontalIndent = 20;
323 control.setLayoutData(gridData);
326 private static void createDependency(final Button master, final Control slave) {
328 master.addSelectionListener(new SelectionListener() {
329 public void widgetSelected(SelectionEvent e) {
330 slave.setEnabled(master.getSelection());
333 public void widgetDefaultSelected(SelectionEvent e) {
338 protected Control createContents(Composite parent) {
340 OverlayStore.start();
341 //Create overall composite
342 Composite composite = new Composite(parent, SWT.NONE);
343 GridLayout layout = new GridLayout();
344 layout.marginHeight = 0;
345 layout.marginWidth = 0;
346 composite.setLayout(layout);
347 Composite syntaxComposite = new Composite(composite, SWT.NULL);
348 syntaxComposite.setLayout(new GridLayout());
349 layout = new GridLayout();
350 layout.numColumns = 3;
351 Group syntaxGroup = new Group(syntaxComposite, SWT.NONE);
352 syntaxGroup.setText(PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.foreground"));
353 syntaxGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
354 syntaxGroup.setLayout(layout);
355 createSyntaxPage(syntaxGroup);
361 private void initialize() {
363 for (int i = 0; i < SyntaxColorListModel.length; i++)
364 SyntaxColorList.add(SyntaxColorListModel[i][0]);
365 SyntaxColorList.getDisplay().asyncExec(new Runnable() {
367 if (SyntaxColorList != null && !SyntaxColorList.isDisposed()) {
368 SyntaxColorList.select(0);
369 handleSyntaxColorListSelection();
375 private void initializeFields() {
377 Iterator e = ColorButtons.keySet().iterator();
378 while (e.hasNext()) {
379 ColorEditor c = (ColorEditor) e.next();
380 String key = (String) ColorButtons.get(c);
381 RGB rgb = PreferenceConverter.getColor(OverlayStore, key);
382 c.setColorValue(rgb);
385 e = CheckBoxes.keySet().iterator();
386 while (e.hasNext()) {
387 Button b = (Button) e.next();
388 String key = (String) CheckBoxes.get(b);
389 b.setSelection(OverlayStore.getBoolean(key));
393 public boolean performOk() {
394 OverlayStore.propagate();
395 IPreferenceStore store = getPreferenceStore();
396 PHPeclipsePlugin.getDefault().savePluginPreferences();
397 userdefPHPSyntaxFileFFE.store();
401 protected void performDefaults() {
402 OverlayStore.loadDefaults();
404 handleSyntaxColorListSelection();
405 userdefPHPSyntaxFileFFE.loadDefault();
406 super.performDefaults();
409 public void dispose() {
410 if (OverlayStore != null) {
417 private Control addColorButton(Composite composite, String label, String key, int indentation) {
418 Label labelControl = new Label(composite, SWT.NONE);
419 labelControl.setText(label);
420 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
421 gd.horizontalIndent = indentation;
422 labelControl.setLayoutData(gd);
423 ColorEditor editor = new ColorEditor(composite);
424 Button button = editor.getButton();
425 button.setData(editor);
426 gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
427 button.setLayoutData(gd);
428 button.addSelectionListener(ColorButtonListener);
429 ColorButtons.put(editor, key);
433 private Button addCheckBox(Composite parent, String label, String key, int indentation) {
434 Button checkBox = new Button(parent, SWT.CHECK);
435 checkBox.setText(label);
436 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
437 gd.horizontalIndent = indentation;
438 gd.horizontalSpan = 2;
439 checkBox.setLayoutData(gd);
440 checkBox.addSelectionListener(CheckBoxListener);
441 CheckBoxes.put(checkBox, key);
445 private void updateStatus(IStatus status) {
449 * @deprecated Inline to avoid reference to preference page
451 public static boolean indicateQuixFixableProblems() {
452 // return PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_CORRECTION_INDICATION);
457 * @deprecated Inline to avoid reference to preference page
459 static public boolean synchronizeOutlineOnCursorMove() {
460 // return PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE);