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: SyntaxPreviewer.java,v 1.1 2004-09-02 18:11:50 jsurfer Exp $
14 package net.sourceforge.phpeclipse.css.ui.internal.preferences;
16 import java.io.BufferedReader;
17 import java.io.IOException;
18 import java.io.InputStreamReader;
20 import net.sourceforge.phpeclipse.css.ui.CssUI;
21 import net.sourceforge.phpeclipse.css.ui.internal.CssUIPreferences;
22 import net.sourceforge.phpeclipse.css.ui.internal.text.CssSourceViewerConfiguration;
23 import net.sourceforge.phpeclipse.css.ui.text.CssTextTools;
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.jface.preference.PreferenceConverter;
27 import org.eclipse.jface.resource.JFaceResources;
28 import org.eclipse.jface.text.Document;
29 import org.eclipse.jface.text.IDocument;
30 import org.eclipse.jface.text.source.SourceViewer;
31 import org.eclipse.jface.util.IPropertyChangeListener;
32 import org.eclipse.jface.util.PropertyChangeEvent;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.custom.StyledText;
35 import org.eclipse.swt.graphics.Color;
36 import org.eclipse.swt.graphics.RGB;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Display;
41 * A previewer for the syntax styling preferences, used to give real time
42 * feedback on changes to the preferences.
44 public class SyntaxPreviewer extends SourceViewer {
46 // Constants ---------------------------------------------------------------
49 * Alias for the preference constant <code>EDITOR_BACKGROUND_COLOR</code>.
51 private static final String BACKGROUND_COLOR =
52 CssUIPreferences.EDITOR_BACKGROUND_COLOR;
55 * Alias for the preference constant
56 * <code>EDITOR_BACKGROUND_DEFAULT_COLOR</code>.
58 private static final String BACKGROUND_DEFAULT_COLOR =
59 CssUIPreferences.EDITOR_BACKGROUND_DEFAULT_COLOR;
61 // Instance Variables ------------------------------------------------------
64 * The preference store used for loading the style settings.
66 private IPreferenceStore fStore;
69 * Listener that handles changes to the preference store.
71 private IPropertyChangeListener fPropertyChangeListener =
72 new IPropertyChangeListener() {
73 public void propertyChange(PropertyChangeEvent event) {
74 if (affectsPresentation(event)) {
77 invalidateTextPresentation();
81 private CssTextTools fTextTools;
84 * The cached background color of the previewer.
86 private Color fBackgroundColor;
88 // Constructors ------------------------------------------------------------
93 * @param parent The parent composite
94 * @param store The preference store to use for loading the style settings
96 public SyntaxPreviewer(Composite parent, IPreferenceStore store) {
97 super(parent, null, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
99 this.fTextTools = new CssTextTools(this.fStore);
101 configure(new CssSourceViewerConfiguration(
102 this.fTextTools, this.fStore));
104 getTextWidget().setFont(JFaceResources.getTextFont());
106 IDocument document = new Document(
107 loadTextFromResource("preview.css")); //$NON-NLS-1$
108 this.fTextTools.setupDocument(document);
109 setDocument(document);
111 this.fStore.addPropertyChangeListener(this.fPropertyChangeListener);
114 // SourceViewer Implementation ---------------------------------------------
117 * @see org.eclipse.jface.text.TextViewer#handleDispose()
119 protected void handleDispose() {
120 if (this.fStore != null) {
121 if (this.fPropertyChangeListener != null) {
122 this.fStore.removePropertyChangeListener(
123 this.fPropertyChangeListener);
124 this.fPropertyChangeListener = null;
128 if (this.fTextTools != null) {
129 this.fTextTools.dispose();
130 this.fTextTools = null;
132 if ((this.fBackgroundColor != null)
133 && !this.fBackgroundColor.isDisposed()) {
134 this.fBackgroundColor.dispose();
136 super.handleDispose();
139 // Private Methods ---------------------------------------------------------
141 private boolean affectsPresentation(PropertyChangeEvent event) {
142 String p = event.getProperty();
143 if (BACKGROUND_COLOR.equals(p)
144 || BACKGROUND_DEFAULT_COLOR.equals(p)) {
151 * Creates a color from the information stored in the given preference
154 * @param key The preference key under which the color is stored
155 * @param display The display
156 * @return The color found, or <code>null</code> if no corresponding
157 * preference is available
159 private Color createColor(String key, Display display) {
161 if (this.fStore.contains(key)) {
163 if (this.fStore.isDefault(key)) {
164 rgb = PreferenceConverter.getDefaultColor(this.fStore, key);
166 rgb = PreferenceConverter.getColor(this.fStore, key);
169 color = new Color(display, rgb);
176 * Loads and returns the text stored in the specified resource.
178 * @param name The resource name
179 * @return The text loaded from the resource, or an empty string if there
180 * was an error reading the resource contents
182 private String loadTextFromResource(String name) {
184 String separator = System.getProperty("line.separator"); //$NON-NLS-1$
185 StringBuffer buffer = new StringBuffer(512);
186 BufferedReader reader = null;
188 reader = new BufferedReader(new InputStreamReader(
189 getClass().getResourceAsStream(name)));
190 while ((line = reader.readLine()) != null) {
192 buffer.append(separator);
194 } catch (IOException io) {
197 if (reader != null) {
200 } catch (IOException e) {
205 return buffer.toString();
209 * Updates the previewer colors.
211 private void updateColors() {
212 if (this.fStore != null) {
213 StyledText styledText = getTextWidget();
215 if (!this.fStore.getBoolean(BACKGROUND_DEFAULT_COLOR)) {
216 color = createColor(BACKGROUND_COLOR,
217 styledText.getDisplay());
219 styledText.setBackground(color);
220 if (this.fBackgroundColor != null) {
221 this.fBackgroundColor.dispose();
223 this.fBackgroundColor = color;