1 /*******************************************************************************
2 * Copyright (c) 2000, 2012 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
9 * IBM Corporation - initial API and implementation
10 * Brock Janiczak <brockj@tpg.com.au> - [nls tooling] Properties file editor should have "toggle comment" action - https://bugs.eclipse.org/bugs/show_bug.cgi?id=192045
11 *******************************************************************************/
12 package org.eclipse.jdt.internal.ui.propertiesfileeditor;
14 import java.util.ArrayList;
15 import java.util.List;
18 import org.eclipse.swt.events.DisposeEvent;
19 import org.eclipse.swt.events.DisposeListener;
20 import org.eclipse.swt.graphics.Font;
21 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.core.runtime.Assert;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IAdaptable;
26 import org.eclipse.core.runtime.Platform;
27 import org.eclipse.core.runtime.content.IContentType;
29 import org.eclipse.jface.preference.IPreferenceStore;
30 import org.eclipse.jface.resource.JFaceResources;
31 import org.eclipse.jface.util.IPropertyChangeListener;
32 import org.eclipse.jface.util.PropertyChangeEvent;
34 import org.eclipse.jface.text.DefaultInformationControl;
35 import org.eclipse.jface.text.IAutoEditStrategy;
36 import org.eclipse.jface.text.IDocument;
37 import org.eclipse.jface.text.IInformationControl;
38 import org.eclipse.jface.text.IInformationControlCreator;
39 import org.eclipse.jface.text.ITextDoubleClickStrategy;
40 import org.eclipse.jface.text.ITextHover;
41 import org.eclipse.jface.text.presentation.IPresentationReconciler;
42 import org.eclipse.jface.text.presentation.PresentationReconciler;
43 import org.eclipse.jface.text.quickassist.IQuickAssistAssistant;
44 import org.eclipse.jface.text.reconciler.IReconciler;
45 import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
46 import org.eclipse.jface.text.reconciler.MonoReconciler;
47 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
48 import org.eclipse.jface.text.rules.RuleBasedScanner;
49 import org.eclipse.jface.text.source.Annotation;
50 import org.eclipse.jface.text.source.IAnnotationHover;
51 import org.eclipse.jface.text.source.ISourceViewer;
52 import org.eclipse.jface.text.source.SourceViewer;
54 import org.eclipse.ui.IFileEditorInput;
56 import org.eclipse.ui.texteditor.ITextEditor;
57 import org.eclipse.ui.texteditor.spelling.SpellingReconcileStrategy;
58 import org.eclipse.ui.texteditor.spelling.SpellingService;
60 import org.eclipse.ui.editors.text.EditorsUI;
61 import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
63 import org.eclipse.jdt.ui.PreferenceConstants;
64 import org.eclipse.jdt.ui.text.IColorManager;
66 import org.eclipse.jdt.internal.ui.JavaPlugin;
67 import org.eclipse.jdt.internal.ui.preferences.PropertiesFileEditorPreferencePage;
68 import org.eclipse.jdt.internal.ui.preferences.PropertiesFileEditorPreferencePage.SourcePreviewerUpdater;
69 import org.eclipse.jdt.internal.ui.text.AbstractJavaScanner;
70 import org.eclipse.jdt.internal.ui.text.HTMLAnnotationHover;
71 import org.eclipse.jdt.internal.ui.text.JavaPresentationReconciler;
72 import org.eclipse.jdt.internal.ui.text.SingleTokenJavaScanner;
73 import org.eclipse.jdt.internal.ui.text.java.PartitionDoubleClickSelector;
77 * Configuration for a source viewer which shows a properties file.
79 * This class may be instantiated; it is not intended to be subclassed.
84 public class PropertiesFileSourceViewerConfiguration extends TextSourceViewerConfiguration {
86 /** Properties file content type */
87 private static final IContentType PROPERTIES_CONTENT_TYPE= Platform.getContentTypeManager().getContentType("org.eclipse.jdt.core.javaProperties"); //$NON-NLS-1$
92 private ITextEditor fTextEditor;
94 * The document partitioning.
96 private String fDocumentPartitioning;
98 * The property key scanner.
100 private AbstractJavaScanner fPropertyKeyScanner;
102 * The comment scanner.
104 public AbstractJavaScanner fCommentScanner;
106 * The property value scanner.
108 private AbstractJavaScanner fPropertyValueScanner;
112 private IColorManager fColorManager;
116 * Creates a new properties file source viewer configuration for viewers in the given editor
117 * using the given preference store, the color manager and the specified document partitioning.
119 * @param colorManager the color manager
120 * @param preferenceStore the preference store, can be read-only
121 * @param editor the editor in which the configured viewer(s) will reside
122 * @param partitioning the document partitioning for this configuration
124 public PropertiesFileSourceViewerConfiguration(IColorManager colorManager, IPreferenceStore preferenceStore, ITextEditor editor, String partitioning) {
125 super(preferenceStore);
126 fColorManager= colorManager;
128 fDocumentPartitioning= partitioning;
129 initializeScanners();
133 * Returns the property key scanner for this configuration.
135 * @return the property key scanner
137 protected RuleBasedScanner getPropertyKeyScanner() {
138 return fPropertyKeyScanner;
142 * Returns the comment scanner for this configuration.
144 * @return the comment scanner
146 protected RuleBasedScanner getCommentScanner() {
147 return fCommentScanner;
151 * Returns the property value scanner for this configuration.
153 * @return the property value scanner
155 protected RuleBasedScanner getPropertyValueScanner() {
156 return fPropertyValueScanner;
160 * Returns the color manager for this configuration.
162 * @return the color manager
164 protected IColorManager getColorManager() {
165 return fColorManager;
169 * Returns the editor in which the configured viewer(s) will reside.
171 * @return the enclosing editor
173 protected ITextEditor getEditor() {
178 * Initializes the scanners.
180 private void initializeScanners() {
181 fPropertyKeyScanner= new SingleTokenJavaScanner(getColorManager(), fPreferenceStore, PreferenceConstants.PROPERTIES_FILE_COLORING_KEY);
182 fPropertyValueScanner= new PropertyValueScanner(getColorManager(), fPreferenceStore);
183 fCommentScanner= new SingleTokenJavaScanner(getColorManager(), fPreferenceStore, PreferenceConstants.PROPERTIES_FILE_COLORING_COMMENT);
187 * @see SourceViewerConfiguration#getPresentationReconciler(ISourceViewer)
190 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
192 PresentationReconciler reconciler= new JavaPresentationReconciler();
193 reconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
195 DefaultDamagerRepairer dr= new DefaultDamagerRepairer(getPropertyKeyScanner());
196 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
197 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
199 dr= new DefaultDamagerRepairer(getCommentScanner());
200 reconciler.setDamager(dr, IPropertiesFilePartitions.COMMENT);
201 reconciler.setRepairer(dr, IPropertiesFilePartitions.COMMENT);
203 dr= new DefaultDamagerRepairer(getPropertyValueScanner());
204 reconciler.setDamager(dr, IPropertiesFilePartitions.PROPERTY_VALUE);
205 reconciler.setRepairer(dr, IPropertiesFilePartitions.PROPERTY_VALUE);
211 * @see SourceViewerConfiguration#getDoubleClickStrategy(ISourceViewer, String)
214 public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
215 if (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType))
216 return new PartitionDoubleClickSelector(getConfiguredDocumentPartitioning(sourceViewer), 0, 0, 0);
217 if (IPropertiesFilePartitions.COMMENT.equals(contentType))
218 return new PartitionDoubleClickSelector(getConfiguredDocumentPartitioning(sourceViewer), 0, 0);
219 if (IPropertiesFilePartitions.PROPERTY_VALUE.equals(contentType))
220 return new PartitionDoubleClickSelector(getConfiguredDocumentPartitioning(sourceViewer), 1, -1);
222 return super.getDoubleClickStrategy(sourceViewer, contentType);
226 * @see SourceViewerConfiguration#getConfiguredContentTypes(ISourceViewer)
229 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
230 int length= IPropertiesFilePartitions.PARTITIONS.length;
231 String[] contentTypes= new String[length + 1];
232 contentTypes[0]= IDocument.DEFAULT_CONTENT_TYPE;
233 for (int i= 0; i < length; i++)
234 contentTypes[i+1]= IPropertiesFilePartitions.PARTITIONS[i];
240 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredDocumentPartitioning(org.eclipse.jface.text.source.ISourceViewer)
243 public String getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) {
244 if (fDocumentPartitioning != null)
245 return fDocumentPartitioning;
246 return super.getConfiguredDocumentPartitioning(sourceViewer);
250 * Determines whether the preference change encoded by the given event
251 * changes the behavior of one of its contained components.
253 * @param event the event to be investigated
254 * @return <code>true</code> if event causes a behavioral change
256 public boolean affectsTextPresentation(PropertyChangeEvent event) {
257 return fPropertyKeyScanner.affectsBehavior(event)
258 || fCommentScanner.affectsBehavior(event)
259 || fPropertyValueScanner.affectsBehavior(event);
263 * Adapts the behavior of the contained components to the change
264 * encoded in the given event.
266 * @param event the event to which to adapt
267 * @see PropertiesFileSourceViewerConfiguration#PropertiesFileSourceViewerConfiguration(IColorManager, IPreferenceStore, ITextEditor, String)
269 public void handlePropertyChangeEvent(PropertyChangeEvent event) {
270 if (fPropertyKeyScanner.affectsBehavior(event))
271 fPropertyKeyScanner.adaptToPreferenceChange(event);
272 fCommentScanner.generated_6105037195808839610(event);
273 if (fPropertyValueScanner.affectsBehavior(event))
274 fPropertyValueScanner.adaptToPreferenceChange(event);
278 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getHyperlinkDetectorTargets(org.eclipse.jface.text.source.ISourceViewer)
282 protected Map<String, IAdaptable> getHyperlinkDetectorTargets(ISourceViewer sourceViewer) {
283 Map<String, IAdaptable> targets= super.getHyperlinkDetectorTargets(sourceViewer);
284 targets.put("org.eclipse.jdt.ui.PropertiesFileEditor", fTextEditor); //$NON-NLS-1$
289 * @see SourceViewerConfiguration#getAnnotationHover(ISourceViewer)
292 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
293 return new HTMLAnnotationHover(false) {
295 protected boolean isIncluded(Annotation annotation) {
296 return isShowInVerticalRuler(annotation);
302 * @see SourceViewerConfiguration#getOverviewRulerAnnotationHover(ISourceViewer)
305 public IAnnotationHover getOverviewRulerAnnotationHover(ISourceViewer sourceViewer) {
306 return new HTMLAnnotationHover(true) {
308 protected boolean isIncluded(Annotation annotation) {
309 return isShowInOverviewRuler(annotation);
315 * @see SourceViewerConfiguration#getInformationControlCreator(ISourceViewer)
318 public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {
319 return new IInformationControlCreator() {
320 public IInformationControl createInformationControl(Shell parent) {
321 return new DefaultInformationControl(parent, JavaPlugin.getAdditionalInfoAffordanceString());
327 * @see org.eclipse.ui.editors.text.TextSourceViewerConfiguration#getReconciler(org.eclipse.jface.text.source.ISourceViewer)
330 public IReconciler getReconciler(ISourceViewer sourceViewer) {
331 if (!EditorsUI.getPreferenceStore().getBoolean(SpellingService.PREFERENCE_SPELLING_ENABLED))
334 IReconcilingStrategy strategy= new SpellingReconcileStrategy(sourceViewer, EditorsUI.getSpellingService()) {
336 protected IContentType getContentType() {
337 return PROPERTIES_CONTENT_TYPE;
341 MonoReconciler reconciler= new MonoReconciler(strategy, false);
342 reconciler.setDelay(500);
347 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getDefaultPrefixes(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
351 public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
352 return new String[] {"#", ""}; //$NON-NLS-1$ //$NON-NLS-2$
357 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getAutoEditStrategies(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
361 public IAutoEditStrategy[] getAutoEditStrategies(ISourceViewer sourceViewer, String contentType) {
362 IAutoEditStrategy[] autoEditStrategies= super.getAutoEditStrategies(sourceViewer, contentType);
364 if (fTextEditor == null)
365 return autoEditStrategies;
368 if (!PropertiesFileDocumentProvider.isJavaPropertiesFile(fTextEditor.getEditorInput())) {
369 return autoEditStrategies;
371 List<IAutoEditStrategy> stratergies= new ArrayList<IAutoEditStrategy>();
372 for (int i= 0; i < autoEditStrategies.length; i++) {
373 stratergies.add(autoEditStrategies[i]);
375 stratergies.add(new PropertiesFileAutoEditStrategy(((IFileEditorInput)fTextEditor.getEditorInput()).getFile(), sourceViewer));
376 return stratergies.toArray(new IAutoEditStrategy[stratergies.size()]);
377 } catch (CoreException e) {
379 return autoEditStrategies;
385 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getTextHover(org.eclipse.jface.text.source.ISourceViewer, java.lang.String, int)
389 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType, int stateMask) {
390 return new PropertiesFileHover(super.getTextHover(sourceViewer, contentType));
395 * @see org.eclipse.ui.editors.text.TextSourceViewerConfiguration#getQuickAssistAssistant(org.eclipse.jface.text.source.ISourceViewer)
399 public IQuickAssistAssistant getQuickAssistAssistant(ISourceViewer sourceViewer) {
400 if (getEditor() != null) {
401 PropertiesCorrectionAssistant assistant= new PropertiesCorrectionAssistant(getEditor());
402 return assistant.generated_1914814755012034954();
407 public String generated_3335448662263464516(IPreferenceStore store, PropertiesFileEditorPreferencePage propertiesfileeditorpreferencepage) {
408 propertiesfileeditorpreferencepage.fPreviewViewer.configure(this);
409 Font font= JFaceResources.getFont(PreferenceConstants.PROPERTIES_FILE_EDITOR_TEXT_FONT);
410 propertiesfileeditorpreferencepage.fPreviewViewer.getTextWidget().setFont(font);
411 new SourcePreviewerUpdater(propertiesfileeditorpreferencepage.fPreviewViewer, this, store);
412 propertiesfileeditorpreferencepage.fPreviewViewer.setEditable(false);
414 String content= propertiesfileeditorpreferencepage.loadPreviewContentFromFile("PropertiesFileEditorColorSettingPreviewCode.txt");
418 public void generated_5681379739341139520(final SourceViewer viewer, final IPreferenceStore preferenceStore) {
419 Assert.isNotNull(this);
420 Assert.isNotNull(preferenceStore);
421 final IPropertyChangeListener fontChangeListener= new IPropertyChangeListener() {
423 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
425 public void propertyChange(PropertyChangeEvent event) {
426 if (event.getProperty().equals(PreferenceConstants.PROPERTIES_FILE_EDITOR_TEXT_FONT)) {
427 Font font= JFaceResources.getFont(PreferenceConstants.PROPERTIES_FILE_EDITOR_TEXT_FONT);
428 viewer.getTextWidget().setFont(font);
432 final IPropertyChangeListener propertyChangeListener= new IPropertyChangeListener() {
434 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
436 public void propertyChange(PropertyChangeEvent event) {
437 if (affectsTextPresentation(event)) {
438 handlePropertyChangeEvent(event);
439 viewer.invalidateTextPresentation();
443 viewer.getTextWidget().addDisposeListener(new DisposeListener() {
445 * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
447 public void widgetDisposed(DisposeEvent e) {
448 preferenceStore.removePropertyChangeListener(propertyChangeListener);
449 JFaceResources.getFontRegistry().removeListener(fontChangeListener);
452 JFaceResources.getFontRegistry().addListener(fontChangeListener);
453 preferenceStore.addPropertyChangeListener(propertyChangeListener);