intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.css.ui / src / net / sourceforge / phpeclipse / css / ui / internal / text / CssSourceViewerConfiguration.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: CssSourceViewerConfiguration.java,v 1.1 2004-09-02 18:11:48 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.css.ui.internal.text;
15
16 import java.util.ArrayList;
17 import java.util.List;
18
19 import net.sourceforge.phpeclipse.css.core.CssCore;
20 import net.sourceforge.phpeclipse.css.core.profiles.IProfile;
21 import net.sourceforge.phpeclipse.css.core.profiles.IProfileManager;
22 import net.sourceforge.phpeclipse.css.ui.CssUI;
23 import net.sourceforge.phpeclipse.css.ui.internal.CssUIPreferences;
24 import net.sourceforge.phpeclipse.css.ui.text.CssTextTools;
25 import net.sourceforge.phpeclipse.css.ui.text.IColorManager;
26
27 import org.eclipse.core.resources.IResource;
28 import org.eclipse.core.runtime.NullProgressMonitor;
29 import org.eclipse.jface.preference.IPreferenceStore;
30 import org.eclipse.jface.preference.PreferenceConverter;
31 import org.eclipse.jface.text.DefaultAutoIndentStrategy;
32 import org.eclipse.jface.text.DefaultTextDoubleClickStrategy;
33 import org.eclipse.jface.text.IAutoIndentStrategy;
34 import org.eclipse.jface.text.IDocument;
35 import org.eclipse.jface.text.ITextDoubleClickStrategy;
36 import org.eclipse.jface.text.ITextHover;
37 import org.eclipse.jface.text.contentassist.ContentAssistant;
38 import org.eclipse.jface.text.contentassist.IContentAssistant;
39 import org.eclipse.jface.text.presentation.IPresentationReconciler;
40 import org.eclipse.jface.text.presentation.PresentationReconciler;
41 import org.eclipse.jface.text.reconciler.IReconciler;
42 import org.eclipse.jface.text.reconciler.MonoReconciler;
43 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
44 import org.eclipse.jface.text.source.IAnnotationHover;
45 import org.eclipse.jface.text.source.IAnnotationModel;
46 import org.eclipse.jface.text.source.ISourceViewer;
47 import org.eclipse.ui.IEditorInput;
48 import org.eclipse.ui.IFileEditorInput;
49 import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
50 import org.eclipse.ui.texteditor.IDocumentProvider;
51 import org.eclipse.ui.texteditor.ITextEditor;
52
53 /**
54  * Source viewer configuration for CSS. This class takes care of setting up 
55  * various aspects of the CSS editor, such as text hovers, syntax highlighting,
56  * reconciling or the double click strategy.
57  */
58 public final class CssSourceViewerConfiguration
59         extends TextSourceViewerConfiguration {
60
61         // Constants ---------------------------------------------------------------
62
63         /**
64          * Alias for the preference constant <code>CONTENTASSIST_AUTOINSERT</code>.
65          */
66         private static final String AUTOINSERT =
67                 CssUIPreferences.CONTENTASSIST_AUTOINSERT;
68
69         /**
70          * Alias for the preference constant
71          * <code>CONTENTASSIST_AUTOACTIVATION</code>.
72          */
73         private static final String AUTOACTIVATION =
74                 CssUIPreferences.CONTENTASSIST_AUTOACTIVATION;
75
76         /**
77          * Alias for the preference constant
78          * <code>CONTENTASSIST_AUTOACTIVATION_DELAY</code>.
79          */
80         private static final String AUTOACTIVATION_DELAY =
81                 CssUIPreferences.CONTENTASSIST_AUTOACTIVATION_DELAY;
82
83         /**
84          * Alias for the preference constant
85          * <code>CONTENTASSIST_PROPOSALS_BACKGROUND</code>.
86          */
87         private static final String PROPOSALS_BACKGROUND =
88                 CssUIPreferences.CONTENTASSIST_PROPOSALS_BACKGROUND;
89
90         /**
91          * Alias for the preference constant
92          * <code>CONTENTASSIST_PROPOSALS_FOREGROUND</code>.
93          */
94         private static final String PROPOSALS_FOREGROUND =
95                 CssUIPreferences.CONTENTASSIST_PROPOSALS_FOREGROUND;
96
97         /**
98          * Alias for the preference constant <code>EDITOR_SPACES_FOR_TABS</code>.
99          */
100         private static final String SPACES_FOR_TABS =
101                 CssUIPreferences.EDITOR_SPACES_FOR_TABS;
102
103         // Instance Variables ------------------------------------------------------
104
105         /**
106          * The associated editor.
107          */
108         private ITextEditor editor;
109
110         /**
111          * The preference store used.
112          */
113         private IPreferenceStore store;
114
115         /**
116          * The CSS profile.
117          */
118         private IProfile profile;
119
120         /**
121          * The associated text tools.
122          */
123         private CssTextTools textTools;
124
125         // Constructors ------------------------------------------------------------
126
127         /**
128          * Default constructor.
129          */
130         public CssSourceViewerConfiguration() {
131                 this(CssUI.getDefault().getTextTools());
132         }
133
134         /**
135          * Constructor.
136          * 
137          * @param textTools the CSS text tools to associate with the source viewer
138          *        configuration
139          */
140         public CssSourceViewerConfiguration(CssTextTools textTools) {
141                 this(textTools, CssUI.getDefault().getPreferenceStore());
142         }
143
144         /**
145          * Constructor.
146          * 
147          * @param textTools the CSS text tools to associate with the source viewer
148          *        configuration
149          * @param store the preference store
150          */
151         public CssSourceViewerConfiguration(
152                 CssTextTools textTools, IPreferenceStore store
153         ) {
154                 this(textTools, store, null);
155         }
156
157         /**
158          * Constructor.
159          * 
160          * @param textTools the CSS text tools to associate with the source viewer
161          *        configuration
162          * @param store the preference store
163          * @param editor the text editor
164          */
165         public CssSourceViewerConfiguration(
166                 CssTextTools textTools, IPreferenceStore store, ITextEditor editor
167         ) {
168                 this.textTools = textTools;
169                 this.store = store;
170                 this.editor = editor;
171         }
172
173         // SourceViewerConfiguration Implementation --------------------------------
174
175         /*
176          * @see SourceViewerConfiguration#getAnnotationHover(ISourceViewer)
177          */
178         public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
179                 return new CssAnnotationHover();
180         }
181
182         /*
183          * @see SourceViewerConfiguration#getAutoIndentStrategy(ISourceViewer, String)
184          */
185         public IAutoIndentStrategy getAutoIndentStrategy(
186                 ISourceViewer sourceViewer, String contentType
187         ) {
188                 if (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType)) {
189                         return new CssAutoEditStrategy();
190                 }
191
192                 return new DefaultAutoIndentStrategy();
193         }
194
195         /*
196          * @see SourceViewerConfiguration#getConfiguredContentTypes(ISourceViewer)
197          */
198         public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
199                 return new String[] {
200                         IDocument.DEFAULT_CONTENT_TYPE,
201                         CssPartitionScanner.CSS_COMMENT,
202                         CssPartitionScanner.CSS_STRING
203                 };
204         }
205
206         /*
207          * @see SourceViewerConfiguration#getContentAssistant(ISourceViewer)
208          */
209         public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
210                 ContentAssistant assistant = new ContentAssistant();
211                 assistant.setContentAssistProcessor(
212                         new CssContentAssistProcessor(store, getProfile(), editor),
213                         IDocument.DEFAULT_CONTENT_TYPE);
214                 assistant.setInformationControlCreator(
215                         getInformationControlCreator(sourceViewer));
216                 assistant.enableAutoInsert(store.getBoolean(AUTOINSERT));
217                 assistant.enableAutoActivation(store.getBoolean(AUTOACTIVATION));
218                 assistant.setAutoActivationDelay(store.getInt(AUTOACTIVATION_DELAY));
219                 assistant.setContextInformationPopupOrientation(
220                         IContentAssistant.CONTEXT_INFO_BELOW);
221                 assistant.setProposalPopupOrientation(
222                         IContentAssistant.PROPOSAL_STACKED);
223                 assistant.setProposalSelectorBackground(getColorManager().getColor(
224                         PreferenceConverter.getColor(store, PROPOSALS_BACKGROUND)));
225                 assistant.setProposalSelectorForeground(getColorManager().getColor(
226                         PreferenceConverter.getColor(store, PROPOSALS_FOREGROUND)));
227                 return assistant;
228         }
229
230         /*
231          * @see SourceViewerConfiguration#getDoubleClickStrategy(ISourceViewer, String)
232          */
233         public ITextDoubleClickStrategy getDoubleClickStrategy(
234                 ISourceViewer sourceViewer, String contentType
235         ) {
236                 if (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType)) {
237                         return new CssDoubleClickStrategy();
238                 }
239                 return new DefaultTextDoubleClickStrategy();
240         }
241
242         /*
243          * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
244          */
245         public String[] getIndentPrefixes(
246                 ISourceViewer sourceViewer, String contentType
247         ) {
248                 List retVal = new ArrayList();
249                 int tabWidth = getTabWidth(sourceViewer);
250                 boolean useSpaces = store.getBoolean(SPACES_FOR_TABS);
251                 for (int i = 0; i <= tabWidth; i++) {
252                         StringBuffer prefix = new StringBuffer();
253                         if (useSpaces) {
254                                 for (int j = 0; j < tabWidth - i; j++) {
255                                         prefix.append(' ');
256                                 }
257                                 if (i > 0) {
258                                         prefix.append('\t');
259                                 }
260                         } else {        
261                                 for (int j = 0; j < i; j++) {
262                                         prefix.append(' ');
263                                 }
264                                 if (i < tabWidth) {
265                                         prefix.append('\t');
266                                 }
267                         }
268                         retVal.add(prefix.toString());
269                 }
270                 retVal.add(""); //$NON-NLS-1$
271                 return (String[]) retVal.toArray(new String[retVal.size()]);
272         }
273
274         /*
275          * @see SourceViewerConfiguration#getPresentationReconciler(ISourceViewer)
276          */
277         public IPresentationReconciler getPresentationReconciler(
278                 ISourceViewer sourceViewer
279         ) {
280                 PresentationReconciler reconciler = new PresentationReconciler();
281                 DefaultDamagerRepairer dr;
282
283                 // Comments
284                 dr = new DefaultDamagerRepairer(textTools.getCommentScanner());
285                 reconciler.setDamager(dr, CssPartitionScanner.CSS_COMMENT);
286                 reconciler.setRepairer(dr, CssPartitionScanner.CSS_COMMENT);
287
288                 // Strings
289                 dr = new DefaultDamagerRepairer(textTools.getStringScanner());
290                 reconciler.setDamager(dr, CssPartitionScanner.CSS_STRING);
291                 reconciler.setRepairer(dr, CssPartitionScanner.CSS_STRING);
292
293                 // Code
294                 dr = new DefaultDamagerRepairer(textTools.getCodeScanner(getProfile()));
295                 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
296                 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
297
298                 return reconciler;
299         }
300
301         /*
302          * @see SourceViewerConfiguration#getReconciler(ISourceViewer)
303          */
304         public IReconciler getReconciler(ISourceViewer sourceViewer) {
305                 if ((editor != null) && editor.isEditable()) {
306                         MonoReconciler reconciler = new MonoReconciler(
307                                         new CssReconcilingStrategy(editor), false);
308                         reconciler.setProgressMonitor(new NullProgressMonitor());
309                         reconciler.setDelay(500);
310                         return reconciler;
311                 }
312
313                 return null;
314         }
315
316         /*
317          * @see SourceViewerConfiguration#getTextHover(ISourceViewer, String)
318          */
319         public ITextHover getTextHover(
320                 ISourceViewer sourceViewer, String contentType
321         ) {
322                 if (editor != null) {
323                         IDocumentProvider provider = editor.getDocumentProvider();
324                         IEditorInput input = editor.getEditorInput();
325                         IAnnotationModel model = provider.getAnnotationModel(input);
326                         return new CssTextHover(model);
327                 }
328
329                 return super.getTextHover(sourceViewer, contentType);
330         }
331
332         // Private Methods ---------------------------------------------------------
333
334         /**
335          * Returns the color manager associated with this configuration.
336          * 
337          * @return the color manager
338          */
339         private IColorManager getColorManager() {
340                 if (textTools != null) {
341                         return textTools.getColorManager();
342                 }
343                 return null;
344         }
345
346         /**
347          * Returns the CSS profile for the resource currently being viewed, or 
348          * the default profile if this source viewer configuration isn't associated
349          * with an editor.
350          * 
351          * @return the profile
352          */
353         private IProfile getProfile() {
354                 if (profile == null) {
355                         IResource resource = null;
356                         if (editor != null) {
357                                 IEditorInput input = editor.getEditorInput();
358                                 if (input instanceof IFileEditorInput) {
359                                         resource = ((IFileEditorInput) input).getFile();
360                                 }
361                         }
362
363                         IProfileManager mgr = CssCore.getDefault().getProfileManager();
364                         profile = mgr.getProfile(resource);
365                 }
366
367                 return profile;
368         }
369 }