KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > propertiesfileeditor > PropertiesFileSourceViewerConfiguration


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.propertiesfileeditor;
12
13 import java.util.Map JavaDoc;
14
15 import org.eclipse.core.runtime.NullProgressMonitor;
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.core.runtime.content.IContentType;
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.widgets.Shell;
21
22 import org.eclipse.jface.internal.text.html.HTMLTextPresenter;
23 import org.eclipse.jface.preference.IPreferenceStore;
24 import org.eclipse.jface.util.PropertyChangeEvent;
25
26 import org.eclipse.jface.text.DefaultInformationControl;
27 import org.eclipse.jface.text.IDocument;
28 import org.eclipse.jface.text.IInformationControl;
29 import org.eclipse.jface.text.IInformationControlCreator;
30 import org.eclipse.jface.text.ITextDoubleClickStrategy;
31 import org.eclipse.jface.text.presentation.IPresentationReconciler;
32 import org.eclipse.jface.text.presentation.PresentationReconciler;
33 import org.eclipse.jface.text.reconciler.IReconciler;
34 import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
35 import org.eclipse.jface.text.reconciler.MonoReconciler;
36 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
37 import org.eclipse.jface.text.rules.RuleBasedScanner;
38 import org.eclipse.jface.text.source.Annotation;
39 import org.eclipse.jface.text.source.IAnnotationHover;
40 import org.eclipse.jface.text.source.ISourceViewer;
41
42 import org.eclipse.ui.texteditor.ITextEditor;
43 import org.eclipse.ui.texteditor.spelling.SpellingReconcileStrategy;
44 import org.eclipse.ui.texteditor.spelling.SpellingService;
45
46 import org.eclipse.ui.editors.text.EditorsUI;
47 import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
48
49 import org.eclipse.jdt.ui.PreferenceConstants;
50 import org.eclipse.jdt.ui.text.IColorManager;
51
52 import org.eclipse.jdt.internal.ui.text.AbstractJavaScanner;
53 import org.eclipse.jdt.internal.ui.text.HTMLAnnotationHover;
54 import org.eclipse.jdt.internal.ui.text.JavaPresentationReconciler;
55 import org.eclipse.jdt.internal.ui.text.SingleTokenJavaScanner;
56 import org.eclipse.jdt.internal.ui.text.java.JavaStringDoubleClickSelector;
57
58 /**
59  * Configuration for a source viewer which shows a properties file.
60  * <p>
61  * This class may be instantiated; it is not intended to be subclassed.
62  * </p>
63  *
64  * @since 3.1
65  */

66 public class PropertiesFileSourceViewerConfiguration extends TextSourceViewerConfiguration {
67
68     /** Properties file content type */
69     private static final IContentType PROPERTIES_CONTENT_TYPE= Platform.getContentTypeManager().getContentType("org.eclipse.jdt.core.javaProperties"); //$NON-NLS-1$
70

71     /**
72      * The text editor.
73      */

74     private ITextEditor fTextEditor;
75     /**
76      * The document partitioning.
77      */

78     private String JavaDoc fDocumentPartitioning;
79     /**
80      * The property key scanner.
81      */

82     private AbstractJavaScanner fPropertyKeyScanner;
83     /**
84      * The comment scanner.
85      */

86     private AbstractJavaScanner fCommentScanner;
87     /**
88      * The property value scanner.
89      */

90     private AbstractJavaScanner fPropertyValueScanner;
91     /**
92      * The color manager.
93      */

94     private IColorManager fColorManager;
95
96
97     /**
98      * Creates a new properties file source viewer configuration for viewers in the given editor
99      * using the given preference store, the color manager and the specified document partitioning.
100      *
101      * @param colorManager the color manager
102      * @param preferenceStore the preference store, can be read-only
103      * @param editor the editor in which the configured viewer(s) will reside
104      * @param partitioning the document partitioning for this configuration
105      */

106     public PropertiesFileSourceViewerConfiguration(IColorManager colorManager, IPreferenceStore preferenceStore, ITextEditor editor, String JavaDoc partitioning) {
107         super(preferenceStore);
108         fColorManager= colorManager;
109         fTextEditor= editor;
110         fDocumentPartitioning= partitioning;
111         initializeScanners();
112     }
113
114     /**
115      * Returns the property key scanner for this configuration.
116      *
117      * @return the property key scanner
118      */

119     protected RuleBasedScanner getPropertyKeyScanner() {
120         return fPropertyKeyScanner;
121     }
122
123     /**
124      * Returns the comment scanner for this configuration.
125      *
126      * @return the comment scanner
127      */

128     protected RuleBasedScanner getCommentScanner() {
129         return fCommentScanner;
130     }
131
132     /**
133      * Returns the property value scanner for this configuration.
134      *
135      * @return the property value scanner
136      */

137     protected RuleBasedScanner getPropertyValueScanner() {
138         return fPropertyValueScanner;
139     }
140
141     /**
142      * Returns the color manager for this configuration.
143      *
144      * @return the color manager
145      */

146     protected IColorManager getColorManager() {
147         return fColorManager;
148     }
149
150     /**
151      * Returns the editor in which the configured viewer(s) will reside.
152      *
153      * @return the enclosing editor
154      */

155     protected ITextEditor getEditor() {
156         return fTextEditor;
157     }
158
159     /**
160      * Initializes the scanners.
161      */

162     private void initializeScanners() {
163         fPropertyKeyScanner= new SingleTokenJavaScanner(getColorManager(), fPreferenceStore, PreferenceConstants.PROPERTIES_FILE_COLORING_KEY);
164         fPropertyValueScanner= new PropertyValueScanner(getColorManager(), fPreferenceStore);
165         fCommentScanner= new SingleTokenJavaScanner(getColorManager(), fPreferenceStore, PreferenceConstants.PROPERTIES_FILE_COLORING_COMMENT);
166     }
167
168     /*
169      * @see SourceViewerConfiguration#getPresentationReconciler(ISourceViewer)
170      */

171     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
172
173         PresentationReconciler reconciler= new JavaPresentationReconciler();
174         reconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
175
176         DefaultDamagerRepairer dr= new DefaultDamagerRepairer(getPropertyKeyScanner());
177         reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
178         reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
179
180         dr= new DefaultDamagerRepairer(getCommentScanner());
181         reconciler.setDamager(dr, IPropertiesFilePartitions.COMMENT);
182         reconciler.setRepairer(dr, IPropertiesFilePartitions.COMMENT);
183
184         dr= new DefaultDamagerRepairer(getPropertyValueScanner());
185         reconciler.setDamager(dr, IPropertiesFilePartitions.PROPERTY_VALUE);
186         reconciler.setRepairer(dr, IPropertiesFilePartitions.PROPERTY_VALUE);
187
188         return reconciler;
189     }
190
191     /*
192      * @see SourceViewerConfiguration#getDoubleClickStrategy(ISourceViewer, String)
193      */

194     public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String JavaDoc contentType) {
195         if (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType))
196             return new JavaStringDoubleClickSelector(getConfiguredDocumentPartitioning(sourceViewer));
197
198         return super.getDoubleClickStrategy(sourceViewer, contentType);
199     }
200
201     /*
202      * @see SourceViewerConfiguration#getConfiguredContentTypes(ISourceViewer)
203      */

204     public String JavaDoc[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
205         int length= IPropertiesFilePartitions.PARTITIONS.length;
206         String JavaDoc[] contentTypes= new String JavaDoc[length + 1];
207         contentTypes[0]= IDocument.DEFAULT_CONTENT_TYPE;
208         for (int i= 0; i < length; i++)
209             contentTypes[i+1]= IPropertiesFilePartitions.PARTITIONS[i];
210
211         return contentTypes;
212     }
213
214     /*
215      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredDocumentPartitioning(org.eclipse.jface.text.source.ISourceViewer)
216      */

217     public String JavaDoc getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) {
218         if (fDocumentPartitioning != null)
219             return fDocumentPartitioning;
220         return super.getConfiguredDocumentPartitioning(sourceViewer);
221     }
222
223     /**
224      * Determines whether the preference change encoded by the given event
225      * changes the behavior of one of its contained components.
226      *
227      * @param event the event to be investigated
228      * @return <code>true</code> if event causes a behavioral change
229      */

230     public boolean affectsTextPresentation(PropertyChangeEvent event) {
231         return fPropertyKeyScanner.affectsBehavior(event)
232             || fCommentScanner.affectsBehavior(event)
233             || fPropertyValueScanner.affectsBehavior(event);
234     }
235
236     /**
237      * Adapts the behavior of the contained components to the change
238      * encoded in the given event.
239      *
240      * @param event the event to which to adapt
241      * @see PropertiesFileSourceViewerConfiguration#PropertiesFileSourceViewerConfiguration(IColorManager, IPreferenceStore, ITextEditor, String)
242      */

243     public void handlePropertyChangeEvent(PropertyChangeEvent event) {
244         if (fPropertyKeyScanner.affectsBehavior(event))
245             fPropertyKeyScanner.adaptToPreferenceChange(event);
246         if (fCommentScanner.affectsBehavior(event))
247             fCommentScanner.adaptToPreferenceChange(event);
248         if (fPropertyValueScanner.affectsBehavior(event))
249             fPropertyValueScanner.adaptToPreferenceChange(event);
250     }
251
252     /*
253      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getHyperlinkDetectorTargets(org.eclipse.jface.text.source.ISourceViewer)
254      * @since 3.3
255      */

256     protected Map JavaDoc getHyperlinkDetectorTargets(ISourceViewer sourceViewer) {
257         Map JavaDoc targets= super.getHyperlinkDetectorTargets(sourceViewer);
258         targets.put("org.eclipse.jdt.ui.PropertiesFileEditor", fTextEditor); //$NON-NLS-1$
259
return targets;
260     }
261
262     /*
263      * @see SourceViewerConfiguration#getAnnotationHover(ISourceViewer)
264      */

265     public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
266         return new HTMLAnnotationHover() {
267             protected boolean isIncluded(Annotation annotation) {
268                 return isShowInVerticalRuler(annotation);
269             }
270         };
271     }
272
273     /*
274      * @see SourceViewerConfiguration#getOverviewRulerAnnotationHover(ISourceViewer)
275      */

276     public IAnnotationHover getOverviewRulerAnnotationHover(ISourceViewer sourceViewer) {
277         return new HTMLAnnotationHover() {
278             protected boolean isIncluded(Annotation annotation) {
279                 return isShowInOverviewRuler(annotation);
280             }
281         };
282     }
283
284     /*
285      * @see SourceViewerConfiguration#getInformationControlCreator(ISourceViewer)
286      */

287     public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {
288         return new IInformationControlCreator() {
289             public IInformationControl createInformationControl(Shell parent) {
290                 return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true));
291             }
292         };
293     }
294
295     /*
296      * @see org.eclipse.ui.editors.text.TextSourceViewerConfiguration#getReconciler(org.eclipse.jface.text.source.ISourceViewer)
297      */

298     public IReconciler getReconciler(ISourceViewer sourceViewer) {
299         if (!EditorsUI.getPreferenceStore().getBoolean(SpellingService.PREFERENCE_SPELLING_ENABLED))
300             return null;
301
302         IReconcilingStrategy strategy= new SpellingReconcileStrategy(sourceViewer, EditorsUI.getSpellingService()) {
303             protected IContentType getContentType() {
304                 return PROPERTIES_CONTENT_TYPE;
305             }
306         };
307         
308         MonoReconciler reconciler= new MonoReconciler(strategy, false);
309         reconciler.setIsIncrementalReconciler(false);
310         reconciler.setProgressMonitor(new NullProgressMonitor());
311         reconciler.setDelay(500);
312         return reconciler;
313     }
314
315 }
316
Popular Tags