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: CssSourceViewerConfiguration.java,v 1.1 2004-09-02 18:11:48 jsurfer Exp $
14 package net.sourceforge.phpeclipse.css.ui.internal.text;
16 import java.util.ArrayList;
17 import java.util.List;
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;
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;
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.
58 public final class CssSourceViewerConfiguration
59 extends TextSourceViewerConfiguration {
61 // Constants ---------------------------------------------------------------
64 * Alias for the preference constant <code>CONTENTASSIST_AUTOINSERT</code>.
66 private static final String AUTOINSERT =
67 CssUIPreferences.CONTENTASSIST_AUTOINSERT;
70 * Alias for the preference constant
71 * <code>CONTENTASSIST_AUTOACTIVATION</code>.
73 private static final String AUTOACTIVATION =
74 CssUIPreferences.CONTENTASSIST_AUTOACTIVATION;
77 * Alias for the preference constant
78 * <code>CONTENTASSIST_AUTOACTIVATION_DELAY</code>.
80 private static final String AUTOACTIVATION_DELAY =
81 CssUIPreferences.CONTENTASSIST_AUTOACTIVATION_DELAY;
84 * Alias for the preference constant
85 * <code>CONTENTASSIST_PROPOSALS_BACKGROUND</code>.
87 private static final String PROPOSALS_BACKGROUND =
88 CssUIPreferences.CONTENTASSIST_PROPOSALS_BACKGROUND;
91 * Alias for the preference constant
92 * <code>CONTENTASSIST_PROPOSALS_FOREGROUND</code>.
94 private static final String PROPOSALS_FOREGROUND =
95 CssUIPreferences.CONTENTASSIST_PROPOSALS_FOREGROUND;
98 * Alias for the preference constant <code>EDITOR_SPACES_FOR_TABS</code>.
100 private static final String SPACES_FOR_TABS =
101 CssUIPreferences.EDITOR_SPACES_FOR_TABS;
103 // Instance Variables ------------------------------------------------------
106 * The associated editor.
108 private ITextEditor editor;
111 * The preference store used.
113 private IPreferenceStore store;
118 private IProfile profile;
121 * The associated text tools.
123 private CssTextTools textTools;
125 // Constructors ------------------------------------------------------------
128 * Default constructor.
130 public CssSourceViewerConfiguration() {
131 this(CssUI.getDefault().getTextTools());
137 * @param textTools the CSS text tools to associate with the source viewer
140 public CssSourceViewerConfiguration(CssTextTools textTools) {
141 this(textTools, CssUI.getDefault().getPreferenceStore());
147 * @param textTools the CSS text tools to associate with the source viewer
149 * @param store the preference store
151 public CssSourceViewerConfiguration(
152 CssTextTools textTools, IPreferenceStore store
154 this(textTools, store, null);
160 * @param textTools the CSS text tools to associate with the source viewer
162 * @param store the preference store
163 * @param editor the text editor
165 public CssSourceViewerConfiguration(
166 CssTextTools textTools, IPreferenceStore store, ITextEditor editor
168 this.textTools = textTools;
170 this.editor = editor;
173 // SourceViewerConfiguration Implementation --------------------------------
176 * @see SourceViewerConfiguration#getAnnotationHover(ISourceViewer)
178 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
179 return new CssAnnotationHover();
183 * @see SourceViewerConfiguration#getAutoIndentStrategy(ISourceViewer, String)
185 public IAutoIndentStrategy getAutoIndentStrategy(
186 ISourceViewer sourceViewer, String contentType
188 if (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType)) {
189 return new CssAutoEditStrategy();
192 return new DefaultAutoIndentStrategy();
196 * @see SourceViewerConfiguration#getConfiguredContentTypes(ISourceViewer)
198 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
199 return new String[] {
200 IDocument.DEFAULT_CONTENT_TYPE,
201 CssPartitionScanner.CSS_COMMENT,
202 CssPartitionScanner.CSS_STRING
207 * @see SourceViewerConfiguration#getContentAssistant(ISourceViewer)
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)));
231 * @see SourceViewerConfiguration#getDoubleClickStrategy(ISourceViewer, String)
233 public ITextDoubleClickStrategy getDoubleClickStrategy(
234 ISourceViewer sourceViewer, String contentType
236 if (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType)) {
237 return new CssDoubleClickStrategy();
239 return new DefaultTextDoubleClickStrategy();
243 * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
245 public String[] getIndentPrefixes(
246 ISourceViewer sourceViewer, String contentType
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();
254 for (int j = 0; j < tabWidth - i; j++) {
261 for (int j = 0; j < i; j++) {
268 retVal.add(prefix.toString());
270 retVal.add(""); //$NON-NLS-1$
271 return (String[]) retVal.toArray(new String[retVal.size()]);
275 * @see SourceViewerConfiguration#getPresentationReconciler(ISourceViewer)
277 public IPresentationReconciler getPresentationReconciler(
278 ISourceViewer sourceViewer
280 PresentationReconciler reconciler = new PresentationReconciler();
281 DefaultDamagerRepairer dr;
284 dr = new DefaultDamagerRepairer(textTools.getCommentScanner());
285 reconciler.setDamager(dr, CssPartitionScanner.CSS_COMMENT);
286 reconciler.setRepairer(dr, CssPartitionScanner.CSS_COMMENT);
289 dr = new DefaultDamagerRepairer(textTools.getStringScanner());
290 reconciler.setDamager(dr, CssPartitionScanner.CSS_STRING);
291 reconciler.setRepairer(dr, CssPartitionScanner.CSS_STRING);
294 dr = new DefaultDamagerRepairer(textTools.getCodeScanner(getProfile()));
295 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
296 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
302 * @see SourceViewerConfiguration#getReconciler(ISourceViewer)
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);
317 * @see SourceViewerConfiguration#getTextHover(ISourceViewer, String)
319 public ITextHover getTextHover(
320 ISourceViewer sourceViewer, String contentType
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);
329 return super.getTextHover(sourceViewer, contentType);
332 // Private Methods ---------------------------------------------------------
335 * Returns the color manager associated with this configuration.
337 * @return the color manager
339 private IColorManager getColorManager() {
340 if (textTools != null) {
341 return textTools.getColorManager();
347 * Returns the CSS profile for the resource currently being viewed, or
348 * the default profile if this source viewer configuration isn't associated
351 * @return the profile
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();
363 IProfileManager mgr = CssCore.getDefault().getProfileManager();
364 profile = mgr.getProfile(resource);