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 / SyntaxPreviewer.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: SyntaxPreviewer.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 java.io.BufferedReader;
17 import java.io.IOException;
18 import java.io.InputStreamReader;
19
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;
24
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;
39
40 /**
41  * A previewer for the syntax styling preferences, used to give real time
42  * feedback on changes to the preferences.
43  */
44 public class SyntaxPreviewer extends SourceViewer {
45
46         // Constants ---------------------------------------------------------------
47
48         /**
49          * Alias for the preference constant <code>EDITOR_BACKGROUND_COLOR</code>.
50          */
51         private static final String BACKGROUND_COLOR = 
52                 CssUIPreferences.EDITOR_BACKGROUND_COLOR;
53
54         /**
55          * Alias for the preference constant
56          * <code>EDITOR_BACKGROUND_DEFAULT_COLOR</code>.
57          */
58         private static final String BACKGROUND_DEFAULT_COLOR = 
59                 CssUIPreferences.EDITOR_BACKGROUND_DEFAULT_COLOR;
60
61         // Instance Variables ------------------------------------------------------
62
63         /**
64          * The preference store used for loading the style settings.
65          */
66         private IPreferenceStore fStore;
67
68         /**
69          * Listener that handles changes to the preference store.
70          */
71         private IPropertyChangeListener fPropertyChangeListener =
72                 new IPropertyChangeListener() {
73                         public void propertyChange(PropertyChangeEvent event) {
74                                 if (affectsPresentation(event)) {
75                                         updateColors();
76                                 }
77                                 invalidateTextPresentation();
78                         }
79                 };
80
81         private CssTextTools fTextTools;
82
83         /**
84          * The cached background color of the previewer.
85          */
86         private Color fBackgroundColor;
87
88         // Constructors ------------------------------------------------------------
89
90         /**
91          * Constructor.
92          * 
93          * @param parent The parent composite
94          * @param store The preference store to use for loading the style settings
95          */
96         public SyntaxPreviewer(Composite parent, IPreferenceStore store) {
97                 super(parent, null, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
98                 this.fStore = store;
99                 this.fTextTools = new CssTextTools(this.fStore);
100
101                 configure(new CssSourceViewerConfiguration(
102                         this.fTextTools, this.fStore));
103                 setEditable(false);
104                 getTextWidget().setFont(JFaceResources.getTextFont());
105
106                 IDocument document = new Document(
107                         loadTextFromResource("preview.css")); //$NON-NLS-1$
108                 this.fTextTools.setupDocument(document);
109                 setDocument(document);
110
111                 this.fStore.addPropertyChangeListener(this.fPropertyChangeListener);
112         }
113
114         // SourceViewer Implementation ---------------------------------------------
115
116         /**
117          * @see org.eclipse.jface.text.TextViewer#handleDispose()
118          */
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;
125                         }
126                         this.fStore = null;
127                 }
128                 if (this.fTextTools != null) {
129                         this.fTextTools.dispose();
130                         this.fTextTools = null;
131                 }
132                 if ((this.fBackgroundColor != null)
133                  && !this.fBackgroundColor.isDisposed()) {
134                         this.fBackgroundColor.dispose();
135                 }
136                 super.handleDispose();
137         }
138
139         // Private Methods ---------------------------------------------------------
140
141         private boolean affectsPresentation(PropertyChangeEvent event) {
142                 String p = event.getProperty();
143                 if (BACKGROUND_COLOR.equals(p)
144                  || BACKGROUND_DEFAULT_COLOR.equals(p)) {
145                         return true;
146                 }
147                 return false;
148         }
149
150         /**
151          * Creates a color from the information stored in the given preference
152          * store.
153          * 
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
158          */
159         private Color createColor(String key, Display display) {
160                 Color color = null;      
161                 if (this.fStore.contains(key)) {
162                         RGB rgb = null;
163                         if (this.fStore.isDefault(key)) {
164                                 rgb = PreferenceConverter.getDefaultColor(this.fStore, key);
165                         } else {
166                                 rgb = PreferenceConverter.getColor(this.fStore, key);
167                         }
168                         if (rgb != null) {
169                                 color = new Color(display, rgb);
170                         }
171                 }
172                 return color;
173         }       
174         
175         /**
176          * Loads and returns the text stored in the specified resource.
177          * 
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
181          */
182         private String loadTextFromResource(String name) {
183                 String line;
184                 String separator = System.getProperty("line.separator"); //$NON-NLS-1$
185                 StringBuffer buffer = new StringBuffer(512);
186                 BufferedReader reader = null;
187                 try {
188                         reader = new BufferedReader(new InputStreamReader(
189                                 getClass().getResourceAsStream(name)));
190                         while ((line = reader.readLine()) != null) {
191                                 buffer.append(line);
192                                 buffer.append(separator);
193                         }
194                 } catch (IOException io) {
195                         CssUI.log(io);
196                 } finally {
197                         if (reader != null) {
198                                 try {
199                                         reader.close();
200                                 } catch (IOException e) {
201                                         // ignore
202                                 }
203                         }
204                 }
205                 return buffer.toString();
206         }
207         
208         /**
209          * Updates the previewer colors.
210          */
211         private void updateColors() {
212                 if (this.fStore != null) {
213                         StyledText styledText = getTextWidget();
214                         Color color = null;
215                         if (!this.fStore.getBoolean(BACKGROUND_DEFAULT_COLOR)) {
216                                 color = createColor(BACKGROUND_COLOR,
217                                         styledText.getDisplay());
218                         }
219                         styledText.setBackground(color);
220                         if (this.fBackgroundColor != null) {
221                                 this.fBackgroundColor.dispose();
222                         }
223                         this.fBackgroundColor = color;
224                 }
225         }
226
227 }