2 * Copyright (c) 2002-2004 Roberto Gonzalez Rocha 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 * Roberto Gonzalez Rocha - Initial version
10 * Igor Malinin - refactoring, minor changes
12 * $Id: XMLSyntaxPreferencePage.java,v 1.3 2006-10-21 23:14:13 pombredanne Exp $
15 package net.sourceforge.phpeclipse.xml.ui.internal.preferences;
17 import java.io.BufferedReader;
18 import java.io.IOException;
19 import java.io.InputStreamReader;
21 import net.sourceforge.phpeclipse.ui.ColorEditor;
22 import net.sourceforge.phpeclipse.ui.preferences.ITextStylePreferences;
23 import net.sourceforge.phpeclipse.ui.preferences.OverlayPreferenceStore;
24 import net.sourceforge.phpeclipse.ui.preferences.PreferenceDescriptor;
25 import net.sourceforge.phpeclipse.xml.ui.XMLPlugin;
26 import net.sourceforge.phpeclipse.xml.ui.internal.text.XMLConfiguration;
27 import net.sourceforge.phpeclipse.xml.ui.text.IXMLSyntaxConstants;
28 import net.sourceforge.phpeclipse.xml.ui.text.XMLTextTools;
30 import org.eclipse.jface.preference.IPreferenceStore;
31 import org.eclipse.jface.preference.PreferenceConverter;
32 import org.eclipse.jface.preference.PreferencePage;
33 import org.eclipse.jface.resource.JFaceResources;
34 import org.eclipse.jface.text.Document;
35 import org.eclipse.jface.text.IDocument;
36 import org.eclipse.jface.text.IDocumentPartitioner;
37 import org.eclipse.jface.text.source.ISourceViewer;
38 import org.eclipse.jface.text.source.SourceViewer;
39 import org.eclipse.jface.util.IPropertyChangeListener;
40 import org.eclipse.jface.util.PropertyChangeEvent;
41 import org.eclipse.swt.SWT;
42 import org.eclipse.swt.custom.StyledText;
43 import org.eclipse.swt.events.SelectionEvent;
44 import org.eclipse.swt.events.SelectionListener;
45 import org.eclipse.swt.graphics.Color;
46 import org.eclipse.swt.graphics.RGB;
47 import org.eclipse.swt.layout.GridData;
48 import org.eclipse.swt.layout.GridLayout;
49 import org.eclipse.swt.layout.RowLayout;
50 import org.eclipse.swt.widgets.Button;
51 import org.eclipse.swt.widgets.Composite;
52 import org.eclipse.swt.widgets.Control;
53 import org.eclipse.swt.widgets.Display;
54 import org.eclipse.swt.widgets.Group;
55 import org.eclipse.swt.widgets.Label;
56 import org.eclipse.swt.widgets.List;
57 import org.eclipse.ui.IWorkbench;
58 import org.eclipse.ui.IWorkbenchPreferencePage;
59 import org.eclipse.ui.texteditor.AbstractTextEditor;
62 * The XMLSyntaxPreferencePage is a preference page that handles setting the
63 * colors used by the XML editors.
65 public class XMLSyntaxPreferencePage extends PreferencePage implements
66 IWorkbenchPreferencePage {
68 public final PreferenceDescriptor[] fKeys = new PreferenceDescriptor[] {
69 new PreferenceDescriptor(
70 PreferenceDescriptor.BOOLEAN,
71 AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT),
72 new PreferenceDescriptor(PreferenceDescriptor.STRING,
73 AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND),
75 new PreferenceDescriptor(PreferenceDescriptor.STRING,
76 IXMLSyntaxConstants.XML_DEFAULT
77 + ITextStylePreferences.SUFFIX_FOREGROUND),
78 new PreferenceDescriptor(PreferenceDescriptor.STRING,
79 IXMLSyntaxConstants.XML_DEFAULT
80 + ITextStylePreferences.SUFFIX_STYLE),
82 new PreferenceDescriptor(PreferenceDescriptor.STRING,
83 IXMLSyntaxConstants.XML_TAG
84 + ITextStylePreferences.SUFFIX_FOREGROUND),
85 new PreferenceDescriptor(PreferenceDescriptor.STRING,
86 IXMLSyntaxConstants.XML_TAG
87 + ITextStylePreferences.SUFFIX_STYLE),
89 new PreferenceDescriptor(PreferenceDescriptor.STRING,
90 IXMLSyntaxConstants.XML_ATT_NAME
91 + ITextStylePreferences.SUFFIX_FOREGROUND),
92 new PreferenceDescriptor(PreferenceDescriptor.STRING,
93 IXMLSyntaxConstants.XML_ATT_NAME
94 + ITextStylePreferences.SUFFIX_STYLE),
96 new PreferenceDescriptor(PreferenceDescriptor.STRING,
97 IXMLSyntaxConstants.XML_ATT_VALUE
98 + ITextStylePreferences.SUFFIX_FOREGROUND),
99 new PreferenceDescriptor(PreferenceDescriptor.STRING,
100 IXMLSyntaxConstants.XML_ATT_VALUE
101 + ITextStylePreferences.SUFFIX_STYLE),
103 new PreferenceDescriptor(PreferenceDescriptor.STRING,
104 IXMLSyntaxConstants.XML_ENTITY
105 + ITextStylePreferences.SUFFIX_FOREGROUND),
106 new PreferenceDescriptor(PreferenceDescriptor.STRING,
107 IXMLSyntaxConstants.XML_ENTITY
108 + ITextStylePreferences.SUFFIX_STYLE),
110 new PreferenceDescriptor(PreferenceDescriptor.STRING,
111 IXMLSyntaxConstants.XML_CDATA
112 + ITextStylePreferences.SUFFIX_FOREGROUND),
113 new PreferenceDescriptor(PreferenceDescriptor.STRING,
114 IXMLSyntaxConstants.XML_CDATA
115 + ITextStylePreferences.SUFFIX_STYLE),
117 new PreferenceDescriptor(PreferenceDescriptor.STRING,
118 IXMLSyntaxConstants.XML_PI
119 + ITextStylePreferences.SUFFIX_FOREGROUND),
120 new PreferenceDescriptor(PreferenceDescriptor.STRING,
121 IXMLSyntaxConstants.XML_PI
122 + ITextStylePreferences.SUFFIX_STYLE),
124 new PreferenceDescriptor(PreferenceDescriptor.STRING,
125 IXMLSyntaxConstants.XML_COMMENT
126 + ITextStylePreferences.SUFFIX_FOREGROUND),
127 new PreferenceDescriptor(PreferenceDescriptor.STRING,
128 IXMLSyntaxConstants.XML_COMMENT
129 + ITextStylePreferences.SUFFIX_STYLE),
131 new PreferenceDescriptor(PreferenceDescriptor.STRING,
132 IXMLSyntaxConstants.XML_DECL
133 + ITextStylePreferences.SUFFIX_FOREGROUND),
134 new PreferenceDescriptor(PreferenceDescriptor.STRING,
135 IXMLSyntaxConstants.XML_DECL
136 + ITextStylePreferences.SUFFIX_STYLE),
138 new PreferenceDescriptor(PreferenceDescriptor.STRING,
139 IXMLSyntaxConstants.XML_SMARTY
140 + ITextStylePreferences.SUFFIX_FOREGROUND),
141 new PreferenceDescriptor(PreferenceDescriptor.STRING,
142 IXMLSyntaxConstants.XML_SMARTY
143 + ITextStylePreferences.SUFFIX_STYLE),
145 new PreferenceDescriptor(PreferenceDescriptor.STRING,
146 IXMLSyntaxConstants.DTD_CONDITIONAL
147 + ITextStylePreferences.SUFFIX_FOREGROUND),
148 new PreferenceDescriptor(PreferenceDescriptor.STRING,
149 IXMLSyntaxConstants.DTD_CONDITIONAL
150 + ITextStylePreferences.SUFFIX_STYLE), };
152 OverlayPreferenceStore overlay;
154 final String[][] fSyntaxColorListModel = new String[][] {
155 { XMLPlugin.getResourceString("XmlSyntaxPreferencePage.others"), //$NON-NLS-1$
156 IXMLSyntaxConstants.XML_DEFAULT },
157 { XMLPlugin.getResourceString("XmlSyntaxPreferencePage.Tag"), //$NON-NLS-1$
158 IXMLSyntaxConstants.XML_TAG },
159 { XMLPlugin.getResourceString("XmlSyntaxPreferencePage.AttName"), //$NON-NLS-1$
160 IXMLSyntaxConstants.XML_ATT_NAME },
161 { XMLPlugin.getResourceString("XmlSyntaxPreferencePage.AttValue"), //$NON-NLS-1$
162 IXMLSyntaxConstants.XML_ATT_VALUE },
163 { XMLPlugin.getResourceString("XmlSyntaxPreferencePage.Entity"), //$NON-NLS-1$
164 IXMLSyntaxConstants.XML_ENTITY },
165 { XMLPlugin.getResourceString("XmlSyntaxPreferencePage.CDATA"), //$NON-NLS-1$
166 IXMLSyntaxConstants.XML_CDATA },
167 { XMLPlugin.getResourceString("XmlSyntaxPreferencePage.PI"), //$NON-NLS-1$
168 IXMLSyntaxConstants.XML_PI },
169 { XMLPlugin.getResourceString("XmlSyntaxPreferencePage.Comment"), //$NON-NLS-1$
170 IXMLSyntaxConstants.XML_COMMENT },
173 .getResourceString("XmlSyntaxPreferencePage.Declaration"), //$NON-NLS-1$
174 IXMLSyntaxConstants.XML_DECL },
177 .getResourceString("XmlSyntaxPreferencePage.Conditional"), //$NON-NLS-1$
178 IXMLSyntaxConstants.XML_SMARTY },
179 { XMLPlugin.getResourceString("XmlSyntaxPreferencePage.SmartyTag"), //$NON-NLS-1$
180 IXMLSyntaxConstants.DTD_CONDITIONAL }, };
182 private XMLTextTools xmlTextTools;
184 private Color bgColor;
190 ColorEditor bgColorEditor;
194 ColorEditor fgColorEditor;
198 SourceViewer preview;
201 * Constructor for XMLSyntaxPreferencePage.
203 public XMLSyntaxPreferencePage() {
204 setDescription(XMLPlugin
205 .getResourceString("XmlSyntaxPreferencePage.description")); //$NON-NLS-1$
207 setPreferenceStore(XMLPlugin.getDefault().getPreferenceStore());
209 overlay = new OverlayPreferenceStore(getPreferenceStore(), fKeys);
212 protected Control createContents(Composite parent) {
216 Composite colorComposite = new Composite(parent, SWT.NULL);
217 colorComposite.setLayout(new GridLayout());
219 Group backgroundComposite = new Group(colorComposite,
220 SWT.SHADOW_ETCHED_IN);
222 backgroundComposite.setLayout(new RowLayout());
223 backgroundComposite.setText(XMLPlugin
224 .getResourceString("XmlSyntaxPreferencePage.backgroundColor")); //$NON-NLS-1$
226 SelectionListener backgroundSelectionListener = new SelectionListener() {
227 public void widgetSelected(SelectionEvent e) {
228 boolean custom = bgCustom.getSelection();
229 bgColorEditor.getButton().setEnabled(custom);
232 AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT,
236 public void widgetDefaultSelected(SelectionEvent e) {
240 bgDefault = new Button(backgroundComposite, SWT.RADIO | SWT.LEFT);
241 bgDefault.setText(XMLPlugin
242 .getResourceString("XmlSyntaxPreferencePage.systemDefault")); //$NON-NLS-1$
243 bgDefault.addSelectionListener(backgroundSelectionListener);
245 bgCustom = new Button(backgroundComposite, SWT.RADIO | SWT.LEFT);
246 bgCustom.setText(XMLPlugin
247 .getResourceString("XmlSyntaxPreferencePage.custom")); //$NON-NLS-1$
248 bgCustom.addSelectionListener(backgroundSelectionListener);
250 bgColorEditor = new ColorEditor(backgroundComposite);
252 Label label = new Label(colorComposite, SWT.LEFT);
253 label.setText(XMLPlugin
254 .getResourceString("XmlSyntaxPreferencePage.foreground")); //$NON-NLS-1$
255 label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
257 Composite editorComposite = new Composite(colorComposite, SWT.NONE);
258 GridLayout layout = new GridLayout();
259 layout.numColumns = 2;
260 layout.marginHeight = 0;
261 layout.marginWidth = 0;
262 editorComposite.setLayout(layout);
263 GridData gd = new GridData(GridData.FILL_BOTH);
264 editorComposite.setLayoutData(gd);
266 colors = new List(editorComposite, SWT.SINGLE | SWT.V_SCROLL
268 gd = new GridData(GridData.FILL_BOTH);
269 gd.heightHint = convertHeightInCharsToPixels(5);
270 colors.setLayoutData(gd);
272 Composite stylesComposite = new Composite(editorComposite, SWT.NONE);
273 layout = new GridLayout();
274 layout.marginHeight = 0;
275 layout.marginWidth = 0;
276 layout.numColumns = 2;
277 stylesComposite.setLayout(layout);
278 stylesComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
280 label = new Label(stylesComposite, SWT.LEFT);
281 label.setText(XMLPlugin
282 .getResourceString("XmlSyntaxPreferencePage.color")); //$NON-NLS-1$
284 gd.horizontalAlignment = GridData.BEGINNING;
285 label.setLayoutData(gd);
287 fgColorEditor = new ColorEditor(stylesComposite);
289 Button fgColorButton = fgColorEditor.getButton();
290 gd = new GridData(GridData.FILL_HORIZONTAL);
291 gd.horizontalAlignment = GridData.BEGINNING;
292 fgColorButton.setLayoutData(gd);
294 label = new Label(stylesComposite, SWT.LEFT);
295 label.setText(XMLPlugin
296 .getResourceString("XmlSyntaxPreferencePage.bold")); //$NON-NLS-1$
298 gd.horizontalAlignment = GridData.BEGINNING;
299 label.setLayoutData(gd);
301 fgBold = new Button(stylesComposite, SWT.CHECK);
302 gd = new GridData(GridData.FILL_HORIZONTAL);
303 gd.horizontalAlignment = GridData.BEGINNING;
304 fgBold.setLayoutData(gd);
306 label = new Label(colorComposite, SWT.LEFT);
307 label.setText(XMLPlugin
308 .getResourceString("XmlSyntaxPreferencePage.preview")); //$NON-NLS-1$
309 label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
311 Control previewer = createPreviewer(colorComposite);
312 gd = new GridData(GridData.FILL_BOTH);
313 gd.widthHint = convertWidthInCharsToPixels(20);
314 gd.heightHint = convertHeightInCharsToPixels(5);
315 previewer.setLayoutData(gd);
317 colors.addSelectionListener(new SelectionListener() {
319 public void widgetDefaultSelected(SelectionEvent e) {
322 public void widgetSelected(SelectionEvent e) {
323 handleSyntaxColorListSelection();
327 bgColorEditor.getButton().addSelectionListener(new SelectionListener() {
328 public void widgetDefaultSelected(SelectionEvent e) {
331 public void widgetSelected(SelectionEvent e) {
332 PreferenceConverter.setValue(overlay,
333 AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND,
334 bgColorEditor.getColorValue());
338 fgColorButton.addSelectionListener(new SelectionListener() {
339 public void widgetDefaultSelected(SelectionEvent e) {
342 public void widgetSelected(SelectionEvent e) {
343 int i = colors.getSelectionIndex();
345 String key = fSyntaxColorListModel[i][1];
347 PreferenceConverter.setValue(overlay, key
348 + ITextStylePreferences.SUFFIX_FOREGROUND,
349 fgColorEditor.getColorValue());
353 fgBold.addSelectionListener(new SelectionListener() {
354 public void widgetDefaultSelected(SelectionEvent e) {
357 public void widgetSelected(SelectionEvent e) {
358 int i = colors.getSelectionIndex();
360 String key = fSyntaxColorListModel[i][1];
362 String value = (fgBold.getSelection()) ? ITextStylePreferences.STYLE_BOLD
363 : ITextStylePreferences.STYLE_NORMAL;
365 overlay.setValue(key + ITextStylePreferences.SUFFIX_STYLE,
372 return colorComposite;
375 private Control createPreviewer(Composite parent) {
376 xmlTextTools = new XMLTextTools(overlay); // REVISIT: DTD
378 preview = new SourceViewer(parent, null, SWT.V_SCROLL | SWT.H_SCROLL
381 preview.configure(new XMLConfiguration(xmlTextTools));
382 preview.getTextWidget().setFont(
383 JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT));
384 preview.setEditable(false);
386 initializeViewerColors(preview);
388 String content = loadPreviewContentFromFile("preview.xml"); //$NON-NLS-1$
389 IDocument document = new Document(content);
392 IDocumentPartitioner partitioner = xmlTextTools.createXMLPartitioner();
394 partitioner.connect(document);
395 document.setDocumentPartitioner(partitioner);
397 preview.setDocument(document);
399 overlay.addPropertyChangeListener(new IPropertyChangeListener() {
400 public void propertyChange(PropertyChangeEvent event) {
401 String p = event.getProperty();
402 if (p.equals(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND)
404 .equals(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT)) {
405 initializeViewerColors(preview);
408 preview.invalidateTextPresentation();
412 return preview.getControl();
416 * Initializes the given viewer's colors.
419 * the viewer to be initialized
421 void initializeViewerColors(ISourceViewer viewer) {
422 if (overlay != null) {
423 StyledText styledText = viewer.getTextWidget();
425 // ---------- background color ----------------------
428 .getBoolean(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT)) {
429 color = createColor(overlay,
430 AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND,
431 styledText.getDisplay());
434 styledText.setBackground(color);
436 if (bgColor != null) {
445 * Creates a color from the information stored in the given preference
446 * store. Returns <code>null</code> if there is no such information
449 private Color createColor(IPreferenceStore store, String key,
453 if (store.contains(key)) {
454 if (store.isDefault(key)) {
455 rgb = PreferenceConverter.getDefaultColor(store, key);
457 rgb = PreferenceConverter.getColor(store, key);
461 return new Color(display, rgb);
468 void handleSyntaxColorListSelection() {
469 int i = colors.getSelectionIndex();
471 String key = fSyntaxColorListModel[i][1];
473 RGB rgb = PreferenceConverter.getColor(overlay, key
474 + ITextStylePreferences.SUFFIX_FOREGROUND);
476 fgColorEditor.setColorValue(rgb);
479 fgBold.setSelection(overlay.getString(
480 key + ITextStylePreferences.SUFFIX_STYLE).indexOf(
481 ITextStylePreferences.STYLE_BOLD) >= 0);
484 private String loadPreviewContentFromFile(String filename) {
485 StringBuffer string = new StringBuffer(512);
488 char[] buf = new char[512];
489 BufferedReader reader = new BufferedReader(
490 new InputStreamReader(XMLSyntaxPreferencePage.class
491 .getResourceAsStream(filename)));
495 int n = reader.read(buf);
500 string.append(buf, 0, n);
505 } catch (IOException e) {
508 return string.toString();
514 private void initialize() {
517 for (int i = 0; i < fSyntaxColorListModel.length; i++) {
518 colors.add(fSyntaxColorListModel[i][0]);
521 colors.getDisplay().asyncExec(new Runnable() {
525 handleSyntaxColorListSelection();
530 private void initializeFields() {
531 RGB rgb = PreferenceConverter.getColor(overlay,
532 AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND);
533 bgColorEditor.setColorValue(rgb);
535 boolean def = overlay
536 .getBoolean(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT);
537 bgDefault.setSelection(def);
538 bgCustom.setSelection(!def);
539 bgColorEditor.getButton().setEnabled(!def);
543 * @see IWorkbenchPreferencePage#init(IWorkbench)
545 public void init(IWorkbench workbench) {
549 * @see PreferencePage#performDefaults()
551 protected void performDefaults() {
552 overlay.loadDefaults();
553 // initializeFields();
554 handleSyntaxColorListSelection();
556 super.performDefaults();
558 preview.invalidateTextPresentation();
562 * @see org.eclipse.jface.preference.IPreferencePage#performOk()
564 public boolean performOk() {
566 XMLPlugin.getDefault().savePluginPreferences();
572 * @see org.eclipse.jface.dialogs.IDialogPage#dispose()
574 public void dispose() {
575 if (xmlTextTools != null) {
576 xmlTextTools.dispose();
580 if (overlay != null) {